blob: 810ebfcdefcda87ac1c9ca46cda54239e155fbe3 [file] [log] [blame]
Manuel Klimek76f13012011-12-16 13:09:10 +00001//===- JSONBench - Benchmark the JSONParser implementation ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program executes the JSONParser on differntly sized JSON texts and
11// outputs the run time.
12//
13//===----------------------------------------------------------------------===//
14
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"
20
21static llvm::cl::opt<bool>
22Verify("verify", llvm::cl::desc(
23 "Run a quick verification useful for regression testing"),
24 llvm::cl::init(false));
25
Manuel Klimekd21428a2011-12-19 09:32:05 +000026static llvm::cl::opt<unsigned>
27MemoryLimitMB("memory-limit", llvm::cl::desc(
28 "Do not use more megabytes of memory"),
29 llvm::cl::init(1000));
30
Manuel Klimek76f13012011-12-16 13:09:10 +000031void benchmark(llvm::TimerGroup &Group, llvm::StringRef Name,
32 llvm::StringRef JSONText) {
33 llvm::Timer BaseLine((Name + ": Loop").str(), Group);
34 BaseLine.startTimer();
35 char C = 0;
36 for (llvm::StringRef::iterator I = JSONText.begin(),
37 E = JSONText.end();
38 I != E; ++I) { C += *I; }
39 BaseLine.stopTimer();
40 volatile char DontOptimizeOut = C; (void)DontOptimizeOut;
41
42 llvm::Timer Parsing((Name + ": Parsing").str(), Group);
43 Parsing.startTimer();
44 llvm::JSONParser Parser(JSONText);
45 if (!Parser.validate()) {
46 llvm::errs() << "Parsing error in JSON parser benchmark.\n";
47 exit(1);
48 }
49 Parsing.stopTimer();
50}
51
Manuel Klimekd21428a2011-12-19 09:32:05 +000052std::string createJSONText(unsigned MemoryMB, unsigned ValueSize) {
Manuel Klimek76f13012011-12-16 13:09:10 +000053 std::string JSONText;
54 llvm::raw_string_ostream Stream(JSONText);
55 Stream << "[\n";
Manuel Klimekd21428a2011-12-19 09:32:05 +000056 unsigned MemoryBytes = MemoryMB * 1024 * 1024;
57 while (JSONText.size() < MemoryBytes) {
Manuel Klimek76f13012011-12-16 13:09:10 +000058 Stream << " {\n"
59 << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
60 << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
61 << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
62 << " }";
Manuel Klimekd21428a2011-12-19 09:32:05 +000063 Stream.flush();
64 if (JSONText.size() < MemoryBytes) Stream << ",";
Manuel Klimek76f13012011-12-16 13:09:10 +000065 Stream << "\n";
66 }
67 Stream << "]\n";
68 Stream.flush();
69 return JSONText;
70}
71
72int main(int argc, char **argv) {
73 llvm::cl::ParseCommandLineOptions(argc, argv);
74 llvm::TimerGroup Group("JSON parser benchmark");
75 if (Verify) {
Manuel Klimekd21428a2011-12-19 09:32:05 +000076 benchmark(Group, "Fast", createJSONText(10, 500));
Manuel Klimek76f13012011-12-16 13:09:10 +000077 } else {
Manuel Klimekd21428a2011-12-19 09:32:05 +000078 benchmark(Group, "Small Values", createJSONText(MemoryLimitMB, 5));
79 benchmark(Group, "Medium Values", createJSONText(MemoryLimitMB, 500));
80 benchmark(Group, "Large Values", createJSONText(MemoryLimitMB, 50000));
Manuel Klimek76f13012011-12-16 13:09:10 +000081 }
82 return 0;
83}
84