2011-12-16 21:09:10 +08:00
|
|
|
//===- 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<bool>
|
|
|
|
Verify("verify", llvm::cl::desc(
|
|
|
|
"Run a quick verification useful for regression testing"),
|
|
|
|
llvm::cl::init(false));
|
|
|
|
|
2011-12-20 18:34:29 +08:00
|
|
|
static llvm::cl::opt<unsigned>
|
2011-12-19 17:32:05 +08:00
|
|
|
MemoryLimitMB("memory-limit", llvm::cl::desc(
|
|
|
|
"Do not use more megabytes of memory"),
|
|
|
|
llvm::cl::init(1000));
|
|
|
|
|
2011-12-16 21:09:10 +08:00
|
|
|
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();
|
2011-12-22 02:16:39 +08:00
|
|
|
llvm::SourceMgr SM;
|
|
|
|
llvm::JSONParser Parser(JSONText, &SM);
|
2011-12-16 21:09:10 +08:00
|
|
|
if (!Parser.validate()) {
|
|
|
|
llvm::errs() << "Parsing error in JSON parser benchmark.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Parsing.stopTimer();
|
|
|
|
}
|
|
|
|
|
2011-12-19 17:56:35 +08:00
|
|
|
std::string createJSONText(size_t MemoryMB, unsigned ValueSize) {
|
2011-12-16 21:09:10 +08:00
|
|
|
std::string JSONText;
|
|
|
|
llvm::raw_string_ostream Stream(JSONText);
|
|
|
|
Stream << "[\n";
|
2011-12-19 17:56:35 +08:00
|
|
|
size_t MemoryBytes = MemoryMB * 1024 * 1024;
|
2011-12-19 17:32:05 +08:00
|
|
|
while (JSONText.size() < MemoryBytes) {
|
2011-12-16 21:09:10 +08:00
|
|
|
Stream << " {\n"
|
|
|
|
<< " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
|
|
|
|
<< " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
|
|
|
|
<< " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
|
|
|
|
<< " }";
|
2011-12-19 17:32:05 +08:00
|
|
|
Stream.flush();
|
|
|
|
if (JSONText.size() < MemoryBytes) Stream << ",";
|
2011-12-16 21:09:10 +08:00
|
|
|
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) {
|
2011-12-19 17:32:05 +08:00
|
|
|
benchmark(Group, "Fast", createJSONText(10, 500));
|
2011-12-16 21:09:10 +08:00
|
|
|
} else {
|
2011-12-19 17:32:05 +08:00
|
|
|
benchmark(Group, "Small Values", createJSONText(MemoryLimitMB, 5));
|
|
|
|
benchmark(Group, "Medium Values", createJSONText(MemoryLimitMB, 500));
|
|
|
|
benchmark(Group, "Large Values", createJSONText(MemoryLimitMB, 50000));
|
2011-12-16 21:09:10 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|