OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // https://developers.google.com/protocol-buffers/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
9 // * Redistributions of source code must retain the above copyright | |
10 // notice, this list of conditions and the following disclaimer. | |
11 // * Redistributions in binary form must reproduce the above | |
12 // copyright notice, this list of conditions and the following disclaimer | |
13 // in the documentation and/or other materials provided with the | |
14 // distribution. | |
15 // * Neither the name of Google Inc. nor the names of its | |
16 // contributors may be used to endorse or promote products derived from | |
17 // this software without specific prior written permission. | |
18 // | |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 #include <glob.h> | |
32 #include <iostream> | |
33 #include <fstream> | |
34 #include "benchmark/benchmark_api.h" | |
35 #include "benchmarks.pb.h" | |
36 #include "benchmark_messages_proto2.pb.h" | |
37 #include "benchmark_messages_proto3.pb.h" | |
38 | |
39 #define PREFIX "dataset." | |
40 #define SUFFIX ".pb" | |
41 | |
42 using benchmarks::BenchmarkDataset; | |
43 using google::protobuf::Arena; | |
44 using google::protobuf::Descriptor; | |
45 using google::protobuf::DescriptorPool; | |
46 using google::protobuf::Message; | |
47 using google::protobuf::MessageFactory; | |
48 | |
49 class Fixture : public benchmark::Fixture { | |
50 public: | |
51 Fixture(const BenchmarkDataset& dataset, const std::string& suffix) { | |
52 for (int i = 0; i < dataset.payload_size(); i++) { | |
53 payloads_.push_back(dataset.payload(i)); | |
54 } | |
55 | |
56 const Descriptor* d = | |
57 DescriptorPool::generated_pool()->FindMessageTypeByName( | |
58 dataset.message_name()); | |
59 | |
60 if (!d) { | |
61 std::cerr << "Couldn't find message named '" << dataset.message_name() | |
62 << "\n"; | |
63 } | |
64 | |
65 prototype_ = MessageFactory::generated_factory()->GetPrototype(d); | |
66 SetName((dataset.name() + suffix).c_str()); | |
67 } | |
68 | |
69 protected: | |
70 std::vector<std::string> payloads_; | |
71 const Message* prototype_; | |
72 }; | |
73 | |
74 class WrappingCounter { | |
75 public: | |
76 WrappingCounter(size_t limit) : value_(0), limit_(limit) {} | |
77 | |
78 size_t Next() { | |
79 size_t ret = value_; | |
80 if (++value_ == limit_) { | |
81 value_ = 0; | |
82 } | |
83 return ret; | |
84 } | |
85 | |
86 private: | |
87 size_t value_; | |
88 size_t limit_; | |
89 }; | |
90 | |
91 template <class T> | |
92 class ParseNewFixture : public Fixture { | |
93 public: | |
94 ParseNewFixture(const BenchmarkDataset& dataset) | |
95 : Fixture(dataset, "_parse_new") {} | |
96 | |
97 virtual void BenchmarkCase(benchmark::State& state) { | |
98 WrappingCounter i(payloads_.size()); | |
99 size_t total = 0; | |
100 | |
101 while (state.KeepRunning()) { | |
102 T m; | |
103 const std::string& payload = payloads_[i.Next()]; | |
104 total += payload.size(); | |
105 m.ParseFromString(payload); | |
106 } | |
107 | |
108 state.SetBytesProcessed(total); | |
109 } | |
110 }; | |
111 | |
112 template <class T> | |
113 class ParseNewArenaFixture : public Fixture { | |
114 public: | |
115 ParseNewArenaFixture(const BenchmarkDataset& dataset) | |
116 : Fixture(dataset, "_parse_newarena") {} | |
117 | |
118 virtual void BenchmarkCase(benchmark::State& state) { | |
119 WrappingCounter i(payloads_.size()); | |
120 size_t total = 0; | |
121 | |
122 while (state.KeepRunning()) { | |
123 Arena arena; | |
124 Message* m = Arena::CreateMessage<T>(&arena); | |
125 const std::string& payload = payloads_[i.Next()]; | |
126 total += payload.size(); | |
127 m->ParseFromString(payload); | |
128 } | |
129 | |
130 state.SetBytesProcessed(total); | |
131 } | |
132 }; | |
133 | |
134 template <class T> | |
135 class ParseReuseFixture : public Fixture { | |
136 public: | |
137 ParseReuseFixture(const BenchmarkDataset& dataset) | |
138 : Fixture(dataset, "_parse_reuse") {} | |
139 | |
140 virtual void BenchmarkCase(benchmark::State& state) { | |
141 T m; | |
142 WrappingCounter i(payloads_.size()); | |
143 size_t total = 0; | |
144 | |
145 while (state.KeepRunning()) { | |
146 const std::string& payload = payloads_[i.Next()]; | |
147 total += payload.size(); | |
148 m.ParseFromString(payload); | |
149 } | |
150 | |
151 state.SetBytesProcessed(total); | |
152 } | |
153 }; | |
154 | |
155 template <class T> | |
156 class SerializeFixture : public Fixture { | |
157 public: | |
158 SerializeFixture(const BenchmarkDataset& dataset) | |
159 : Fixture(dataset, "_serialize") { | |
160 for (size_t i = 0; i < payloads_.size(); i++) { | |
161 message_.push_back(new T); | |
162 message_.back()->ParseFromString(payloads_[i]); | |
163 } | |
164 } | |
165 | |
166 ~SerializeFixture() { | |
167 for (size_t i = 0; i < message_.size(); i++) { | |
168 delete message_[i]; | |
169 } | |
170 } | |
171 | |
172 virtual void BenchmarkCase(benchmark::State& state) { | |
173 size_t total = 0; | |
174 std::string str; | |
175 WrappingCounter i(payloads_.size()); | |
176 | |
177 while (state.KeepRunning()) { | |
178 str.clear(); | |
179 message_[i.Next()]->SerializeToString(&str); | |
180 total += str.size(); | |
181 } | |
182 | |
183 state.SetBytesProcessed(total); | |
184 } | |
185 | |
186 private: | |
187 std::vector<T*> message_; | |
188 }; | |
189 | |
190 std::string ReadFile(const std::string& name) { | |
191 std::ifstream file(name.c_str()); | |
192 GOOGLE_CHECK(file.is_open()) << "Couldn't find file '" << name << | |
193 "', please make sure you are running " | |
194 "this command from the benchmarks/ " | |
195 "directory.\n"; | |
196 return std::string((std::istreambuf_iterator<char>(file)), | |
197 std::istreambuf_iterator<char>()); | |
198 } | |
199 | |
200 template <class T> | |
201 void RegisterBenchmarksForType(const BenchmarkDataset& dataset) { | |
202 ::benchmark::internal::RegisterBenchmarkInternal( | |
203 new ParseNewFixture<T>(dataset)); | |
204 ::benchmark::internal::RegisterBenchmarkInternal( | |
205 new ParseReuseFixture<T>(dataset)); | |
206 ::benchmark::internal::RegisterBenchmarkInternal( | |
207 new ParseNewArenaFixture<T>(dataset)); | |
208 ::benchmark::internal::RegisterBenchmarkInternal( | |
209 new SerializeFixture<T>(dataset)); | |
210 } | |
211 | |
212 void RegisterBenchmarks(const std::string& dataset_bytes) { | |
213 BenchmarkDataset dataset; | |
214 GOOGLE_CHECK(dataset.ParseFromString(dataset_bytes)); | |
215 | |
216 if (dataset.message_name() == "benchmarks.proto3.GoogleMessage1") { | |
217 RegisterBenchmarksForType<benchmarks::proto3::GoogleMessage1>(dataset); | |
218 } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage1") { | |
219 RegisterBenchmarksForType<benchmarks::proto2::GoogleMessage1>(dataset); | |
220 } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage2") { | |
221 RegisterBenchmarksForType<benchmarks::proto2::GoogleMessage2>(dataset); | |
222 } else { | |
223 std::cerr << "Unknown message type: " << dataset.message_name(); | |
224 exit(1); | |
225 } | |
226 } | |
227 | |
228 int main(int argc, char *argv[]) { | |
229 glob_t glob_result; | |
230 if (glob("dataset.*.pb", 0, NULL, &glob_result) != 0) { | |
231 fprintf(stderr, "No dataset files found.\n"); | |
232 return 1; | |
233 } | |
234 | |
235 for (size_t i = 0; i < glob_result.gl_pathc; i++) { | |
236 fprintf(stderr, "Found input dataset: %s\n", glob_result.gl_pathv[i]); | |
237 RegisterBenchmarks(ReadFile(glob_result.gl_pathv[i])); | |
238 } | |
239 | |
240 ::benchmark::Initialize(&argc, argv); | |
241 ::benchmark::RunSpecifiedBenchmarks(); | |
242 } | |
OLD | NEW |