2012-04-04 07:09:22 +08:00
|
|
|
//===- YAMLBench - Benchmark the YAMLParser implementation ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-04-06 04:28:13 +08:00
|
|
|
// This program executes the YAMLParser on differently sized YAML texts and
|
2012-04-04 07:09:22 +08:00
|
|
|
// outputs the run time.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
2015-05-08 02:08:46 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2012-04-04 07:09:22 +08:00
|
|
|
#include "llvm/Support/YAMLParser.h"
|
2012-12-04 18:37:14 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2012-04-04 07:09:22 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DumpTokens( "tokens"
|
|
|
|
, cl::desc("Print the tokenization of the file.")
|
|
|
|
, cl::init(false)
|
|
|
|
);
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DumpCanonical( "canonical"
|
|
|
|
, cl::desc("Print the canonical YAML for this file.")
|
|
|
|
, cl::init(false)
|
|
|
|
);
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
Input(cl::Positional, cl::desc("<input>"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Verify( "verify"
|
|
|
|
, cl::desc(
|
|
|
|
"Run a quick verification useful for regression testing")
|
|
|
|
, cl::init(false)
|
|
|
|
);
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
MemoryLimitMB("memory-limit", cl::desc(
|
|
|
|
"Do not use more megabytes of memory"),
|
|
|
|
cl::init(1000));
|
|
|
|
|
2015-05-08 02:08:46 +08:00
|
|
|
cl::opt<cl::boolOrDefault>
|
|
|
|
UseColor("use-color", cl::desc("Emit colored output (default=autodetect)"),
|
|
|
|
cl::init(cl::BOU_UNSET));
|
|
|
|
|
2012-04-04 07:09:22 +08:00
|
|
|
struct indent {
|
|
|
|
unsigned distance;
|
|
|
|
indent(unsigned d) : distance(d) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
static raw_ostream &operator <<(raw_ostream &os, const indent &in) {
|
|
|
|
for (unsigned i = 0; i < in.distance; ++i)
|
|
|
|
os << " ";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2013-10-19 06:38:04 +08:00
|
|
|
/// \brief Pretty print a tag by replacing tag:yaml.org,2002: with !!.
|
|
|
|
static std::string prettyTag(yaml::Node *N) {
|
|
|
|
std::string Tag = N->getVerbatimTag();
|
|
|
|
if (StringRef(Tag).startswith("tag:yaml.org,2002:")) {
|
|
|
|
std::string Ret = "!!";
|
|
|
|
Ret += StringRef(Tag).substr(18);
|
2015-05-01 23:16:11 +08:00
|
|
|
return Ret;
|
2013-10-19 06:38:04 +08:00
|
|
|
}
|
|
|
|
std::string Ret = "!<";
|
|
|
|
Ret += Tag;
|
|
|
|
Ret += ">";
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2012-04-04 07:09:22 +08:00
|
|
|
static void dumpNode( yaml::Node *n
|
|
|
|
, unsigned Indent = 0
|
|
|
|
, bool SuppressFirstIndent = false) {
|
|
|
|
if (!n)
|
|
|
|
return;
|
|
|
|
if (!SuppressFirstIndent)
|
|
|
|
outs() << indent(Indent);
|
|
|
|
StringRef Anchor = n->getAnchor();
|
|
|
|
if (!Anchor.empty())
|
|
|
|
outs() << "&" << Anchor << " ";
|
|
|
|
if (yaml::ScalarNode *sn = dyn_cast<yaml::ScalarNode>(n)) {
|
|
|
|
SmallString<32> Storage;
|
|
|
|
StringRef Val = sn->getValue(Storage);
|
2013-10-19 06:38:04 +08:00
|
|
|
outs() << prettyTag(n) << " \"" << yaml::escape(Val) << "\"";
|
2015-05-14 07:10:51 +08:00
|
|
|
} else if (yaml::BlockScalarNode *BN = dyn_cast<yaml::BlockScalarNode>(n)) {
|
|
|
|
outs() << prettyTag(n) << " \"" << yaml::escape(BN->getValue()) << "\"";
|
2012-04-04 07:09:22 +08:00
|
|
|
} else if (yaml::SequenceNode *sn = dyn_cast<yaml::SequenceNode>(n)) {
|
2013-10-19 06:38:04 +08:00
|
|
|
outs() << prettyTag(n) << " [\n";
|
2012-04-04 07:09:22 +08:00
|
|
|
++Indent;
|
|
|
|
for (yaml::SequenceNode::iterator i = sn->begin(), e = sn->end();
|
|
|
|
i != e; ++i) {
|
|
|
|
dumpNode(i, Indent);
|
|
|
|
outs() << ",\n";
|
|
|
|
}
|
|
|
|
--Indent;
|
|
|
|
outs() << indent(Indent) << "]";
|
|
|
|
} else if (yaml::MappingNode *mn = dyn_cast<yaml::MappingNode>(n)) {
|
2013-10-19 06:38:04 +08:00
|
|
|
outs() << prettyTag(n) << " {\n";
|
2012-04-04 07:09:22 +08:00
|
|
|
++Indent;
|
|
|
|
for (yaml::MappingNode::iterator i = mn->begin(), e = mn->end();
|
|
|
|
i != e; ++i) {
|
|
|
|
outs() << indent(Indent) << "? ";
|
|
|
|
dumpNode(i->getKey(), Indent, true);
|
|
|
|
outs() << "\n";
|
|
|
|
outs() << indent(Indent) << ": ";
|
|
|
|
dumpNode(i->getValue(), Indent, true);
|
|
|
|
outs() << ",\n";
|
|
|
|
}
|
|
|
|
--Indent;
|
|
|
|
outs() << indent(Indent) << "}";
|
|
|
|
} else if (yaml::AliasNode *an = dyn_cast<yaml::AliasNode>(n)){
|
|
|
|
outs() << "*" << an->getName();
|
2015-04-10 19:24:51 +08:00
|
|
|
} else if (isa<yaml::NullNode>(n)) {
|
2013-10-19 06:38:04 +08:00
|
|
|
outs() << prettyTag(n) << " null";
|
2012-04-04 07:09:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dumpStream(yaml::Stream &stream) {
|
|
|
|
for (yaml::document_iterator di = stream.begin(), de = stream.end(); di != de;
|
|
|
|
++di) {
|
|
|
|
outs() << "%YAML 1.2\n"
|
|
|
|
<< "---\n";
|
|
|
|
yaml::Node *n = di->getRoot();
|
|
|
|
if (n)
|
|
|
|
dumpNode(n);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
outs() << "\n...\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static 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 Tokenizing((Name + ": Tokenizing").str(), Group);
|
|
|
|
Tokenizing.startTimer();
|
|
|
|
{
|
|
|
|
yaml::scanTokens(JSONText);
|
|
|
|
}
|
|
|
|
Tokenizing.stopTimer();
|
|
|
|
|
|
|
|
llvm::Timer Parsing((Name + ": Parsing").str(), Group);
|
|
|
|
Parsing.startTimer();
|
|
|
|
{
|
|
|
|
llvm::SourceMgr SM;
|
|
|
|
llvm::yaml::Stream stream(JSONText, SM);
|
|
|
|
stream.skip();
|
|
|
|
}
|
|
|
|
Parsing.stopTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string createJSONText(size_t MemoryMB, unsigned ValueSize) {
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string JSONText;
|
|
|
|
llvm::raw_string_ostream Stream(JSONText);
|
|
|
|
Stream << "[\n";
|
2012-04-04 07:09:22 +08:00
|
|
|
size_t MemoryBytes = MemoryMB * 1024 * 1024;
|
2014-06-27 06:52:05 +08:00
|
|
|
while (JSONText.size() < MemoryBytes) {
|
|
|
|
Stream << " {\n"
|
|
|
|
<< " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
|
|
|
|
<< " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
|
|
|
|
<< " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
|
|
|
|
<< " }";
|
|
|
|
Stream.flush();
|
|
|
|
if (JSONText.size() < MemoryBytes) Stream << ",";
|
|
|
|
Stream << "\n";
|
2012-04-04 07:09:22 +08:00
|
|
|
}
|
2014-06-27 06:52:05 +08:00
|
|
|
Stream << "]\n";
|
|
|
|
Stream.flush();
|
|
|
|
return JSONText;
|
2012-04-04 07:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv);
|
2015-05-08 02:08:46 +08:00
|
|
|
bool ShowColors = UseColor == cl::BOU_UNSET
|
|
|
|
? sys::Process::StandardOutHasColors()
|
|
|
|
: UseColor == cl::BOU_TRUE;
|
2012-04-04 07:09:22 +08:00
|
|
|
if (Input.getNumOccurrences()) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Input);
|
|
|
|
if (!BufOrErr)
|
2012-04-04 07:09:22 +08:00
|
|
|
return 1;
|
2014-08-01 22:31:55 +08:00
|
|
|
MemoryBuffer &Buf = *BufOrErr.get();
|
2012-04-04 07:09:22 +08:00
|
|
|
|
|
|
|
llvm::SourceMgr sm;
|
|
|
|
if (DumpTokens) {
|
2014-08-01 22:31:55 +08:00
|
|
|
yaml::dumpTokens(Buf.getBuffer(), outs());
|
2012-04-04 07:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DumpCanonical) {
|
2015-05-08 02:08:46 +08:00
|
|
|
yaml::Stream stream(Buf.getBuffer(), sm, ShowColors);
|
2012-04-04 07:09:22 +08:00
|
|
|
dumpStream(stream);
|
2015-05-08 02:08:46 +08:00
|
|
|
if (stream.failed())
|
|
|
|
return 1;
|
2012-04-04 07:09:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Verify) {
|
|
|
|
llvm::TimerGroup Group("YAML parser benchmark");
|
|
|
|
benchmark(Group, "Fast", createJSONText(10, 500));
|
|
|
|
} else if (!DumpCanonical && !DumpTokens) {
|
|
|
|
llvm::TimerGroup Group("YAML parser benchmark");
|
|
|
|
benchmark(Group, "Small Values", createJSONText(MemoryLimitMB, 5));
|
|
|
|
benchmark(Group, "Medium Values", createJSONText(MemoryLimitMB, 500));
|
|
|
|
benchmark(Group, "Large Values", createJSONText(MemoryLimitMB, 50000));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|