• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "benchmarks.pb.h"
2 #include "datasets/google_message1/proto2/benchmark_message1_proto2.pb.h"
3 #include "datasets/google_message1/proto3/benchmark_message1_proto3.pb.h"
4 #include "datasets/google_message2/benchmark_message2.pb.h"
5 #include "datasets/google_message3/benchmark_message3.pb.h"
6 #include "datasets/google_message4/benchmark_message4.pb.h"
7 #include "data_proto2_to_proto3_util.h"
8 
9 #include <fstream>
10 
11 using google::protobuf::util::GogoDataStripper;
12 
ReadFile(const std::string & name)13 std::string ReadFile(const std::string& name) {
14   std::ifstream file(name.c_str());
15   GOOGLE_CHECK(file.is_open()) << "Couldn't find file '"
16       << name
17       << "', please make sure you are running this command from the benchmarks"
18       << " directory.\n";
19   return std::string((std::istreambuf_iterator<char>(file)),
20                      std::istreambuf_iterator<char>());
21 }
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[]) {
24   if (argc % 2 == 0 || argc == 1) {
25     std::cerr << "Usage: [input_files] [output_file_names] where " <<
26         "input_files are one to one mapping to output_file_names." <<
27         std::endl;
28     return 1;
29   }
30 
31   for (int i = argc / 2; i > 0; i--) {
32     const std::string &input_file = argv[i];
33     const std::string &output_file = argv[i + argc / 2];
34 
35     std::cerr << "Generating " << input_file
36         << " to " << output_file << std::endl;
37     benchmarks::BenchmarkDataset dataset;
38     Message* message;
39     std::string dataset_payload = ReadFile(input_file);
40     GOOGLE_CHECK(dataset.ParseFromString(dataset_payload))
41       << "Can' t parse data file " << input_file;
42 
43     if (dataset.message_name() == "benchmarks.proto3.GoogleMessage1") {
44       message = new benchmarks::proto3::GoogleMessage1;
45     } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage1") {
46       message = new benchmarks::proto2::GoogleMessage1;
47     } else if (dataset.message_name() == "benchmarks.proto2.GoogleMessage2") {
48       message = new benchmarks::proto2::GoogleMessage2;
49     } else if (dataset.message_name() ==
50         "benchmarks.google_message3.GoogleMessage3") {
51       message = new benchmarks::google_message3::GoogleMessage3;
52     } else if (dataset.message_name() ==
53         "benchmarks.google_message4.GoogleMessage4") {
54       message = new benchmarks::google_message4::GoogleMessage4;
55     } else {
56       std::cerr << "Unknown message type: " << dataset.message_name();
57       exit(1);
58     }
59 
60     for (int i = 0; i < dataset.payload_size(); i++) {
61       message->ParseFromString(dataset.payload(i));
62       GogoDataStripper stripper;
63       stripper.StripMessage(message);
64       dataset.set_payload(i, message->SerializeAsString());
65     }
66 
67     std::ofstream ofs(output_file);
68     ofs << dataset.SerializeAsString();
69     ofs.close();
70   }
71 
72 
73   return 0;
74 }
75