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 | |
Manuel Klimek | 2c777c8 | 2011-12-20 10:34:29 +0000 | [diff] [blame] | 26 | static llvm::cl::opt<unsigned> |
Manuel Klimek | d21428a | 2011-12-19 09:32:05 +0000 | [diff] [blame] | 27 | MemoryLimitMB("memory-limit", llvm::cl::desc( |
| 28 | "Do not use more megabytes of memory"), |
| 29 | llvm::cl::init(1000)); |
| 30 | |
Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 31 | void 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(); |
Manuel Klimek | 84cbb6f | 2011-12-21 18:16:39 +0000 | [diff] [blame] | 44 | llvm::SourceMgr SM; |
| 45 | llvm::JSONParser Parser(JSONText, &SM); |
Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 46 | if (!Parser.validate()) { |
| 47 | llvm::errs() << "Parsing error in JSON parser benchmark.\n"; |
| 48 | exit(1); |
| 49 | } |
| 50 | Parsing.stopTimer(); |
| 51 | } |
| 52 | |
Manuel Klimek | 5b25cff | 2011-12-19 09:56:35 +0000 | [diff] [blame] | 53 | std::string createJSONText(size_t MemoryMB, unsigned ValueSize) { |
Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 54 | std::string JSONText; |
| 55 | llvm::raw_string_ostream Stream(JSONText); |
| 56 | Stream << "[\n"; |
Manuel Klimek | 5b25cff | 2011-12-19 09:56:35 +0000 | [diff] [blame] | 57 | size_t MemoryBytes = MemoryMB * 1024 * 1024; |
Manuel Klimek | d21428a | 2011-12-19 09:32:05 +0000 | [diff] [blame] | 58 | while (JSONText.size() < MemoryBytes) { |
Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 59 | Stream << " {\n" |
| 60 | << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n" |
| 61 | << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n" |
| 62 | << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n" |
| 63 | << " }"; |
Manuel Klimek | d21428a | 2011-12-19 09:32:05 +0000 | [diff] [blame] | 64 | Stream.flush(); |
| 65 | if (JSONText.size() < MemoryBytes) Stream << ","; |
Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 66 | Stream << "\n"; |
| 67 | } |
| 68 | Stream << "]\n"; |
| 69 | Stream.flush(); |
| 70 | return JSONText; |
| 71 | } |
| 72 | |
| 73 | int main(int argc, char **argv) { |
| 74 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 75 | llvm::TimerGroup Group("JSON parser benchmark"); |
| 76 | if (Verify) { |
Manuel Klimek | d21428a | 2011-12-19 09:32:05 +0000 | [diff] [blame] | 77 | benchmark(Group, "Fast", createJSONText(10, 500)); |
Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 78 | } else { |
Manuel Klimek | d21428a | 2011-12-19 09:32:05 +0000 | [diff] [blame] | 79 | benchmark(Group, "Small Values", createJSONText(MemoryLimitMB, 5)); |
| 80 | benchmark(Group, "Medium Values", createJSONText(MemoryLimitMB, 500)); |
| 81 | benchmark(Group, "Large Values", createJSONText(MemoryLimitMB, 50000)); |
Manuel Klimek | 76f1301 | 2011-12-16 13:09:10 +0000 | [diff] [blame] | 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |