generate_datasets.cc 4.2 KB

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