run_benchmark.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following disclaimer
  10. // in the documentation and/or other materials provided with the
  11. // distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived from
  14. // this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #include "run_benchmark.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/time.h>
  32. struct internal_bench {
  33. bench_body body;
  34. uintptr_t param;
  35. };
  36. static void run_body(struct internal_bench *b, long iterations)
  37. {
  38. b->body(iterations, b->param);
  39. }
  40. static double measure_once(struct internal_bench *b, long iterations)
  41. {
  42. struct timeval tv_before, tv_after;
  43. int rv;
  44. double time;
  45. rv = gettimeofday(&tv_before, NULL);
  46. if (rv) {
  47. perror("gettimeofday");
  48. abort();
  49. }
  50. run_body(b, iterations);
  51. rv = gettimeofday(&tv_after, NULL);
  52. if (rv) {
  53. perror("gettimeofday");
  54. abort();
  55. }
  56. tv_after.tv_sec -= tv_before.tv_sec;
  57. time = tv_after.tv_sec * 1E6 + tv_after.tv_usec;
  58. time -= tv_before.tv_usec;
  59. time *= 1000;
  60. return time;
  61. }
  62. #define TRIAL_NSEC 0.3E9
  63. #define TARGET_NSEC 3E9
  64. static double run_benchmark(struct internal_bench *b)
  65. {
  66. long iterations = 128;
  67. double nsec;
  68. while (1) {
  69. nsec = measure_once(b, iterations);
  70. if (nsec > TRIAL_NSEC) {
  71. break;
  72. }
  73. iterations <<= 1;
  74. }
  75. while (nsec < TARGET_NSEC) {
  76. iterations = (long)(iterations * TARGET_NSEC * 1.1 / nsec);
  77. nsec = measure_once(b, iterations);
  78. }
  79. return nsec / iterations;
  80. }
  81. void report_benchmark(const char *name, bench_body body, uintptr_t param)
  82. {
  83. int i;
  84. struct internal_bench b = {.body = body, .param = param};
  85. for (i = 0; i < 3; i++) {
  86. double nsec = run_benchmark(&b);
  87. int slen;
  88. int padding_size;
  89. slen = printf("Benchmark: %s", name);
  90. if (param && name[strlen(name)-1] != ')') {
  91. slen += printf("(%lld)", (long long)param);
  92. }
  93. padding_size = 60 - slen;
  94. if (padding_size < 1) {
  95. padding_size = 1;
  96. }
  97. printf("%*c%f nsec\n", padding_size, ' ', nsec);
  98. fflush(stdout);
  99. }
  100. }