From 76f13017fc67b35f10b61e05c13f3643b714fccf Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Fri, 16 Dec 2011 13:09:10 +0000 Subject: Adds a JSON parser and a benchmark (json-bench) to catch performance regressions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@146735 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/json-bench/CMakeLists.txt | 5 +++ utils/json-bench/JSONBench.cpp | 77 +++++++++++++++++++++++++++++++++++++++++ utils/json-bench/Makefile | 21 +++++++++++ 3 files changed, 103 insertions(+) create mode 100644 utils/json-bench/CMakeLists.txt create mode 100644 utils/json-bench/JSONBench.cpp create mode 100644 utils/json-bench/Makefile (limited to 'utils/json-bench') diff --git a/utils/json-bench/CMakeLists.txt b/utils/json-bench/CMakeLists.txt new file mode 100644 index 0000000000..03ac51ce64 --- /dev/null +++ b/utils/json-bench/CMakeLists.txt @@ -0,0 +1,5 @@ +add_llvm_utility(json-bench + JSONBench.cpp + ) + +target_link_libraries(json-bench LLVMSupport) diff --git a/utils/json-bench/JSONBench.cpp b/utils/json-bench/JSONBench.cpp new file mode 100644 index 0000000000..b9f4af63d0 --- /dev/null +++ b/utils/json-bench/JSONBench.cpp @@ -0,0 +1,77 @@ +//===- JSONBench - Benchmark the JSONParser implementation ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program executes the JSONParser on differntly sized JSON texts and +// outputs the run time. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/Twine.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/JSONParser.h" +#include "llvm/Support/Timer.h" +#include "llvm/Support/raw_ostream.h" + +static llvm::cl::opt +Verify("verify", llvm::cl::desc( + "Run a quick verification useful for regression testing"), + llvm::cl::init(false)); + +void benchmark(llvm::TimerGroup &Group, llvm::StringRef Name, + llvm::StringRef JSONText) { + llvm::Timer BaseLine((Name + ": Loop").str(), Group); + BaseLine.startTimer(); + char C = 0; + for (llvm::StringRef::iterator I = JSONText.begin(), + E = JSONText.end(); + I != E; ++I) { C += *I; } + BaseLine.stopTimer(); + volatile char DontOptimizeOut = C; (void)DontOptimizeOut; + + llvm::Timer Parsing((Name + ": Parsing").str(), Group); + Parsing.startTimer(); + llvm::JSONParser Parser(JSONText); + if (!Parser.validate()) { + llvm::errs() << "Parsing error in JSON parser benchmark.\n"; + exit(1); + } + Parsing.stopTimer(); +} + +std::string createJSONText(int N, int ValueSize) { + std::string JSONText; + llvm::raw_string_ostream Stream(JSONText); + Stream << "[\n"; + for (int I = 0; I < N; ++I) { + Stream << " {\n" + << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n" + << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n" + << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n" + << " }"; + if (I + 1 < N) Stream << ","; + Stream << "\n"; + } + Stream << "]\n"; + Stream.flush(); + return JSONText; +} + +int main(int argc, char **argv) { + llvm::cl::ParseCommandLineOptions(argc, argv); + llvm::TimerGroup Group("JSON parser benchmark"); + if (Verify) { + benchmark(Group, "Fast", createJSONText(1000, 500)); + } else { + benchmark(Group, "Small Values", createJSONText(1000000, 5)); + benchmark(Group, "Medium Values", createJSONText(1000000, 500)); + benchmark(Group, "Large Values", createJSONText(10000, 50000)); + } + return 0; +} + diff --git a/utils/json-bench/Makefile b/utils/json-bench/Makefile new file mode 100644 index 0000000000..6651626f68 --- /dev/null +++ b/utils/json-bench/Makefile @@ -0,0 +1,21 @@ +##===- utils/FileCheck/Makefile ----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../.. +TOOLNAME = json-bench +USEDLIBS = LLVMSupport.a + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +# Don't install this utility +NO_INSTALL = 1 + +include $(LEVEL)/Makefile.common + -- cgit v1.2.3