Fix public issue #38: Make the microbenchmark framework handle
properly cases where gettimeofday() can stand return the same
result twice (as sometimes on GNU/Hurd) or go backwards
(as when the user adjusts the clock). We avoid a division-by-zero,
and put a lower bound on the number of iterations -- the same
amount as we use to calibrate.

We should probably use CLOCK_MONOTONIC for platforms that support
it, to be robust against clock adjustments; we already use Windows'
monotonic timers. However, that's for a later changelist.

R=csilvers
DELTA=7  (5 added, 0 deleted, 2 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1798


git-svn-id: https://snappy.googlecode.com/svn/trunk@34 03e5f5b5-db94-4691-08a0-1a8bf15f6143
diff --git a/snappy-test.cc b/snappy-test.cc
index c9aae2e..4c8caaf 100644
--- a/snappy-test.cc
+++ b/snappy-test.cc
@@ -182,11 +182,16 @@
     (*function_)(kCalibrateIterations, test_case_num);
     StopBenchmarkTiming();
 
-    // Let each test case run for about 200ms.
+    // Let each test case run for about 200ms, but at least as many
+    // as we used to calibrate.
     // Run five times and pick the median.
     const int kNumRuns = 5;
     const int kMedianPos = kNumRuns / 2;
-    int num_iterations = 200000 * kCalibrateIterations / benchmark_real_time_us;
+    int num_iterations = 0;
+    if (benchmark_real_time_us > 0) {
+      num_iterations = 200000 * kCalibrateIterations / benchmark_real_time_us;
+    }
+    num_iterations = max(num_iterations, kCalibrateIterations);
     BenchmarkRun benchmark_runs[kNumRuns];
 
     for (int run = 0; run < kNumRuns; ++run) {