cpp_benchmark.cc 7.9 KB

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