cpp_benchmark.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_api.h"
  33. #include "benchmarks.pb.h"
  34. #include "datasets/google_message1/benchmark_message1_proto2.pb.h"
  35. #include "datasets/google_message1/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. while (state.KeepRunning()) {
  107. Arena arena;
  108. Message* m = Arena::CreateMessage<T>(&arena);
  109. const std::string& payload = payloads_[i.Next()];
  110. total += payload.size();
  111. m->ParseFromString(payload);
  112. }
  113. state.SetBytesProcessed(total);
  114. }
  115. };
  116. template <class T>
  117. class ParseReuseFixture : public Fixture {
  118. public:
  119. ParseReuseFixture(const BenchmarkDataset& dataset)
  120. : Fixture(dataset, "_parse_reuse") {}
  121. virtual void BenchmarkCase(benchmark::State& state) {
  122. T m;
  123. WrappingCounter i(payloads_.size());
  124. size_t total = 0;
  125. while (state.KeepRunning()) {
  126. const std::string& payload = payloads_[i.Next()];
  127. total += payload.size();
  128. m.ParseFromString(payload);
  129. }
  130. state.SetBytesProcessed(total);
  131. }
  132. };
  133. template <class T>
  134. class SerializeFixture : public Fixture {
  135. public:
  136. SerializeFixture(const BenchmarkDataset& dataset)
  137. : Fixture(dataset, "_serialize") {
  138. for (size_t i = 0; i < payloads_.size(); i++) {
  139. message_.push_back(new T);
  140. message_.back()->ParseFromString(payloads_[i]);
  141. }
  142. }
  143. ~SerializeFixture() {
  144. for (size_t i = 0; i < message_.size(); i++) {
  145. delete message_[i];
  146. }
  147. }
  148. virtual void BenchmarkCase(benchmark::State& state) {
  149. size_t total = 0;
  150. std::string str;
  151. WrappingCounter i(payloads_.size());
  152. while (state.KeepRunning()) {
  153. str.clear();
  154. message_[i.Next()]->SerializeToString(&str);
  155. total += str.size();
  156. }
  157. state.SetBytesProcessed(total);
  158. }
  159. private:
  160. std::vector<T*> message_;
  161. };
  162. std::string ReadFile(const std::string& name) {
  163. std::ifstream file(name.c_str());
  164. GOOGLE_CHECK(file.is_open()) << "Couldn't find file '" << name <<
  165. "', please make sure you are running "
  166. "this command from the benchmarks/ "
  167. "directory.\n";
  168. return std::string((std::istreambuf_iterator<char>(file)),
  169. std::istreambuf_iterator<char>());
  170. }
  171. template <class T>
  172. void RegisterBenchmarksForType(const BenchmarkDataset& dataset) {
  173. ::benchmark::internal::RegisterBenchmarkInternal(
  174. new ParseNewFixture<T>(dataset));
  175. ::benchmark::internal::RegisterBenchmarkInternal(
  176. new ParseReuseFixture<T>(dataset));
  177. ::benchmark::internal::RegisterBenchmarkInternal(
  178. new ParseNewArenaFixture<T>(dataset));
  179. ::benchmark::internal::RegisterBenchmarkInternal(
  180. new SerializeFixture<T>(dataset));
  181. }
  182. void RegisterBenchmarks(const std::string& dataset_bytes) {
  183. BenchmarkDataset dataset;
  184. GOOGLE_CHECK(dataset.ParseFromString(dataset_bytes));
  185. if (dataset.message_name() == "benchmarks.proto3.GoogleMessage1") {
  186. RegisterBenchmarksForType<benchmarks::proto3::GoogleMessage1>(dataset);
  187. } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage1") {
  188. RegisterBenchmarksForType<benchmarks::proto2::GoogleMessage1>(dataset);
  189. } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage2") {
  190. RegisterBenchmarksForType<benchmarks::proto2::GoogleMessage2>(dataset);
  191. } else if (dataset.message_name() ==
  192. "benchmarks.google_message3.GoogleMessage3") {
  193. RegisterBenchmarksForType
  194. <benchmarks::google_message3::GoogleMessage3>(dataset);
  195. } else if (dataset.message_name() ==
  196. "benchmarks.google_message4.GoogleMessage4") {
  197. RegisterBenchmarksForType
  198. <benchmarks::google_message4::GoogleMessage4>(dataset);
  199. } else {
  200. std::cerr << "Unknown message type: " << dataset.message_name();
  201. exit(1);
  202. }
  203. }
  204. int main(int argc, char *argv[]) {
  205. if (argc == 1) {
  206. std::cerr << "Usage: ./cpp-benchmark <input data>" << std::endl;
  207. std::cerr << "input data is in the format of \"benchmarks.proto\""
  208. << std::endl;
  209. return 1;
  210. } else {
  211. for (int i = 1; i < argc; i++) {
  212. RegisterBenchmarks(ReadFile(argv[i]));
  213. }
  214. }
  215. ::benchmark::Initialize(&argc, argv);
  216. ::benchmark::RunSpecifiedBenchmarks();
  217. }