1//===- YAMLBench - Benchmark the YAMLParser implementation ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This program executes the YAMLParser on differently sized YAML texts and
10// outputs the run time.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/SmallString.h"
15#include "llvm/Support/Casting.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/Process.h"
19#include "llvm/Support/SourceMgr.h"
20#include "llvm/Support/Timer.h"
21#include "llvm/Support/YAMLParser.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace llvm;
25
26static cl::opt<bool>
27 DumpTokens( "tokens"
28 , cl::desc("Print the tokenization of the file.")
29 , cl::init(Val: false)
30 );
31
32static cl::opt<bool>
33 DumpCanonical( "canonical"
34 , cl::desc("Print the canonical YAML for this file.")
35 , cl::init(Val: false)
36 );
37
38static cl::opt<std::string>
39 Input(cl::Positional, cl::desc("<input>"));
40
41static cl::opt<bool>
42 Verify( "verify"
43 , cl::desc(
44 "Run a quick verification useful for regression testing")
45 , cl::init(Val: false)
46 );
47
48static cl::opt<unsigned>
49 MemoryLimitMB("memory-limit", cl::desc(
50 "Do not use more megabytes of memory"),
51 cl::init(Val: 1000));
52
53static cl::opt<cl::boolOrDefault>
54 UseColor("use-color", cl::desc("Emit colored output (default=autodetect)"),
55 cl::init(Val: cl::boolOrDefault::BOU_UNSET));
56
57/// Pretty print a tag by replacing tag:yaml.org,2002: with !!.
58static std::string prettyTag(yaml::Node *N) {
59 std::string Tag = N->getVerbatimTag();
60 if (StringRef(Tag).starts_with(Prefix: "tag:yaml.org,2002:")) {
61 std::string Ret = "!!";
62 Ret += StringRef(Tag).substr(Start: 18);
63 return Ret;
64 }
65 std::string Ret = "!<";
66 Ret += Tag;
67 Ret += ">";
68 return Ret;
69}
70
71static void dumpNode( yaml::Node *n
72 , unsigned Indent = 0
73 , bool SuppressFirstIndent = false) {
74 if (!n)
75 return;
76 if (!SuppressFirstIndent)
77 outs() << indent(Indent);
78 StringRef Anchor = n->getAnchor();
79 if (!Anchor.empty())
80 outs() << "&" << Anchor << " ";
81 if (yaml::ScalarNode *sn = dyn_cast<yaml::ScalarNode>(Val: n)) {
82 SmallString<32> Storage;
83 StringRef Val = sn->getValue(Storage);
84 outs() << prettyTag(N: n) << " \"" << yaml::escape(Input: Val) << "\"";
85 } else if (yaml::BlockScalarNode *BN = dyn_cast<yaml::BlockScalarNode>(Val: n)) {
86 outs() << prettyTag(N: n) << " \"" << yaml::escape(Input: BN->getValue()) << "\"";
87 } else if (yaml::SequenceNode *sn = dyn_cast<yaml::SequenceNode>(Val: n)) {
88 outs() << prettyTag(N: n) << " [\n";
89 ++Indent;
90 for (yaml::SequenceNode::iterator i = sn->begin(), e = sn->end();
91 i != e; ++i) {
92 dumpNode(n: i, Indent);
93 outs() << ",\n";
94 }
95 --Indent;
96 outs() << indent(Indent) << "]";
97 } else if (yaml::MappingNode *mn = dyn_cast<yaml::MappingNode>(Val: n)) {
98 outs() << prettyTag(N: n) << " {\n";
99 ++Indent;
100 for (yaml::MappingNode::iterator i = mn->begin(), e = mn->end();
101 i != e; ++i) {
102 outs() << indent(Indent) << "? ";
103 dumpNode(n: i->getKey(), Indent, SuppressFirstIndent: true);
104 outs() << "\n";
105 outs() << indent(Indent) << ": ";
106 dumpNode(n: i->getValue(), Indent, SuppressFirstIndent: true);
107 outs() << ",\n";
108 }
109 --Indent;
110 outs() << indent(Indent) << "}";
111 } else if (yaml::AliasNode *an = dyn_cast<yaml::AliasNode>(Val: n)){
112 outs() << "*" << an->getName();
113 } else if (isa<yaml::NullNode>(Val: n)) {
114 outs() << prettyTag(N: n) << " null";
115 }
116}
117
118static void dumpStream(yaml::Stream &stream) {
119 for (yaml::document_iterator di = stream.begin(), de = stream.end(); di != de;
120 ++di) {
121 outs() << "%YAML 1.2\n"
122 << "---\n";
123 yaml::Node *n = di->getRoot();
124 if (n)
125 dumpNode(n);
126 else
127 break;
128 outs() << "\n...\n";
129 }
130}
131
132static void benchmark(llvm::TimerGroup &Group, llvm::StringRef Name,
133 llvm::StringRef Description, llvm::StringRef JSONText) {
134 llvm::Timer BaseLine((Name + ".loop").str(), (Description + ": Loop").str(),
135 Group);
136 BaseLine.startTimer();
137 char C = 0;
138 for (llvm::StringRef::iterator I = JSONText.begin(),
139 E = JSONText.end();
140 I != E; ++I) { C += *I; }
141 BaseLine.stopTimer();
142 volatile char DontOptimizeOut = C; (void)DontOptimizeOut;
143
144 llvm::Timer Tokenizing((Name + ".tokenizing").str(),
145 (Description + ": Tokenizing").str(), Group);
146 Tokenizing.startTimer();
147 {
148 yaml::scanTokens(Input: JSONText);
149 }
150 Tokenizing.stopTimer();
151
152 llvm::Timer Parsing((Name + ".parsing").str(),
153 (Description + ": Parsing").str(), Group);
154 Parsing.startTimer();
155 {
156 llvm::SourceMgr SM;
157 llvm::yaml::Stream stream(JSONText, SM);
158 stream.skip();
159 }
160 Parsing.stopTimer();
161}
162
163static std::string createJSONText(size_t MemoryMB, unsigned ValueSize) {
164 std::string JSONText;
165 llvm::raw_string_ostream Stream(JSONText);
166 Stream << "[\n";
167 size_t MemoryBytes = MemoryMB * 1024 * 1024;
168 while (JSONText.size() < MemoryBytes) {
169 Stream << " {\n"
170 << " \"key1\": \"" << std::string(ValueSize, '*') << "\",\n"
171 << " \"key2\": \"" << std::string(ValueSize, '*') << "\",\n"
172 << " \"key3\": \"" << std::string(ValueSize, '*') << "\"\n"
173 << " }";
174 if (JSONText.size() < MemoryBytes) Stream << ",";
175 Stream << "\n";
176 }
177 Stream << "]\n";
178 return JSONText;
179}
180
181int main(int argc, char **argv) {
182 llvm::cl::ParseCommandLineOptions(argc, argv);
183 bool ShowColors = UseColor == cl::boolOrDefault::BOU_UNSET
184 ? sys::Process::StandardOutHasColors()
185 : UseColor == cl::boolOrDefault::BOU_TRUE;
186 if (Input.getNumOccurrences()) {
187 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
188 MemoryBuffer::getFileOrSTDIN(Filename: Input);
189 if (!BufOrErr)
190 return 1;
191 MemoryBuffer &Buf = *BufOrErr.get();
192
193 llvm::SourceMgr sm;
194 if (DumpTokens) {
195 yaml::dumpTokens(Input: Buf.getBuffer(), outs());
196 }
197
198 if (DumpCanonical) {
199 yaml::Stream stream(Buf.getBuffer(), sm, ShowColors);
200 dumpStream(stream);
201 if (stream.failed())
202 return 1;
203 }
204 }
205
206 if (Verify) {
207 llvm::TimerGroup Group("yaml", "YAML parser benchmark");
208 benchmark(Group, Name: "Fast", Description: "Fast", JSONText: createJSONText(MemoryMB: 10, ValueSize: 500));
209 } else if (!DumpCanonical && !DumpTokens) {
210 llvm::TimerGroup Group("yaml", "YAML parser benchmark");
211 benchmark(Group, Name: "Small", Description: "Small Values", JSONText: createJSONText(MemoryMB: MemoryLimitMB, ValueSize: 5));
212 benchmark(Group, Name: "Medium", Description: "Medium Values",
213 JSONText: createJSONText(MemoryMB: MemoryLimitMB, ValueSize: 500));
214 benchmark(Group, Name: "Large", Description: "Large Values",
215 JSONText: createJSONText(MemoryMB: MemoryLimitMB, ValueSize: 50000));
216 }
217
218 return 0;
219}
220