cpp_benchmark.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include <glob.h>
  31. #include <iostream>
  32. #include <fstream>
  33. #include "benchmark/benchmark_api.h"
  34. #include "benchmarks.pb.h"
  35. #include "benchmark_messages_proto2.pb.h"
  36. #include "benchmark_messages_proto3.pb.h"
  37. #define PREFIX "dataset."
  38. #define SUFFIX ".pb"
  39. using benchmarks::BenchmarkDataset;
  40. using google::protobuf::Arena;
  41. using google::protobuf::Descriptor;
  42. using google::protobuf::DescriptorPool;
  43. using google::protobuf::Message;
  44. using google::protobuf::MessageFactory;
  45. class Fixture : public benchmark::Fixture {
  46. public:
  47. Fixture(const BenchmarkDataset& dataset, const std::string& suffix) {
  48. for (int i = 0; i < dataset.payload_size(); i++) {
  49. payloads_.push_back(dataset.payload(i));
  50. }
  51. const Descriptor* d =
  52. DescriptorPool::generated_pool()->FindMessageTypeByName(
  53. dataset.message_name());
  54. if (!d) {
  55. std::cerr << "Couldn't find message named '" << dataset.message_name()
  56. << "\n";
  57. }
  58. prototype_ = MessageFactory::generated_factory()->GetPrototype(d);
  59. SetName((dataset.name() + suffix).c_str());
  60. }
  61. protected:
  62. std::vector<std::string> payloads_;
  63. const Message* prototype_;
  64. };
  65. class WrappingCounter {
  66. public:
  67. WrappingCounter(size_t limit) : value_(0), limit_(limit) {}
  68. size_t Next() {
  69. size_t ret = value_;
  70. if (++value_ == limit_) {
  71. value_ = 0;
  72. }
  73. return ret;
  74. }
  75. private:
  76. size_t value_;
  77. size_t limit_;
  78. };
  79. template <class T>
  80. class ParseNewFixture : public Fixture {
  81. public:
  82. ParseNewFixture(const BenchmarkDataset& dataset)
  83. : Fixture(dataset, "_parse_new") {}
  84. virtual void BenchmarkCase(benchmark::State& state) {
  85. WrappingCounter i(payloads_.size());
  86. size_t total = 0;
  87. while (state.KeepRunning()) {
  88. T m;
  89. const std::string& payload = payloads_[i.Next()];
  90. total += payload.size();
  91. m.ParseFromString(payload);
  92. }
  93. state.SetBytesProcessed(total);
  94. }
  95. };
  96. template <class T>
  97. class ParseNewArenaFixture : public Fixture {
  98. public:
  99. ParseNewArenaFixture(const BenchmarkDataset& dataset)
  100. : Fixture(dataset, "_parse_newarena") {}
  101. virtual void BenchmarkCase(benchmark::State& state) {
  102. WrappingCounter i(payloads_.size());
  103. size_t total = 0;
  104. while (state.KeepRunning()) {
  105. Arena arena;
  106. Message* m = Arena::CreateMessage<T>(&arena);
  107. const std::string& payload = payloads_[i.Next()];
  108. total += payload.size();
  109. m->ParseFromString(payload);
  110. }
  111. state.SetBytesProcessed(total);
  112. }
  113. };
  114. template <class T>
  115. class ParseReuseFixture : public Fixture {
  116. public:
  117. ParseReuseFixture(const BenchmarkDataset& dataset)
  118. : Fixture(dataset, "_parse_reuse") {}
  119. virtual void BenchmarkCase(benchmark::State& state) {
  120. T m;
  121. WrappingCounter i(payloads_.size());
  122. size_t total = 0;
  123. while (state.KeepRunning()) {
  124. const std::string& payload = payloads_[i.Next()];
  125. total += payload.size();
  126. m.ParseFromString(payload);
  127. }
  128. state.SetBytesProcessed(total);
  129. }
  130. };
  131. template <class T>
  132. class SerializeFixture : public Fixture {
  133. public:
  134. SerializeFixture(const BenchmarkDataset& dataset)
  135. : Fixture(dataset, "_serialize") {
  136. for (size_t i = 0; i < payloads_.size(); i++) {
  137. message_.push_back(new T);
  138. message_.back()->ParseFromString(payloads_[i]);
  139. }
  140. }
  141. ~SerializeFixture() {
  142. for (size_t i = 0; i < message_.size(); i++) {
  143. delete message_[i];
  144. }
  145. }
  146. virtual void BenchmarkCase(benchmark::State& state) {
  147. size_t total = 0;
  148. std::string str;
  149. WrappingCounter i(payloads_.size());
  150. while (state.KeepRunning()) {
  151. str.clear();
  152. message_[i.Next()]->SerializeToString(&str);
  153. total += str.size();
  154. }
  155. state.SetBytesProcessed(total);
  156. }
  157. private:
  158. std::vector<T*> message_;
  159. };
  160. std::string ReadFile(const std::string& name) {
  161. std::ifstream file(name.c_str());
  162. GOOGLE_CHECK(file.is_open()) << "Couldn't find file '" << name <<
  163. "', please make sure you are running "
  164. "this command from the benchmarks/ "
  165. "directory.\n";
  166. return std::string((std::istreambuf_iterator<char>(file)),
  167. std::istreambuf_iterator<char>());
  168. }
  169. template <class T>
  170. void RegisterBenchmarksForType(const BenchmarkDataset& dataset) {
  171. ::benchmark::internal::RegisterBenchmarkInternal(
  172. new ParseNewFixture<T>(dataset));
  173. ::benchmark::internal::RegisterBenchmarkInternal(
  174. new ParseReuseFixture<T>(dataset));
  175. ::benchmark::internal::RegisterBenchmarkInternal(
  176. new ParseNewArenaFixture<T>(dataset));
  177. ::benchmark::internal::RegisterBenchmarkInternal(
  178. new SerializeFixture<T>(dataset));
  179. }
  180. void RegisterBenchmarks(const std::string& dataset_bytes) {
  181. BenchmarkDataset dataset;
  182. GOOGLE_CHECK(dataset.ParseFromString(dataset_bytes));
  183. if (dataset.message_name() == "benchmarks.proto3.GoogleMessage1") {
  184. RegisterBenchmarksForType<benchmarks::proto3::GoogleMessage1>(dataset);
  185. } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage1") {
  186. RegisterBenchmarksForType<benchmarks::proto2::GoogleMessage1>(dataset);
  187. } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage2") {
  188. RegisterBenchmarksForType<benchmarks::proto2::GoogleMessage2>(dataset);
  189. } else {
  190. std::cerr << "Unknown message type: " << dataset.message_name();
  191. exit(1);
  192. }
  193. }
  194. int main(int argc, char *argv[]) {
  195. glob_t glob_result;
  196. if (glob("dataset.*.pb", 0, NULL, &glob_result) != 0) {
  197. fprintf(stderr, "No dataset files found.\n");
  198. return 1;
  199. }
  200. for (size_t i = 0; i < glob_result.gl_pathc; i++) {
  201. fprintf(stderr, "Found input dataset: %s\n", glob_result.gl_pathv[i]);
  202. RegisterBenchmarks(ReadFile(glob_result.gl_pathv[i]));
  203. }
  204. ::benchmark::Initialize(&argc, argv);
  205. ::benchmark::RunSpecifiedBenchmarks();
  206. }