blob: 61e7adf1ba3f0e21a788d9a412f0cee22bf99746 [file] [log] [blame]
Josh Haberman2e831102016-04-27 18:22:22 -07001// 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
Josh Haberman2e831102016-04-27 18:22:22 -070031#include <fstream>
32#include <iostream>
33#include "benchmarks.pb.h"
Josh Haberman2e831102016-04-27 18:22:22 -070034
35using benchmarks::BenchmarkDataset;
36using google::protobuf::Descriptor;
37using google::protobuf::DescriptorPool;
38using google::protobuf::Message;
39using google::protobuf::MessageFactory;
40
Josh Haberman2e831102016-04-27 18:22:22 -070041std::set<std::string> names;
42
Josh Haberman247ef1f2016-05-03 12:53:49 -070043const char *file_prefix = "dataset.";
44const char *file_suffix = ".pb";
45
Josh Haberman2e831102016-04-27 18:22:22 -070046void WriteFileWithPayloads(const std::string& name,
47 const std::string& message_name,
48 const std::vector<std::string>& payload) {
49 if (!names.insert(name).second) {
50 std::cerr << "Duplicate test name: " << name << "\n";
51 abort();
52 }
53
54 // First verify that this message name exists in our set of benchmark messages
55 // and that these payloads are valid for the given message.
56 const Descriptor* d =
57 DescriptorPool::generated_pool()->FindMessageTypeByName(message_name);
58
59 if (!d) {
60 std::cerr << "For dataset " << name << ", no such message: "
61 << message_name << "\n";
62 abort();
63 }
64
65 Message* m = MessageFactory::generated_factory()->GetPrototype(d)->New();
66
67 for (size_t i = 0; i < payload.size(); i++) {
68 if (!m->ParseFromString(payload[i])) {
69 std::cerr << "For dataset " << name << ", payload[" << i << "] fails "
70 << "to parse\n";
71 abort();
72 }
73 }
74
75 BenchmarkDataset dataset;
76 dataset.set_name(name);
77 dataset.set_message_name(message_name);
78 for (size_t i = 0; i < payload.size(); i++) {
79 dataset.add_payload()->assign(payload[i]);
80 }
81
Josh Haberman2e831102016-04-27 18:22:22 -070082 std::ofstream writer;
83 std::string fname = file_prefix + name + file_suffix;
Josh Habermanb2d4b1a2016-04-29 10:22:56 -070084 writer.open(fname.c_str());
Josh Haberman247ef1f2016-05-03 12:53:49 -070085 dataset.SerializeToOstream(&writer);
Josh Haberman2e831102016-04-27 18:22:22 -070086 writer.close();
87
88 std::cerr << "Wrote dataset: " << fname << "\n";
89}
90
91void WriteFile(const std::string& name, const std::string& message_name,
92 const std::string& payload) {
93 std::vector<std::string> payloads;
94 payloads.push_back(payload);
95 WriteFileWithPayloads(name, message_name, payloads);
96}
97
Josh Haberman49a89182016-04-29 10:19:03 -070098std::string ReadFile(const std::string& name) {
Josh Habermanb2d4b1a2016-04-29 10:22:56 -070099 std::ifstream file(name.c_str());
Josh Haberman49a89182016-04-29 10:19:03 -0700100 GOOGLE_CHECK(file.is_open()) << "Couldn't find file '" << name <<
101 "', please make sure you are running "
102 "this command from the benchmarks/ "
103 "directory.\n";
104 return std::string((std::istreambuf_iterator<char>(file)),
105 std::istreambuf_iterator<char>());
106}
107
Josh Haberman2e831102016-04-27 18:22:22 -0700108int main() {
Josh Haberman247ef1f2016-05-03 12:53:49 -0700109 WriteFile("google_message1_proto3", "benchmarks.proto3.GoogleMessage1",
Josh Haberman49a89182016-04-29 10:19:03 -0700110 ReadFile("google_message1.dat"));
Josh Haberman247ef1f2016-05-03 12:53:49 -0700111 WriteFile("google_message1_proto2", "benchmarks.proto2.GoogleMessage1",
Josh Haberman49a89182016-04-29 10:19:03 -0700112 ReadFile("google_message1.dat"));
Josh Haberman2e831102016-04-27 18:22:22 -0700113
114 // Not in proto3 because it has a group, which is not supported.
Josh Haberman247ef1f2016-05-03 12:53:49 -0700115 WriteFile("google_message2", "benchmarks.proto2.GoogleMessage2",
Josh Haberman49a89182016-04-29 10:19:03 -0700116 ReadFile("google_message2.dat"));
Josh Haberman2e831102016-04-27 18:22:22 -0700117}