Home | History | Annotate | Download | only in benchmarks
      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 
     31 #include <fstream>
     32 #include <iostream>
     33 #include "benchmarks.pb.h"
     34 
     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 
     41 std::set<std::string> names;
     42 
     43 const char *file_prefix = "dataset.";
     44 const char *file_suffix = ".pb";
     45 
     46 void 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 
     82   std::ofstream writer;
     83   std::string fname = file_prefix + name + file_suffix;
     84   writer.open(fname.c_str());
     85   dataset.SerializeToOstream(&writer);
     86   writer.close();
     87 
     88   std::cerr << "Wrote dataset: " << fname << "\n";
     89 }
     90 
     91 void 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 
     98 std::string ReadFile(const std::string& name) {
     99   std::ifstream file(name.c_str());
    100   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 
    108 int main() {
    109   WriteFile("google_message1_proto3", "benchmarks.proto3.GoogleMessage1",
    110             ReadFile("google_message1.dat"));
    111   WriteFile("google_message1_proto2", "benchmarks.proto2.GoogleMessage1",
    112             ReadFile("google_message1.dat"));
    113 
    114   // Not in proto3 because it has a group, which is not supported.
    115   WriteFile("google_message2", "benchmarks.proto2.GoogleMessage2",
    116             ReadFile("google_message2.dat"));
    117 }
    118