blob: 3688db6f438b7debfe129929b824ee5efbf56c88 [file] [log] [blame]
Michael J. Spencer93210e82012-04-03 23:09:22 +00001//===- YAMLBench - Benchmark the YAMLParser 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//
David Blaikiea3cc2212014-04-05 20:28:13 +000010// This program executes the YAMLParser on differently sized YAML texts and
Michael J. Spencer93210e82012-04-03 23:09:22 +000011// outputs the run time.
12//
13//===----------------------------------------------------------------------===//
14
15
16#include "llvm/ADT/SmallString.h"
17#include "llvm/Support/Casting.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer93210e82012-04-03 23:09:22 +000020#include "llvm/Support/SourceMgr.h"
Michael J. Spencer93210e82012-04-03 23:09:22 +000021#include "llvm/Support/Timer.h"
Alex Lorenz9e31c0c2015-05-07 18:08:46 +000022#include "llvm/Support/Process.h"
Michael J. Spencer93210e82012-04-03 23:09:22 +000023#include "llvm/Support/YAMLParser.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000024#include "llvm/Support/raw_ostream.h"
Rafael Espindolad5132f92014-06-12 17:38:55 +000025#include <system_error>
Michael J. Spencer93210e82012-04-03 23:09:22 +000026
27using namespace llvm;
28
29static cl::opt<bool>
30 DumpTokens( "tokens"
31 , cl::desc("Print the tokenization of the file.")
32 , cl::init(false)
33 );
34
35static cl::opt<bool>
36 DumpCanonical( "canonical"
37 , cl::desc("Print the canonical YAML for this file.")
38 , cl::init(false)
39 );
40
41static cl::opt<std::string>
42 Input(cl::Positional, cl::desc("<input>"));
43
44static cl::opt<bool>
45 Verify( "verify"
46 , cl::desc(
47 "Run a quick verification useful for regression testing")
48 , cl::init(false)
49 );
50
51static cl::opt<unsigned>
52 MemoryLimitMB("memory-limit", cl::desc(
53 "Do not use more megabytes of memory"),
54 cl::init(1000));
55
Alex Lorenz9e31c0c2015-05-07 18:08:46 +000056cl::opt<cl::boolOrDefault>
57 UseColor("use-color", cl::desc("Emit colored output (default=autodetect)"),
58 cl::init(cl::BOU_UNSET));
59
Michael J. Spencer93210e82012-04-03 23:09:22 +000060struct indent {
61 unsigned distance;
62 indent(unsigned d) : distance(d) {}
63};
64
65static raw_ostream &operator <<(raw_ostream &os, const indent &in) {
66 for (unsigned i = 0; i < in.distance; ++i)
67 os << " ";
68 return os;
69}
70
Adrian Prantl26b584c2018-05-01 15:54:18 +000071/// Pretty print a tag by replacing tag:yaml.org,2002: with !!.
Michael J. Spencer44a4cfb2013-10-18 22:38:04 +000072static std::string prettyTag(yaml::Node *N) {
73 std::string Tag = N->getVerbatimTag();
74 if (StringRef(Tag).startswith("tag:yaml.org,2002:")) {
75 std::string Ret = "!!";
76 Ret += StringRef(Tag).substr(18);
Benjamin Kramerfda9fd42015-05-01 15:16:11 +000077 return Ret;
Michael J. Spencer44a4cfb2013-10-18 22:38:04 +000078 }
79 std::string Ret = "!<";
80 Ret += Tag;
81 Ret += ">";
82 return Ret;
83}
84
Michael J. Spencer93210e82012-04-03 23:09:22 +000085static void dumpNode( yaml::Node *n
86 , unsigned Indent = 0
87 , bool SuppressFirstIndent = false) {
88 if (!n)
89 return;
90 if (!SuppressFirstIndent)
91 outs() << indent(Indent);
92 StringRef Anchor = n->getAnchor();
93 if (!Anchor.empty())
94 outs() << "&" << Anchor << " ";
95 if (yaml::ScalarNode *sn = dyn_cast<yaml::ScalarNode>(n)) {
96 SmallString<32> Storage;
97 StringRef Val = sn->getValue(Storage);
Michael J. Spencer44a4cfb2013-10-18 22:38:04 +000098 outs() << prettyTag(n) << " \"" << yaml::escape(Val) << "\"";
Alex Lorenzb96942f2015-05-13 23:10:51 +000099 } else if (yaml::BlockScalarNode *BN = dyn_cast<yaml::BlockScalarNode>(n)) {
100 outs() << prettyTag(n) << " \"" << yaml::escape(BN->getValue()) << "\"";
Michael J. Spencer93210e82012-04-03 23:09:22 +0000101 } else if (yaml::SequenceNode *sn = dyn_cast<yaml::SequenceNode>(n)) {
Michael J. Spencer44a4cfb2013-10-18 22:38:04 +0000102 outs() << prettyTag(n) << " [\n";
Michael J. Spencer93210e82012-04-03 23:09:22 +0000103 ++Indent;
104 for (yaml::SequenceNode::iterator i = sn->begin(), e = sn->end();
105 i != e; ++i) {
106 dumpNode(i, Indent);
107 outs() << ",\n";
108 }
109 --Indent;
110 outs() << indent(Indent) << "]";
111 } else if (yaml::MappingNode *mn = dyn_cast<yaml::MappingNode>(n)) {
Michael J. Spencer44a4cfb2013-10-18 22:38:04 +0000112 outs() << prettyTag(n) << " {\n";
Michael J. Spencer93210e82012-04-03 23:09:22 +0000113 ++Indent;
114 for (yaml::MappingNode::iterator i = mn->begin(), e = mn->end();
115 i != e; ++i) {
116 outs() << indent(Indent) << "? ";
117 dumpNode(i->getKey(), Indent, true);
118 outs() << "\n";
119 outs() << indent(Indent) << ": ";
120 dumpNode(i->getValue(), Indent, true);
121 outs() << ",\n";
122 }
123 --Indent;
124 outs() << indent(Indent) << "}";
125 } else if (yaml::AliasNode *an = dyn_cast<yaml::AliasNode>(n)){
126 outs() << "*" << an->getName();
Benjamin Kramer0973b7d2015-04-10 11:24:51 +0000127 } else if (isa<yaml::NullNode>(n)) {
Michael J. Spencer44a4cfb2013-10-18 22:38:04 +0000128 outs() << prettyTag(n) << " null";
Michael J. Spencer93210e82012-04-03 23:09:22 +0000129 }
130}
131
132static void dumpStream(yaml::Stream &stream) {
133 for (yaml::document_iterator di = stream.begin(), de = stream.end(); di != de;
134 ++di) {
135 outs() << "%YAML 1.2\n"
136 << "---\n";
137 yaml::Node *n = di->getRoot();
138 if (n)
139 dumpNode(n);
140 else
141 break;
142 outs() << "\n...\n";
143 }
144}
145
Matthias Braun9262f002016-11-18 19:43:18 +0000146static void benchmark(llvm::TimerGroup &Group, llvm::StringRef Name,
147 llvm::StringRef Description, llvm::StringRef JSONText) {
148 llvm::Timer BaseLine((Name + ".loop").str(), (Description + ": Loop").str(),
149 Group);
Michael J. Spencer93210e82012-04-03 23:09:22 +0000150 BaseLine.startTimer();
151 char C = 0;
152 for (llvm::StringRef::iterator I = JSONText.begin(),
153 E = JSONText.end();
154 I != E; ++I) { C += *I; }
155 BaseLine.stopTimer();
156 volatile char DontOptimizeOut = C; (void)DontOptimizeOut;
157
Matthias Braun9262f002016-11-18 19:43:18 +0000158 llvm::Timer Tokenizing((Name + ".tokenizing").str(),
159 (Description + ": Tokenizing").str(), Group);
Michael J. Spencer93210e82012-04-03 23:09:22 +0000160 Tokenizing.startTimer();
161 {
162 yaml::scanTokens(JSONText);
163 }
164 Tokenizing.stopTimer();
165
Matthias Braun9262f002016-11-18 19:43:18 +0000166 llvm::Timer Parsing((Name + ".parsing").str(),
167 (Description + ": Parsing").str(), Group);
Michael J. Spencer93210e82012-04-03 23:09:22 +0000168 Parsing.startTimer();
169 {
170 llvm::SourceMgr SM;
171 llvm::yaml::Stream stream(JSONText, SM);
172 stream.skip();
173 }
174 Parsing.stopTimer();
175}
176
177static std::string createJSONText(size_t MemoryMB, unsigned ValueSize) {
Alp Toker8dd8d5c2014-06-26 22:52:05 +0000178 std::string JSONText;
179 llvm::raw_string_ostream Stream(JSONText);
180 Stream << "[\n";
Michael J. Spencer93210e82012-04-03 23:09:22 +0000181 size_t MemoryBytes = MemoryMB * 1024 * 1024;
Alp Toker8dd8d5c2014-06-26 22:52:05 +0000182 while (JSONText.size() < MemoryBytes) {
183 Stream << " {\n"
184 << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
185 << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
186 << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
187 << " }";
188 Stream.flush();
189 if (JSONText.size() < MemoryBytes) Stream << ",";
190 Stream << "\n";
Michael J. Spencer93210e82012-04-03 23:09:22 +0000191 }
Alp Toker8dd8d5c2014-06-26 22:52:05 +0000192 Stream << "]\n";
193 Stream.flush();
194 return JSONText;
Michael J. Spencer93210e82012-04-03 23:09:22 +0000195}
196
197int main(int argc, char **argv) {
198 llvm::cl::ParseCommandLineOptions(argc, argv);
Alex Lorenz9e31c0c2015-05-07 18:08:46 +0000199 bool ShowColors = UseColor == cl::BOU_UNSET
200 ? sys::Process::StandardOutHasColors()
201 : UseColor == cl::BOU_TRUE;
Michael J. Spencer93210e82012-04-03 23:09:22 +0000202 if (Input.getNumOccurrences()) {
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000203 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
204 MemoryBuffer::getFileOrSTDIN(Input);
205 if (!BufOrErr)
Michael J. Spencer93210e82012-04-03 23:09:22 +0000206 return 1;
Rafael Espindola9aa0b5e2014-08-01 14:31:55 +0000207 MemoryBuffer &Buf = *BufOrErr.get();
Michael J. Spencer93210e82012-04-03 23:09:22 +0000208
209 llvm::SourceMgr sm;
210 if (DumpTokens) {
Rafael Espindola9aa0b5e2014-08-01 14:31:55 +0000211 yaml::dumpTokens(Buf.getBuffer(), outs());
Michael J. Spencer93210e82012-04-03 23:09:22 +0000212 }
213
214 if (DumpCanonical) {
Alex Lorenz9e31c0c2015-05-07 18:08:46 +0000215 yaml::Stream stream(Buf.getBuffer(), sm, ShowColors);
Michael J. Spencer93210e82012-04-03 23:09:22 +0000216 dumpStream(stream);
Alex Lorenz9e31c0c2015-05-07 18:08:46 +0000217 if (stream.failed())
218 return 1;
Michael J. Spencer93210e82012-04-03 23:09:22 +0000219 }
220 }
221
222 if (Verify) {
Matthias Braun9262f002016-11-18 19:43:18 +0000223 llvm::TimerGroup Group("yaml", "YAML parser benchmark");
224 benchmark(Group, "Fast", "Fast", createJSONText(10, 500));
Michael J. Spencer93210e82012-04-03 23:09:22 +0000225 } else if (!DumpCanonical && !DumpTokens) {
Matthias Braun9262f002016-11-18 19:43:18 +0000226 llvm::TimerGroup Group("yaml", "YAML parser benchmark");
227 benchmark(Group, "Small", "Small Values", createJSONText(MemoryLimitMB, 5));
228 benchmark(Group, "Medium", "Medium Values",
229 createJSONText(MemoryLimitMB, 500));
230 benchmark(Group, "Large", "Large Values",
231 createJSONText(MemoryLimitMB, 50000));
Michael J. Spencer93210e82012-04-03 23:09:22 +0000232 }
233
234 return 0;
235}