Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / tbasicperf.cpp
blob94e91796e90f605220f7b0ba55f15c49b056e782
1 // BEGIN_COPYRIGHT -*- glean -*-
2 //
3 // Copyright (C) 2000 Allen Akin All Rights Reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
12 // conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
26 //
27 // END_COPYRIGHT
29 // tbasicperf.cpp: implementation of example class for basic
30 // performance tests
32 // To customize this for benchmarking a particular function,
33 // create a new performance test object of type GLEAN::Perf,
34 // overriding the preop(), op(), and postop() methods as needed.
35 // (For OpenGL timing tests preop() and postop() will both call
36 // glFinish(), but other pre- and post-ops may be used for
37 // timing things other than OpenGL.) Then invoke the object's
38 // calibrate() and time() methods as shown in runOne().
40 #include "tbasicperf.h"
41 #include "timer.h"
42 #include <algorithm>
44 namespace {
45 class MyPerf : public GLEAN::Timer {
46 public:
47 int msec;
49 void preop() { glFinish(); }
50 void op() {
51 #ifdef __WIN__
52 Sleep(msec); /* milliseconds */
53 #else
54 usleep(msec*1000); /* microseconds */
55 #endif
57 void postop() { glFinish(); }
59 MyPerf() { msec = 100; }
63 // Complex results helper functions
65 void
66 diffHeader(bool& same, const string& name,
67 GLEAN::DrawingSurfaceConfig* config, GLEAN::Environment* env) {
68 if (same) {
69 same = false;
70 env->log << name << ": DIFF "
71 << config->conciseDescription() << '\n';
73 } // diffHeader
77 namespace GLEAN {
79 ///////////////////////////////////////////////////////////////////////////////
80 // runOne: Run a single test case
81 ///////////////////////////////////////////////////////////////////////////////
82 void
83 BasicPerfTest::runOne(BasicPerfResult& r, Window &w) {
84 MyPerf perf;
86 perf.calibrate();
87 vector<float> m;
88 for (int i = 0; i < 5; i++) {
89 env->quiesce();
90 double t = perf.time();
91 w.swap(); // So user can see something
92 m.push_back(t);
94 sort(m.begin(), m.end());
95 r.timeAvg = (m[1] + m[2] + m[3]) / 3.0;
96 r.timeLow = m[1];
97 r.timeHigh = m[3];
98 r.pass = true;
99 } // BasicPerfTest::runOne
101 ///////////////////////////////////////////////////////////////////////////////
102 // logOne: Log a single test case
103 ///////////////////////////////////////////////////////////////////////////////
104 void
105 BasicPerfTest::logOne(BasicPerfResult& r) {
106 logPassFail(r);
107 logConcise(r);
108 logStats(r);
109 } // BasicPerfTest::logOne
111 ///////////////////////////////////////////////////////////////////////////////
112 // compareOne: Compare results for a single test case
113 ///////////////////////////////////////////////////////////////////////////////
114 void
115 BasicPerfTest::compareOne(BasicPerfResult& oldR, BasicPerfResult& newR) {
116 bool same = true;
117 const char *title = "100mS sleep";
119 if (newR.timeLow < oldR.timeLow) {
120 double percent = (100.0
121 * (oldR.timeLow - newR.timeLow)
122 / newR.timeLow + 0.5);
123 if (percent >= 5.0) {
124 diffHeader(same, name, oldR.config, env);
125 env->log << '\t'
126 << env->options.db1Name
127 << " may be "
128 << percent
129 << "% faster on "
130 << title
131 << '\n';
134 if (newR.timeHigh > oldR.timeHigh) {
135 double percent = (100.0
136 * (newR.timeHigh - oldR.timeHigh)
137 / oldR.timeHigh + 0.5);
138 if (percent >= 5.0) {
139 diffHeader(same, name, oldR.config, env);
140 env->log << '\t'
141 << env->options.db2Name
142 << " may be "
143 << percent
144 << "% faster on "
145 << title
146 << '\n';
150 if (same && env->options.verbosity) {
151 env->log << name
152 << ": SAME "
153 << newR.config->conciseDescription()
154 << "\n\t"
155 << env->options.db2Name
156 << " test time falls within the"
157 << " valid measurement range of\n\t"
158 << env->options.db1Name
159 << " test time.\n";
161 if (env->options.verbosity) {
162 env->log << env->options.db1Name << ':';
163 logStats(oldR);
164 env->log << env->options.db2Name << ':';
165 logStats(newR);
167 } // BasicPerfTest::compareOne
169 void
170 BasicPerfTest::logStats(BasicPerfResult& r) {
171 env->log << "\tAverage = "
172 << r.timeAvg
173 << "\tRange = ["
174 << r.timeLow
175 << ", "
176 << r.timeHigh
177 << "]\n";
180 ///////////////////////////////////////////////////////////////////////////////
181 // The test object itself:
182 ///////////////////////////////////////////////////////////////////////////////
183 BasicPerfTest basicPerfTest("basicPerf", "window",
184 "This trivial test simply verifies the internal support for basic\n"
185 "performance tests. It is run on every OpenGL-capable drawing surface\n"
186 "configuration that supports creation of a window. If everything is\n"
187 "working correctly, each result should be close to 0.1 second.\n");
189 } // namespace GLEAN