generate_datasets.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. const char *file_prefix = "dataset.";
  31. const char *file_suffix = ".pb";
  32. #include <fstream>
  33. #include <iostream>
  34. #include "benchmarks.pb.h"
  35. using benchmarks::BenchmarkDataset;
  36. using google::protobuf::Descriptor;
  37. using google::protobuf::DescriptorPool;
  38. using google::protobuf::Message;
  39. using google::protobuf::MessageFactory;
  40. #define ARRAY_TO_STRING(arr) std::string(arr, arr + sizeof(arr))
  41. std::set<std::string> names;
  42. void WriteFileWithPayloads(const std::string& name,
  43. const std::string& message_name,
  44. const std::vector<std::string>& payload) {
  45. if (!names.insert(name).second) {
  46. std::cerr << "Duplicate test name: " << name << "\n";
  47. abort();
  48. }
  49. // First verify that this message name exists in our set of benchmark messages
  50. // and that these payloads are valid for the given message.
  51. const Descriptor* d =
  52. DescriptorPool::generated_pool()->FindMessageTypeByName(message_name);
  53. if (!d) {
  54. std::cerr << "For dataset " << name << ", no such message: "
  55. << message_name << "\n";
  56. abort();
  57. }
  58. Message* m = MessageFactory::generated_factory()->GetPrototype(d)->New();
  59. for (size_t i = 0; i < payload.size(); i++) {
  60. if (!m->ParseFromString(payload[i])) {
  61. std::cerr << "For dataset " << name << ", payload[" << i << "] fails "
  62. << "to parse\n";
  63. abort();
  64. }
  65. }
  66. BenchmarkDataset dataset;
  67. dataset.set_name(name);
  68. dataset.set_message_name(message_name);
  69. for (size_t i = 0; i < payload.size(); i++) {
  70. dataset.add_payload()->assign(payload[i]);
  71. }
  72. std::string serialized;
  73. dataset.SerializeToString(&serialized);
  74. std::ofstream writer;
  75. std::string fname = file_prefix + name + file_suffix;
  76. writer.open(fname.c_str());
  77. writer << serialized;
  78. writer.close();
  79. std::cerr << "Wrote dataset: " << fname << "\n";
  80. }
  81. void WriteFile(const std::string& name, const std::string& message_name,
  82. const std::string& payload) {
  83. std::vector<std::string> payloads;
  84. payloads.push_back(payload);
  85. WriteFileWithPayloads(name, message_name, payloads);
  86. }
  87. std::string ReadFile(const std::string& name) {
  88. std::ifstream file(name.c_str());
  89. GOOGLE_CHECK(file.is_open()) << "Couldn't find file '" << name <<
  90. "', please make sure you are running "
  91. "this command from the benchmarks/ "
  92. "directory.\n";
  93. return std::string((std::istreambuf_iterator<char>(file)),
  94. std::istreambuf_iterator<char>());
  95. }
  96. int main() {
  97. WriteFile("google_message1_proto3", "benchmarks.p3.GoogleMessage1",
  98. ReadFile("google_message1.dat"));
  99. WriteFile("google_message1_proto2", "benchmarks.p2.GoogleMessage1",
  100. ReadFile("google_message1.dat"));
  101. // Not in proto3 because it has a group, which is not supported.
  102. WriteFile("google_message2", "benchmarks.p2.GoogleMessage2",
  103. ReadFile("google_message2.dat"));
  104. }