From 37f182540d67f239b7e29274b92be6e07351a5f3 Mon Sep 17 00:00:00 2001 From: Rajat Goel Date: Fri, 13 Jul 2012 11:00:51 -0700 Subject: [PATCH] A sample diff thats ports FormatTests to benchmarks Summary: Is there any better solution? Maybe a generic flag in Benchmarks to say please don't run benchmarks? Test Plan: (not in folly) Reviewed By: andrei.alexandrescu@fb.com FB internal diff: D503619 --- folly/test/FormatBenchmark.cpp | 149 +++++++++++++++++++++++++++++++++ folly/test/FormatTest.cpp | 124 +-------------------------- 2 files changed, 150 insertions(+), 123 deletions(-) create mode 100644 folly/test/FormatBenchmark.cpp diff --git a/folly/test/FormatBenchmark.cpp b/folly/test/FormatBenchmark.cpp new file mode 100644 index 00000000..6d4b76a4 --- /dev/null +++ b/folly/test/FormatBenchmark.cpp @@ -0,0 +1,149 @@ +/* + * Copyright 2012 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "folly/Format.h" + +#include + +#include "folly/FBVector.h" +#include "folly/Benchmark.h" +#include "folly/dynamic.h" +#include "folly/json.h" + +using namespace folly; + +namespace { + +char bigBuf[300]; + +} // namespace + +BENCHMARK(octal_sprintf, iters) { + while (iters--) { + sprintf(bigBuf, "%o", static_cast(iters)); + } +} + +BENCHMARK_RELATIVE(octal_uintToOctal, iters) { + while (iters--) { + detail::uintToOctal(bigBuf, detail::kMaxOctalLength, + static_cast(iters)); + } +} + +BENCHMARK_DRAW_LINE() + +BENCHMARK(hex_sprintf, iters) { + while (iters--) { + sprintf(bigBuf, "%x", static_cast(iters)); + } +} + +BENCHMARK_RELATIVE(hex_uintToHex, iters) { + while (iters--) { + detail::uintToHexLower(bigBuf, detail::kMaxHexLength, + static_cast(iters)); + } +} + +BENCHMARK_DRAW_LINE() + +BENCHMARK(intAppend_sprintf) { + fbstring out; + for (int i = -1000; i < 1000; i++) { + sprintf(bigBuf, "%d", i); + out.append(bigBuf); + } +} + +BENCHMARK_RELATIVE(intAppend_to) { + fbstring out; + for (int i = -1000; i < 1000; i++) { + toAppend(i, &out); + } +} + +BENCHMARK_RELATIVE(intAppend_format) { + fbstring out; + for (int i = -1000; i < 1000; i++) { + format(&out, "{}", i); + } +} + +BENCHMARK_DRAW_LINE() + +BENCHMARK(bigFormat_sprintf, iters) { + while (iters--) { + for (int i = -100; i < 100; i++) { + sprintf(bigBuf, + "%d %d %d %d %d" + "%d %d %d %d %d" + "%d %d %d %d %d" + "%d %d %d %d %d", + i, i+1, i+2, i+3, i+4, + i+5, i+6, i+7, i+8, i+9, + i+10, i+11, i+12, i+13, i+14, + i+15, i+16, i+17, i+18, i+19); + } + } +} + +BENCHMARK_RELATIVE(bigFormat_format, iters) { + char* p; + auto writeToBuf = [&p] (StringPiece sp) mutable { + memcpy(p, sp.data(), sp.size()); + p += sp.size(); + }; + + while (iters--) { + for (int i = -100; i < 100; i++) { + p = bigBuf; + format("{} {} {} {} {}" + "{} {} {} {} {}" + "{} {} {} {} {}" + "{} {} {} {} {}", + i, i+1, i+2, i+3, i+4, + i+5, i+6, i+7, i+8, i+9, + i+10, i+11, i+12, i+13, i+14, + i+15, i+16, i+17, i+18, i+19)(writeToBuf); + } + } +} + +// Benchmark results on my dev server (dual-CPU Xeon L5520 @ 2.7GHz) +// +// ============================================================================ +// folly/test/FormatTest.cpp relative ns/iter iters/s +// ============================================================================ +// octal_sprintf 100.57 9.94M +// octal_uintToOctal 2599.47% 3.87 258.46M +// ---------------------------------------------------------------------------- +// hex_sprintf 100.13 9.99M +// hex_uintToHex 3331.75% 3.01 332.73M +// ---------------------------------------------------------------------------- +// intAppend_sprintf 406.07K 2.46K +// intAppend_to 166.03% 244.58K 4.09K +// intAppend_format 147.57% 275.17K 3.63K +// ---------------------------------------------------------------------------- +// bigFormat_sprintf 255.40K 3.92K +// bigFormat_format 102.18% 249.94K 4.00K +// ============================================================================ + +int main(int argc, char *argv[]) { + google::ParseCommandLineFlags(&argc, &argv, true); + runBenchmarks(); + return 0; +} diff --git a/folly/test/FormatTest.cpp b/folly/test/FormatTest.cpp index a1ca6a29..ea66504f 100644 --- a/folly/test/FormatTest.cpp +++ b/folly/test/FormatTest.cpp @@ -20,7 +20,6 @@ #include #include "folly/FBVector.h" -#include "folly/Benchmark.h" #include "folly/dynamic.h" #include "folly/json.h" @@ -281,130 +280,9 @@ TEST(Format, Custom) { EXPECT_EQ("XX", fstr("{:X>23}", kv)); } -namespace { - -char bigBuf[300]; - -} // namespace - -BENCHMARK(octal_sprintf, iters) { - while (iters--) { - sprintf(bigBuf, "%o", static_cast(iters)); - } -} - -BENCHMARK_RELATIVE(octal_uintToOctal, iters) { - while (iters--) { - detail::uintToOctal(bigBuf, detail::kMaxOctalLength, - static_cast(iters)); - } -} - -BENCHMARK_DRAW_LINE() - -BENCHMARK(hex_sprintf, iters) { - while (iters--) { - sprintf(bigBuf, "%x", static_cast(iters)); - } -} - -BENCHMARK_RELATIVE(hex_uintToHex, iters) { - while (iters--) { - detail::uintToHexLower(bigBuf, detail::kMaxHexLength, - static_cast(iters)); - } -} - -BENCHMARK_DRAW_LINE() - -BENCHMARK(intAppend_sprintf) { - fbstring out; - for (int i = -1000; i < 1000; i++) { - sprintf(bigBuf, "%d", i); - out.append(bigBuf); - } -} - -BENCHMARK_RELATIVE(intAppend_to) { - fbstring out; - for (int i = -1000; i < 1000; i++) { - toAppend(i, &out); - } -} - -BENCHMARK_RELATIVE(intAppend_format) { - fbstring out; - for (int i = -1000; i < 1000; i++) { - format(&out, "{}", i); - } -} - -BENCHMARK_DRAW_LINE() - -BENCHMARK(bigFormat_sprintf, iters) { - while (iters--) { - for (int i = -100; i < 100; i++) { - sprintf(bigBuf, - "%d %d %d %d %d" - "%d %d %d %d %d" - "%d %d %d %d %d" - "%d %d %d %d %d", - i, i+1, i+2, i+3, i+4, - i+5, i+6, i+7, i+8, i+9, - i+10, i+11, i+12, i+13, i+14, - i+15, i+16, i+17, i+18, i+19); - } - } -} - -BENCHMARK_RELATIVE(bigFormat_format, iters) { - char* p; - auto writeToBuf = [&p] (StringPiece sp) mutable { - memcpy(p, sp.data(), sp.size()); - p += sp.size(); - }; - - while (iters--) { - for (int i = -100; i < 100; i++) { - p = bigBuf; - format("{} {} {} {} {}" - "{} {} {} {} {}" - "{} {} {} {} {}" - "{} {} {} {} {}", - i, i+1, i+2, i+3, i+4, - i+5, i+6, i+7, i+8, i+9, - i+10, i+11, i+12, i+13, i+14, - i+15, i+16, i+17, i+18, i+19)(writeToBuf); - } - } -} - -// Benchmark results on my dev server (dual-CPU Xeon L5520 @ 2.7GHz) -// -// ============================================================================ -// folly/test/FormatTest.cpp relative ns/iter iters/s -// ============================================================================ -// octal_sprintf 100.57 9.94M -// octal_uintToOctal 2599.47% 3.87 258.46M -// ---------------------------------------------------------------------------- -// hex_sprintf 100.13 9.99M -// hex_uintToHex 3331.75% 3.01 332.73M -// ---------------------------------------------------------------------------- -// intAppend_sprintf 406.07K 2.46K -// intAppend_to 166.03% 244.58K 4.09K -// intAppend_format 147.57% 275.17K 3.63K -// ---------------------------------------------------------------------------- -// bigFormat_sprintf 255.40K 3.92K -// bigFormat_format 102.18% 249.94K 4.00K -// ============================================================================ - int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true); - auto ret = RUN_ALL_TESTS(); - if (!ret) { - runBenchmarksOnFlag(); - } - return ret; + return RUN_ALL_TESTS(); } -- 2.34.1