From e89f20ab46ee11050760c6d57f05c2a3825a911c Mon Sep 17 00:00:00 2001 From: "snappy.mirrorbot@gmail.com" Date: Wed, 4 Jul 2012 09:34:48 +0000 Subject: [PATCH] Handle the case where gettimeofday() goes backwards or returns the same value twice; it could cause division by zero in the unit test framework. (We already had one fix for this in place, but it was incomplete.) This could in theory happen on any system, since there are few guarantees about gettimeofday(), but seems to only happen in practice on GNU/Hurd, where gettimeofday() is cached and only updated ever so often. R=sanjay git-svn-id: https://snappy.googlecode.com/svn/trunk@65 03e5f5b5-db94-4691-08a0-1a8bf15f6143 --- snappy-test.cc | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/snappy-test.cc b/snappy-test.cc index 223cd92..6a0611e 100644 --- a/snappy-test.cc +++ b/snappy-test.cc @@ -204,27 +204,32 @@ void Benchmark::Run() { benchmark_runs[run].cpu_time_us = benchmark_cpu_time_us; } + string heading = StringPrintf("%s/%d", name_.c_str(), test_case_num); + string human_readable_speed; + nth_element(benchmark_runs, benchmark_runs + kMedianPos, benchmark_runs + kNumRuns, BenchmarkCompareCPUTime()); int64 real_time_us = benchmark_runs[kMedianPos].real_time_us; int64 cpu_time_us = benchmark_runs[kMedianPos].cpu_time_us; - int64 bytes_per_second = benchmark_bytes_processed * 1000000 / cpu_time_us; - - string heading = StringPrintf("%s/%d", name_.c_str(), test_case_num); - string human_readable_speed; - if (bytes_per_second < 1024) { - human_readable_speed = StringPrintf("%dB/s", bytes_per_second); - } else if (bytes_per_second < 1024 * 1024) { - human_readable_speed = StringPrintf( - "%.1fkB/s", bytes_per_second / 1024.0f); - } else if (bytes_per_second < 1024 * 1024 * 1024) { - human_readable_speed = StringPrintf( - "%.1fMB/s", bytes_per_second / (1024.0f * 1024.0f)); + if (cpu_time_us <= 0) { + human_readable_speed = "?"; } else { - human_readable_speed = StringPrintf( - "%.1fGB/s", bytes_per_second / (1024.0f * 1024.0f * 1024.0f)); + int64 bytes_per_second = + benchmark_bytes_processed * 1000000 / cpu_time_us; + if (bytes_per_second < 1024) { + human_readable_speed = StringPrintf("%dB/s", bytes_per_second); + } else if (bytes_per_second < 1024 * 1024) { + human_readable_speed = StringPrintf( + "%.1fkB/s", bytes_per_second / 1024.0f); + } else if (bytes_per_second < 1024 * 1024 * 1024) { + human_readable_speed = StringPrintf( + "%.1fMB/s", bytes_per_second / (1024.0f * 1024.0f)); + } else { + human_readable_speed = StringPrintf( + "%.1fGB/s", bytes_per_second / (1024.0f * 1024.0f * 1024.0f)); + } } fprintf(stderr,