Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 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)); |
| 25 | |
| 26 | void benchmark(llvm::TimerGroup &Group, llvm::StringRef Name, |
| 27 | llvm::StringRef JSONText) { |
| 28 | llvm::Timer BaseLine((Name + ": Loop").str(), Group); |
| 29 | BaseLine.startTimer(); |
| 30 | char C = 0; |
| 31 | for (llvm::StringRef::iterator I = JSONText.begin(), |
| 32 | E = JSONText.end(); |
| 33 | I != E; ++I) { C += *I; } |
| 34 | BaseLine.stopTimer(); |
| 35 | volatile char DontOptimizeOut = C; (void)DontOptimizeOut; |
| 36 | |
| 37 | llvm::Timer Parsing((Name + ": Parsing").str(), Group); |
| 38 | Parsing.startTimer(); |
| 39 | llvm::JSONParser Parser(JSONText); |
| 40 | if (!Parser.validate()) { |
| 41 | llvm::errs() << "Parsing error in JSON parser benchmark.\n"; |
| 42 | exit(1); |
| 43 | } |
| 44 | Parsing.stopTimer(); |
| 45 | } |
| 46 | |
| 47 | std::string createJSONText(int N, int ValueSize) { |
| 48 | std::string JSONText; |
| 49 | llvm::raw_string_ostream Stream(JSONText); |
| 50 | Stream << "[\n"; |
| 51 | for (int I = 0; I < N; ++I) { |
| 52 | Stream << " {\n" |
| 53 | << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n" |
| 54 | << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n" |
| 55 | << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n" |
| 56 | << " }"; |
| 57 | if (I + 1 < N) Stream << ","; |
| 58 | Stream << "\n"; |
| 59 | } |
| 60 | Stream << "]\n"; |
| 61 | Stream.flush(); |
| 62 | return JSONText; |
| 63 | } |
| 64 | |
| 65 | int main(int argc, char **argv) { |
| 66 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 67 | llvm::TimerGroup Group("JSON parser benchmark"); |
| 68 | if (Verify) { |
| 69 | benchmark(Group, "Fast", createJSONText(1000, 500)); |
| 70 | } else { |
| 71 | benchmark(Group, "Small Values", createJSONText(1000000, 5)); |
| 72 | benchmark(Group, "Medium Values", createJSONText(1000000, 500)); |
| 73 | benchmark(Group, "Large Values", createJSONText(10000, 50000)); |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |