From: Chip Turner Date: Thu, 11 Dec 2014 05:00:02 +0000 (-0800) Subject: Improve benchmarking around stringPrintf functions X-Git-Tag: v0.22.0~107 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=6a1addcce6b59b93aad1bed31adce268f3c56ae0;p=folly.git Improve benchmarking around stringPrintf functions Summary: Before optimizing this code, it is better to measure different aspects of it to ensure improvements are worth it and don'tintroduce other regressions. This adds a new benchmark as well as parameterizes an old one. Test Plan: run it Reviewed By: lovro@fb.com Subscribers: lins, anca, folly-diffs@ FB internal diff: D1733760 Tasks: 5735468 Signature: t1:1733760:1418313479:9f572d5a4cf014bd266d91d0f7a75407d1514f65 --- diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 23ee318a..9b4dc8da 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -146,13 +146,36 @@ TEST(StringPrintf, oldStringAppendf) { EXPECT_EQ(string("helloa/b/c/d"), s); } -BENCHMARK(new_stringPrintfSmall, iters) { +// A simple benchmark that tests various output sizes for a simple +// input; the goal is to measure the output buffer resize code cost. +void stringPrintfOutputSize(int iters, int param) { + string buffer; + BENCHMARK_SUSPEND { buffer.resize(param, 'x'); } + for (int64_t i = 0; i < iters; ++i) { - int32_t x = int32_t(i); - int32_t y = int32_t(i + 1); - string s = - stringPrintf("msg msg msg msg msg msg msg msg: %d, %d, %s", - x, y, "hello"); + string s = stringPrintf("msg: %d, %d, %s", 10, 20, buffer.c_str()); + } +} + +// The first few of these tend to fit in the inline buffer, while the +// subsequent ones cross that limit, trigger a second vsnprintf, and +// exercise a different codepath. +BENCHMARK_PARAM(stringPrintfOutputSize, 1) +BENCHMARK_PARAM(stringPrintfOutputSize, 4) +BENCHMARK_PARAM(stringPrintfOutputSize, 16) +BENCHMARK_PARAM(stringPrintfOutputSize, 64) +BENCHMARK_PARAM(stringPrintfOutputSize, 256) +BENCHMARK_PARAM(stringPrintfOutputSize, 1024) + +// Benchmark simple stringAppendf behavior to show a pathology Lovro +// reported (t5735468). +BENCHMARK(stringPrintfAppendfBenchmark, iters) { + for (unsigned int i = 0; i < iters; ++i) { + string s; + BENCHMARK_SUSPEND { s.reserve(300000); } + for (int j = 0; j < 300000; ++j) { + stringAppendf(&s, "%d", 1); + } } }