1 //===- YAMLBench - Benchmark the YAMLParser implementation ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program executes the YAMLParser on differently sized YAML texts and
11 // outputs the run time.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/Timer.h"
22 #include "llvm/Support/YAMLParser.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <system_error>
30 , cl::desc("Print the tokenization of the file.")
35 DumpCanonical( "canonical"
36 , cl::desc("Print the canonical YAML for this file.")
40 static cl::opt<std::string>
41 Input(cl::Positional, cl::desc("<input>"));
46 "Run a quick verification useful for regression testing")
50 static cl::opt<unsigned>
51 MemoryLimitMB("memory-limit", cl::desc(
52 "Do not use more megabytes of memory"),
57 indent(unsigned d) : distance(d) {}
60 static raw_ostream &operator <<(raw_ostream &os, const indent &in) {
61 for (unsigned i = 0; i < in.distance; ++i)
66 /// \brief Pretty print a tag by replacing tag:yaml.org,2002: with !!.
67 static std::string prettyTag(yaml::Node *N) {
68 std::string Tag = N->getVerbatimTag();
69 if (StringRef(Tag).startswith("tag:yaml.org,2002:")) {
70 std::string Ret = "!!";
71 Ret += StringRef(Tag).substr(18);
72 return std::move(Ret);
74 std::string Ret = "!<";
80 static void dumpNode( yaml::Node *n
82 , bool SuppressFirstIndent = false) {
85 if (!SuppressFirstIndent)
86 outs() << indent(Indent);
87 StringRef Anchor = n->getAnchor();
89 outs() << "&" << Anchor << " ";
90 if (yaml::ScalarNode *sn = dyn_cast<yaml::ScalarNode>(n)) {
91 SmallString<32> Storage;
92 StringRef Val = sn->getValue(Storage);
93 outs() << prettyTag(n) << " \"" << yaml::escape(Val) << "\"";
94 } else if (yaml::SequenceNode *sn = dyn_cast<yaml::SequenceNode>(n)) {
95 outs() << prettyTag(n) << " [\n";
97 for (yaml::SequenceNode::iterator i = sn->begin(), e = sn->end();
103 outs() << indent(Indent) << "]";
104 } else if (yaml::MappingNode *mn = dyn_cast<yaml::MappingNode>(n)) {
105 outs() << prettyTag(n) << " {\n";
107 for (yaml::MappingNode::iterator i = mn->begin(), e = mn->end();
109 outs() << indent(Indent) << "? ";
110 dumpNode(i->getKey(), Indent, true);
112 outs() << indent(Indent) << ": ";
113 dumpNode(i->getValue(), Indent, true);
117 outs() << indent(Indent) << "}";
118 } else if (yaml::AliasNode *an = dyn_cast<yaml::AliasNode>(n)){
119 outs() << "*" << an->getName();
120 } else if (dyn_cast<yaml::NullNode>(n)) {
121 outs() << prettyTag(n) << " null";
125 static void dumpStream(yaml::Stream &stream) {
126 for (yaml::document_iterator di = stream.begin(), de = stream.end(); di != de;
128 outs() << "%YAML 1.2\n"
130 yaml::Node *n = di->getRoot();
139 static void benchmark( llvm::TimerGroup &Group
140 , llvm::StringRef Name
141 , llvm::StringRef JSONText) {
142 llvm::Timer BaseLine((Name + ": Loop").str(), Group);
143 BaseLine.startTimer();
145 for (llvm::StringRef::iterator I = JSONText.begin(),
147 I != E; ++I) { C += *I; }
148 BaseLine.stopTimer();
149 volatile char DontOptimizeOut = C; (void)DontOptimizeOut;
151 llvm::Timer Tokenizing((Name + ": Tokenizing").str(), Group);
152 Tokenizing.startTimer();
154 yaml::scanTokens(JSONText);
156 Tokenizing.stopTimer();
158 llvm::Timer Parsing((Name + ": Parsing").str(), Group);
159 Parsing.startTimer();
162 llvm::yaml::Stream stream(JSONText, SM);
168 static std::string createJSONText(size_t MemoryMB, unsigned ValueSize) {
169 std::string JSONText;
170 llvm::raw_string_ostream Stream(JSONText);
172 size_t MemoryBytes = MemoryMB * 1024 * 1024;
173 while (JSONText.size() < MemoryBytes) {
175 << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
176 << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
177 << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
180 if (JSONText.size() < MemoryBytes) Stream << ",";
188 int main(int argc, char **argv) {
189 llvm::cl::ParseCommandLineOptions(argc, argv);
190 if (Input.getNumOccurrences()) {
191 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
192 MemoryBuffer::getFileOrSTDIN(Input);
195 MemoryBuffer &Buf = *BufOrErr.get();
199 yaml::dumpTokens(Buf.getBuffer(), outs());
203 yaml::Stream stream(Buf.getBuffer(), sm);
209 llvm::TimerGroup Group("YAML parser benchmark");
210 benchmark(Group, "Fast", createJSONText(10, 500));
211 } else if (!DumpCanonical && !DumpTokens) {
212 llvm::TimerGroup Group("YAML parser benchmark");
213 benchmark(Group, "Small Values", createJSONText(MemoryLimitMB, 5));
214 benchmark(Group, "Medium Values", createJSONText(MemoryLimitMB, 500));
215 benchmark(Group, "Large Values", createJSONText(MemoryLimitMB, 50000));