nanobench: fix and simplify --samplingTime

Recent changes to WallTimer broke --samplingTime.  In particular, this idiom became nonsensical:
   WallTimer timer;
   timer.start();
   do {
     ...
     timer.end();
   } while(timer.fWall < ...);

WallTimer started making private use of fWall between when start() and end() were called, so the second time around the loop we end up with nonsense.

If that makes no sense, don't worry.  The code here using now_ms() is just as fast, just as precise, and clearer.

I took the opportunity to simplify --samplingTime <complicated string parsing> to --ms <int>, and to simplify the code that depends on it.

BUG=skia:

Review URL: https://codereview.chromium.org/1419103004
diff --git a/bench/nanobench.cpp b/bench/nanobench.cpp
index de4545f..dad280d 100644
--- a/bench/nanobench.cpp
+++ b/bench/nanobench.cpp
@@ -25,7 +25,6 @@
 #include "SubsetTranslateBench.h"
 #include "SubsetZoomBench.h"
 #include "Stats.h"
-#include "Timer.h"
 
 #include "SkBitmapRegionDecoderInterface.h"
 #include "SkBBoxHierarchy.h"
@@ -59,8 +58,6 @@
 
 __SK_FORCE_IMAGE_DECODER_LINKING;
 
-static const int kTimedSampling = 0;
-
 static const int kAutoTuneLoops = 0;
 
 static const int kDefaultLoops =
@@ -87,8 +84,7 @@
 DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
 
 DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
-DEFINE_string(samplingTime, "0", "Amount of time to run each bench. Takes precedence over samples."
-                                 "Must be \"0\", \"%%lfs\", or \"%%lfms\"");
+DEFINE_int32(ms, 0, "If >0, run each bench for this many ms instead of obeying --samples.");
 DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
 DEFINE_double(overheadGoal, 0.0001,
               "Loop until timer overhead is at most this fraction of our measurments.");
@@ -114,6 +110,8 @@
 DEFINE_bool(pngBuildTileIndex, false, "If supported, use png buildTileIndex/decodeSubset.");
 DEFINE_bool(jpgBuildTileIndex, false, "If supported, use jpg buildTileIndex/decodeSubset.");
 
+static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
+
 static SkString humanize(double ms) {
     if (FLAGS_verbose) return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
     return HumanizeMs(ms);
@@ -210,26 +208,23 @@
         canvas->clear(SK_ColorWHITE);
     }
     bench->preDraw(canvas);
-    WallTimer timer;
-    timer.start();
+    double start = now_ms();
     canvas = target->beginTiming(canvas);
     bench->draw(loops, canvas);
     if (canvas) {
         canvas->flush();
     }
     target->endTiming();
-    timer.end();
+    double elapsed = now_ms() - start;
     bench->postDraw(canvas);
-    return timer.fWall;
+    return elapsed;
 }
 
 static double estimate_timer_overhead() {
     double overhead = 0;
     for (int i = 0; i < FLAGS_overheadLoops; i++) {
-        WallTimer timer;
-        timer.start();
-        timer.end();
-        overhead += timer.fWall;
+        double start = now_ms();
+        overhead += now_ms() - start;
     }
     return overhead / FLAGS_overheadLoops;
 }
@@ -1160,24 +1155,6 @@
         FLAGS_verbose = true;
     }
 
-    double samplingTimeMs = 0;
-    if (0 != strcmp("0", FLAGS_samplingTime[0])) {
-        SkSTArray<8, char> timeUnit;
-        timeUnit.push_back_n(static_cast<int>(strlen(FLAGS_samplingTime[0])) + 1);
-        if (2 != sscanf(FLAGS_samplingTime[0], "%lf%s", &samplingTimeMs, timeUnit.begin()) ||
-            (0 != strcmp("s", timeUnit.begin()) && 0 != strcmp("ms", timeUnit.begin()))) {
-            SkDebugf("Invalid --samplingTime \"%s\". Must be \"0\", \"%%lfs\", or \"%%lfms\"\n",
-                     FLAGS_samplingTime[0]);
-            exit(0);
-        }
-        if (0 == strcmp("s", timeUnit.begin())) {
-            samplingTimeMs *= 1000;
-        }
-        if (samplingTimeMs) {
-            FLAGS_samples = kTimedSampling;
-        }
-    }
-
     if (kAutoTuneLoops != FLAGS_loops) {
         FLAGS_samples     = 1;
         FLAGS_gpuFrameLag = 0;
@@ -1221,7 +1198,7 @@
         SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
     } else if (FLAGS_quiet) {
         SkDebugf("median\tbench\tconfig\n");
-    } else if (kTimedSampling == FLAGS_samples) {
+    } else if (FLAGS_ms) {
         SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
     } else {
         SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
@@ -1261,23 +1238,17 @@
                 ? setup_gpu_bench(target, bench.get(), maxFrameLag)
                 : setup_cpu_bench(overhead, target, bench.get());
 
-            if (kTimedSampling != FLAGS_samples) {
+            if (FLAGS_ms) {
+                samples.reset();
+                auto stop = now_ms() + FLAGS_ms;
+                do {
+                    samples.push_back(time(loops, bench, target) / loops);
+                } while (now_ms() < stop);
+            } else {
                 samples.reset(FLAGS_samples);
                 for (int s = 0; s < FLAGS_samples; s++) {
                     samples[s] = time(loops, bench, target) / loops;
                 }
-            } else if (samplingTimeMs) {
-                samples.reset();
-                if (FLAGS_verbose) {
-                    SkDebugf("Begin sampling %s for %ims\n",
-                             bench->getUniqueName(), static_cast<int>(samplingTimeMs));
-                }
-                WallTimer timer;
-                timer.start();
-                do {
-                    samples.push_back(time(loops, bench, target) / loops);
-                    timer.end();
-                } while (timer.fWall < samplingTimeMs);
             }
 
             bench->perCanvasPostDraw(canvas);
@@ -1331,8 +1302,7 @@
                         , HUMANIZE(stats.mean)
                         , HUMANIZE(stats.max)
                         , stddev_percent
-                        , kTimedSampling != FLAGS_samples ? stats.plot.c_str()
-                                                          : to_string(samples.count()).c_str()
+                        , FLAGS_ms ? to_string(samples.count()).c_str() : stats.plot.c_str()
                         , config
                         , bench->getUniqueName()
                         );