blob: a50fc11cca56ec51869de39cc60632c5fb9795b7 [file] [log] [blame]
Brian Paul9abbeda2009-09-16 19:33:01 -06001/*
2 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22/**
23 * Common perf code. This should be re-usable with other APIs.
24 */
25
26#include "common.h"
27#include "glmain.h"
28
29
30/**
31 * Run function 'f' for enough iterations to reach a steady state.
32 * Return the rate (iterations/second).
33 */
34double
35PerfMeasureRate(PerfRateFunc f)
36{
37 const double minDuration = 1.0;
38 double rate = 0.0, prevRate = 0.0;
39 unsigned subiters;
40
41 /* Compute initial number of iterations to try.
42 * If the test function is pretty slow this helps to avoid
43 * extraordarily long run times.
44 */
45 subiters = 2;
46 {
47 const double t0 = PerfGetTime();
48 double t1;
49 do {
50 f(subiters); /* call the rendering function */
51 t1 = PerfGetTime();
52 subiters *= 2;
53 } while (t1 - t0 < 0.1 * minDuration);
54 }
55 /*printf("initial subIters = %u\n", subiters);*/
56
57 while (1) {
58 const double t0 = PerfGetTime();
59 unsigned iters = 0;
60 double t1;
61
62 do {
63 f(subiters); /* call the rendering function */
64 t1 = PerfGetTime();
65 iters += subiters;
66 } while (t1 - t0 < minDuration);
67
68 rate = iters / (t1 - t0);
69
70 if (0)
71 printf("prevRate %f rate %f ratio %f iters %u\n",
72 prevRate, rate, rate/prevRate, iters);
73
74 /* Try and speed the search up by skipping a few steps:
75 */
76 if (rate > prevRate * 1.6)
77 subiters *= 8;
78 else if (rate > prevRate * 1.2)
79 subiters *= 4;
80 else if (rate > prevRate * 1.05)
81 subiters *= 2;
82 else
83 break;
84
85 prevRate = rate;
86 }
87
88 if (0)
89 printf("%s returning iters %u rate %f\n", __FUNCTION__, subiters, rate);
90 return rate;
91}
92
93