| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "main.h" | 5 #include "main.h" |
| 6 #include "testbase.h" |
| 6 | 7 |
| 7 uint64_t TimeBench(BenchFunc func, int iter) { | 8 uint64_t TimeTest(glbench::TestBase* test, int iter) { |
| 8 SwapBuffers(); | 9 SwapBuffers(); |
| 9 glFinish(); | 10 glFinish(); |
| 10 uint64_t time1 = GetUTime(); | 11 uint64_t time1 = GetUTime(); |
| 11 func(iter); | 12 test->TestFunc(iter); |
| 12 glFinish(); | 13 glFinish(); |
| 13 uint64_t time2 = GetUTime(); | 14 uint64_t time2 = GetUTime(); |
| 14 return time2 - time1; | 15 return time2 - time1; |
| 15 } | 16 } |
| 16 | 17 |
| 17 // Benchmark some draw commands, by running it many times. | 18 // Benchmark some draw commands, by running it many times. |
| 18 // We want to measure the marginal cost, so we try more and more iterations | 19 // We want to measure the marginal cost, so we try more and more iterations |
| 19 // until we get a somewhat linear response (to eliminate constant cost), and we | 20 // until we get a somewhat linear response (to eliminate constant cost), and we |
| 20 // do a linear regression on a few samples. | 21 // do a linear regression on a few samples. |
| 21 bool Bench(BenchFunc func, float *slope, int64_t *bias) { | 22 bool Bench(glbench::TestBase* test, float *slope, int64_t *bias) { |
| 22 // Do one iteration in case the driver needs to set up states. | 23 // Do one iteration in case the driver needs to set up states. |
| 23 if (TimeBench(func, 1) > MAX_ITERATION_DURATION_MS) | 24 if (TimeTest(test, 1) > MAX_ITERATION_DURATION_MS) |
| 24 return false; | 25 return false; |
| 25 int64_t count = 0; | 26 int64_t count = 0; |
| 26 int64_t sum_x = 0; | 27 int64_t sum_x = 0; |
| 27 int64_t sum_y = 0; | 28 int64_t sum_y = 0; |
| 28 int64_t sum_xy = 0; | 29 int64_t sum_xy = 0; |
| 29 int64_t sum_x2 = 0; | 30 int64_t sum_x2 = 0; |
| 30 uint64_t last_time = 0; | 31 uint64_t last_time = 0; |
| 31 bool do_count = false; | 32 bool do_count = false; |
| 32 uint64_t iter; | 33 uint64_t iter; |
| 33 for (iter = 8; iter < 1<<30; iter *= 2) { | 34 for (iter = 8; iter < 1<<30; iter *= 2) { |
| 34 uint64_t time = TimeBench(func, iter); | 35 uint64_t time = TimeTest(test, iter); |
| 35 if (last_time > 0 && (time > last_time * 1.8)) | 36 if (last_time > 0 && (time > last_time * 1.8)) |
| 36 do_count = true; | 37 do_count = true; |
| 37 last_time = time; | 38 last_time = time; |
| 38 if (do_count) { | 39 if (do_count) { |
| 39 ++count; | 40 ++count; |
| 40 sum_x += iter; | 41 sum_x += iter; |
| 41 sum_y += time; | 42 sum_y += time; |
| 42 sum_xy += iter * time; | 43 sum_xy += iter * time; |
| 43 sum_x2 += iter * iter; | 44 sum_x2 += iter * iter; |
| 44 } | 45 } |
| 45 if ((time >= 500000 && count > 4)) | 46 if ((time >= 500000 && count > 4)) |
| 46 break; | 47 break; |
| 47 } | 48 } |
| 48 if (count < 2) { | 49 if (count < 2) { |
| 49 *slope = 0.f; | 50 *slope = 0.f; |
| 50 *bias = 0; | 51 *bias = 0; |
| 51 } | 52 } |
| 52 *slope = static_cast<float>(sum_x * sum_y - count * sum_xy) / | 53 *slope = static_cast<float>(sum_x * sum_y - count * sum_xy) / |
| 53 (sum_x * sum_x - count * sum_x2); | 54 (sum_x * sum_x - count * sum_x2); |
| 54 *bias = (sum_x * sum_xy - sum_x2 * sum_y) / (sum_x * sum_x - count * sum_x2); | 55 *bias = (sum_x * sum_xy - sum_x2 * sum_y) / (sum_x * sum_x - count * sum_x2); |
| 55 return true; | 56 return true; |
| 56 } | 57 } |
| OLD | NEW |