headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / servers / app / benchmark / StringTest.cpp
blob64ffc80e214eff5faee454991730874372bfbb02
1 /*
2 * Copyright (C) 2008-2009 Stephan Aßmus <superstippi@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 */
6 #include "StringTest.h"
8 #include <stdio.h>
10 #include <String.h>
11 #include <View.h>
13 #include "TestSupport.h"
16 StringTest::StringTest()
17 : Test(),
18 fTestDuration(0),
19 fTestStart(-1),
20 fGlyphsRendered(0),
21 fGlyphsPerLine(500),
22 fIterations(0),
23 fMaxIterations(1500),
25 fStartHeight(11.0),
26 fLineHeight(15.0)
31 StringTest::~StringTest()
36 void
37 StringTest::Prepare(BView* view)
39 // SetupClipping(view);
41 font_height fh;
42 view->GetFontHeight(&fh);
43 fLineHeight = ceilf(fh.ascent) + ceilf(fh.descent)
44 + ceilf(fh.leading);
45 fStartHeight = ceilf(fh.ascent) + ceilf(fh.descent);
46 fViewBounds = view->Bounds();
48 BString string;
49 string.Append('M', fGlyphsPerLine);
50 while (view->StringWidth(string.String()) < fViewBounds.Width() - 10)
51 string.Append('M', 1);
52 while (view->StringWidth(string.String()) > fViewBounds.Width() - 10)
53 string.Remove(string.Length() - 1, 1);
55 fGlyphsPerLine = 60; //string.Length();
56 fTestDuration = 0;
57 fGlyphsRendered = 0;
58 fIterations = 0;
59 fTestStart = system_time();
62 bool
63 StringTest::RunIteration(BView* view)
65 BPoint textLocation;
66 textLocation.x = 5;
67 textLocation.y = random_number_between(fStartHeight,
68 fStartHeight + fLineHeight / 2);
70 char buffer[fGlyphsPerLine + 1];
71 buffer[fGlyphsPerLine] = 0;
73 bigtime_t now = system_time();
75 while (true) {
76 // fill string with random chars
77 for (uint32 j = 0; j < fGlyphsPerLine; j++)
78 buffer[j] = 'A' + rand() % ('z' - 'A');
80 view->DrawString(buffer, textLocation);
82 fGlyphsRendered += fGlyphsPerLine;
84 // offset text location
85 textLocation.y += fLineHeight;
86 if (textLocation.y > fViewBounds.bottom)
87 break;
90 view->Sync();
92 fTestDuration += system_time() - now;
93 fIterations++;
95 return fIterations < fMaxIterations;
99 void
100 StringTest::PrintResults(BView* view)
102 if (fTestDuration == 0) {
103 printf("Test was not run.\n");
104 return;
106 bigtime_t timeLeak = system_time() - fTestStart - fTestDuration;
108 Test::PrintResults(view);
110 printf("Glyphs per DrawString() call: %ld\n", fGlyphsPerLine);
111 printf("Glyphs per second: %.3f\n",
112 fGlyphsRendered * 1000000.0 / fTestDuration);
113 printf("Average time between iterations: %.4f seconds.\n",
114 (float)timeLeak / fIterations / 1000000);
118 Test*
119 StringTest::CreateTest()
121 return new StringTest();