generate_datasets.cc 4.4 KB

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