1 //===- JSONBench - Benchmark the JSONParser implementation ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program executes the JSONParser on differntly sized JSON texts and
11 // outputs the run time.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/JSONParser.h"
18 #include "llvm/Support/Timer.h"
19 #include "llvm/Support/raw_ostream.h"
21 static llvm::cl::opt<bool>
22 Verify("verify", llvm::cl::desc(
23 "Run a quick verification useful for regression testing"),
24 llvm::cl::init(false));
26 static llvm::cl::opt<unsigned>
27 MemoryLimitMB("memory-limit", llvm::cl::desc(
28 "Do not use more megabytes of memory"),
29 llvm::cl::init(1000));
31 void benchmark(llvm::TimerGroup &Group, llvm::StringRef Name,
32 llvm::StringRef JSONText) {
33 llvm::Timer BaseLine((Name + ": Loop").str(), Group);
34 BaseLine.startTimer();
36 for (llvm::StringRef::iterator I = JSONText.begin(),
38 I != E; ++I) { C += *I; }
40 volatile char DontOptimizeOut = C; (void)DontOptimizeOut;
42 llvm::Timer Parsing((Name + ": Parsing").str(), Group);
44 llvm::JSONParser Parser(JSONText);
45 if (!Parser.validate()) {
46 llvm::errs() << "Parsing error in JSON parser benchmark.\n";
52 std::string createJSONText(size_t MemoryMB, unsigned ValueSize) {
54 llvm::raw_string_ostream Stream(JSONText);
56 size_t MemoryBytes = MemoryMB * 1024 * 1024;
57 while (JSONText.size() < MemoryBytes) {
59 << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
60 << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
61 << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
64 if (JSONText.size() < MemoryBytes) Stream << ",";
72 int main(int argc, char **argv) {
73 llvm::cl::ParseCommandLineOptions(argc, argv);
74 llvm::TimerGroup Group("JSON parser benchmark");
76 benchmark(Group, "Fast", createJSONText(10, 500));
78 benchmark(Group, "Small Values", createJSONText(MemoryLimitMB, 5));
79 benchmark(Group, "Medium Values", createJSONText(MemoryLimitMB, 500));
80 benchmark(Group, "Large Values", createJSONText(MemoryLimitMB, 50000));