Index: tools/bbh_shootout.cpp |
diff --git a/tools/bbh_shootout.cpp b/tools/bbh_shootout.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c6566de29d52b6c1694c99e67b9763aa12de5772 |
--- /dev/null |
+++ b/tools/bbh_shootout.cpp |
@@ -0,0 +1,314 @@ |
+/* |
+ * Copyright 2013 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "BenchTimer.h" |
+#include "PictureBenchmark.h" |
+#include "PictureRenderer.h" |
+#include "PictureRenderingFlags.h" |
+#include "SkCommandLineFlags.h" |
+#include "SkForceLinking.h" |
+#include "SkStream.h" |
+#include "SkString.h" |
+#include "SkGraphics.h" |
caryclark
2013/07/10 15:28:23
alphabetize
|
+#include "TimerData.h" |
+ |
+ |
+__SK_FORCE_IMAGE_DECODER_LINKING; |
+ |
+static const int kNumRecordings = 10; |
+static const int kNumPlaybacks = 1; |
+ |
+static const int kNumTileDimensions = 2; |
+ |
+static const int gTileDimensions[kNumTileDimensions][2] = { |
caryclark
2013/07/10 15:28:23
use [] instead of [2]
|
+ {256, 256}, |
+ {512, 512}, |
+}; |
+ |
+enum BenchmarkType { |
+ kNormal_BenchmarkType = 0, |
+ kRTree_BenchmarkType, |
+}; |
+ |
+struct Histogram { |
+ int pathIndex; |
+ SkScalar cpuTime; |
caryclark
2013/07/10 15:28:23
fPathIndex
fCpuTime
|
+}; |
+ |
+// Defined in PictureRenderingFlags.cpp |
+extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap); |
+ |
+static SkPicture* pic_from_path(const char path[]) { |
+ SkASSERT(NULL != success); |
+ SkFILEStream stream(path); |
+ if (!stream.isValid()) { |
+ SkDebugf("-- Can't open '%s'\n", path); |
+ return NULL; |
+ } |
+ return SkPicture::CreateFromStream(&stream, &lazy_decode_bitmap); |
+} |
+ |
+/** |
+ * This function is the sink to which all work ends up going. |
+ * Renders the picture into the renderer. It may or may not use an RTree. |
+ * The renderer is chosen upstream. If we want to measure recording, we will |
+ * use a RecordPictureRenderer. If we want to measure rendering, we eill use a |
+ * TiledPictureRenderer. |
+ */ |
+static void do_benchmark_work(sk_tools::PictureRenderer* renderer, |
+ int benchmarkType, const SkString *path, SkPicture* pic, |
+ const int numRepeats, const char *msg) { |
+ SkASSERT(NULL != pic); |
+ |
+ SkString msgPrefix; |
+ |
+ switch (benchmarkType){ |
+ case kNormal_BenchmarkType: |
+ msgPrefix.printf("Normal"); |
+ renderer->setBBoxHierarchyType(sk_tools::PictureRenderer::kNone_BBoxHierarchyType); |
+ break; |
+ case kRTree_BenchmarkType: |
+ msgPrefix.printf("RTree"); |
+ renderer->setBBoxHierarchyType(sk_tools::PictureRenderer::kRTree_BBoxHierarchyType); |
+ break; |
+ } |
+ |
+ renderer->init(pic); |
+ |
+ SkDebugf("%s %s %s %d times...\n", msgPrefix.c_str(), msg, path->c_str(), numRepeats); |
+ for (int i = 0; i < numRepeats; ++i) { |
+ renderer->setup(); |
+ bool result = renderer->render(path); |
+ if(!result) { |
+ SkDebugf("Error recording.\n"); |
+ } |
+ } |
+ renderer->end(); |
+} |
+ |
+/** |
+ * Call do_benchmark_work with a tiled renderer using the default tile dimensions. |
+ */ |
+static void benchmark_playback(BenchmarkType benchmarkType, const SkString* path, SkPicture* pic) { |
+ sk_tools::TiledPictureRenderer renderer; |
+ |
+ for (int i = 0; i < kNumTileDimensions; ++i) { |
+ SkDebugf("Setting tile dimensions %dx%d\n", gTileDimensions[i][0], gTileDimensions[i][1]); |
+ renderer.setTileWidth(gTileDimensions[i][0]); |
+ renderer.setTileHeight(gTileDimensions[i][1]); |
+ |
+ do_benchmark_work(&renderer, benchmarkType, path, pic, kNumPlaybacks, "tiled playback"); |
+ } |
+} |
+ |
+/** |
+ * Call do_benchmark_work with a RecordPictureRenderer. |
+ */ |
+static void benchmark_recording(BenchmarkType benchmarkType, const SkString* path, SkPicture* pic) { |
+ sk_tools::RecordPictureRenderer renderer; |
+ |
+ do_benchmark_work(&renderer, benchmarkType, path, pic, kNumRecordings, "recording"); |
+} |
+ |
+static void (*benchmark_functions[2])(BenchmarkType, const SkString*, SkPicture*); |
+ |
+static const SkString perIterTimeFormat("%f"); |
+static const SkString normalTimeFormat("%f"); |
+ |
+/** |
+ * Takes argc,argv along with one of the benchmark functions defined above. |
+ * Will loop along all skp files and perform measurments. |
+ * |
+ * Returns a SkScalar representing CPU time taken during benchmark. |
+ * As a side effect, it spits the timer result to stdout. |
+ * Will return -1.0 on error. |
+ */ |
+static SkScalar benchmark_loop( |
+ int argc, |
+ char **argv, |
+ void (*func)(BenchmarkType, const SkString*, SkPicture*), |
+ Histogram histogram[], |
+ BenchmarkType benchmarkType, |
+ const char* configName) { |
+ TimerData timerData(perIterTimeFormat, normalTimeFormat); |
+ for (int index = 1; index < argc; ++index) { |
+ BenchTimer timer; |
+ SkString path(argv[index]); |
+ SkAutoTUnref<SkPicture> pic(pic_from_path(argv[index])); |
+ timer.start(); |
+ if (NULL != pic) { |
+ func(benchmarkType, &path, pic); |
+ } |
+ timer.end(); |
+ timerData.appendTimes(&timer, index == argc - 1); |
+ |
+ histogram[index - 1].pathIndex = index; |
+ histogram[index - 1].cpuTime = timer.fCpu; |
+ } |
+ |
+ const SkString timerResult = timerData.getResult( |
+ /*logPerIter = */ false, |
+ /*printMin = */ false, |
+ /*repeatDraw = */ 1, |
+ /*configName = */ configName, |
+ /*showWallTime = */ false, |
+ /*showTruncatedWallTime = */ false, |
+ /*showCpuTime = */ true, |
+ /*showTruncatedCpuTime = */ false, |
+ /*showGpuTime = */ false); |
+ |
+ const char findStr[] = "= "; |
+ int pos = timerResult.find(findStr); |
+ if (-1 == pos) { |
+ SkDebugf("Unexpected output from TimerData::getResult(...). Unable to parse."); |
+ return -1.0; |
caryclark
2013/07/10 15:28:23
this may generate a warning since it's a double ca
|
+ } |
+ SkDebugf("%s\n", timerResult.c_str()); |
+ |
+ SkScalar cpuTime = atof(timerResult.c_str() + pos + sizeof(findStr) - 1); |
+ if (cpuTime == SkIntToScalar(0)) { // atof returns 0.0 on error. |
+ SkDebugf("Unable to read value from timer result.\n"); |
+ return -1.0; |
+ } |
+ return cpuTime; |
+} |
+ |
+static int tool_main(int argc, char** argv) { |
+ SkAutoGraphics ag; |
+ SkString usage; |
+ usage.printf("Usage: filename [filename]*\n"); |
+ |
+ if (argc < 2) { |
+ SkDebugf("%s\n", usage.c_str()); |
+ return 0; |
+ } |
+ |
+ benchmark_functions[0] = benchmark_recording; |
+ benchmark_functions[1] = benchmark_playback; |
+ |
+ static const int kNumBenchmarks = 4; |
+ static const char* benchNames[] = { |
+ "normal_recording", |
+ "normal_playback", |
+ "rtree_recording", |
+ "rtree_playback", |
+ }; |
+ // We want names for these variables to make the arithmetic clearer. |
+ SkScalar normalRecordResult; |
+ SkScalar rtreeRecordResult; |
+ SkScalar normalPlaybackResult; |
+ SkScalar rtreePlaybackResult; |
+ static SkScalar* resultPointers[] = { |
+ &normalRecordResult, |
+ &normalPlaybackResult, |
+ &rtreeRecordResult, |
+ &rtreePlaybackResult, |
+ }; |
+ // One histogram for every benchmark. |
+ static Histogram *histograms[] = { |
+ SkNEW_ARRAY(Histogram, argc - 1), |
+ SkNEW_ARRAY(Histogram, argc - 1), |
+ SkNEW_ARRAY(Histogram, argc - 1), |
+ SkNEW_ARRAY(Histogram, argc - 1), |
+ }; |
+ |
+ for (int i = 0; i < kNumBenchmarks; ++i) { |
+ BenchmarkType type; |
+ if (i < 2) { |
+ type = kNormal_BenchmarkType; |
+ } else { |
+ type = kRTree_BenchmarkType; |
+ } |
+ |
+ SkString benchmarkName(benchNames[i]); |
+ SkScalar *resultPtr = resultPointers[i]; |
+ *resultPtr = benchmark_loop( |
+ argc, argv, benchmark_functions[i % 2], histograms[i], |
+ type, benchmarkName.c_str()); |
+ } |
+ |
+ // Print results |
+ SkDebugf("\n"); |
+ for (int i = 0; i < kNumBenchmarks; ++i) { |
+ SkDebugf("%s total: \t%f\n", benchNames[i], *resultPointers[i]); |
+ } |
+ |
+ SkASSERT(normalRecordResult != 0 && normalPlaybackResult != 0); |
+ SkDebugf("\n"); |
+ SkDebugf("Recording: Relative difference: %.4f\n", rtreeRecordResult / normalRecordResult); |
+ SkDebugf("Playback: Relative difference: %.4f\n", rtreePlaybackResult / normalPlaybackResult); |
+ |
+ SkScalar times = |
+ (kNumPlaybacks * (normalRecordResult - rtreeRecordResult)) / |
+ (kNumRecordings * (rtreePlaybackResult - normalPlaybackResult)); |
+ |
+ SkDebugf("Number of playback repetitions for RTree to be worth it: %d (ratio: %.4f)\n", |
+ SkScalarCeilToInt(times), times); |
+ |
+ // Print min/max times. |
+ SkDebugf("\n"); |
+ SkScalar minMax[][2] = { |
+ // MIN MAX |
+ {SK_ScalarMax, 0}, |
+ {SK_ScalarMax, 0}, |
+ {SK_ScalarMax, 0}, |
+ {SK_ScalarMax, 0}, |
+ }; |
+ for (int i = 0; i < argc - 1; ++i) { |
+ for (int j = 0; j < kNumBenchmarks; ++j) { |
+ SkScalar value = histograms[j][i].cpuTime; |
+ if (value < minMax[j][0]) { |
+ minMax[j][0] = value; |
+ } |
+ if (value > minMax[j][1]) { |
+ minMax[j][1] = value; |
+ } |
+ } |
+ } |
+ for (int i = 0; i < kNumBenchmarks; ++i) { |
+ SkString out; |
+ out.printf("%s min is ", benchNames[i]); |
+ out.appendf("%f\n", minMax[i][0]); |
+ out.appendf("%s max is ", benchNames[i]); |
+ out.appendf("%f\n", minMax[i][1]); |
+ SkDebugf("%s", out.c_str()); |
+ } |
+ |
+ // Output gnuplot readable histogram data.. |
+ const char* pbTitle = "bbh_shootout_playback.dat"; |
+ const char* recTitle = "bbh_shootout_record.dat"; |
+ SkFILEWStream playbackOut(pbTitle); |
+ SkFILEWStream recordOut(recTitle); |
+ playbackOut.writeText("# Index Normal RTree\n"); |
+ recordOut.writeText("# Index Normal RTree\n"); |
+ for (int i = 0; i < argc - 1; ++i) { |
+ SkString pbLine; |
+ SkString recLine; |
+ pbLine.printf("%d ", i); |
+ recLine.printf("%d ", i); |
+ for (int j = 0; j < kNumBenchmarks; j += 2) { |
+ recLine.appendf("%f ", histograms[j][i].cpuTime); |
+ pbLine.appendf("%f ", histograms[j + 1][i].cpuTime); |
+ } |
+ pbLine.appendf("\n"); |
+ recLine.appendf("\n"); |
+ playbackOut.writeText(pbLine.c_str()); |
+ recordOut.writeText(recLine.c_str()); |
+ } |
+ SkDebugf("Outputed files: %s %s\n", pbTitle, recTitle); |
+ |
+ for (int i = 0; i < kNumBenchmarks; ++i) { |
+ SkDELETE(histograms[i]); |
+ } |
+ |
+ return 0; |
+} |
+ |
+int main(int argc, char** argv) { |
+ return tool_main(argc, argv); |
+} |
+ |