protobufjs_benchmark.js 1.99 KB
Newer Older
Yilun Chong's avatar
Yilun Chong committed
1
var root = require("./generated_bundle_code.js");
2 3
var fs = require('fs');
var benchmark = require("./node_modules/benchmark");
Yilun Chong's avatar
Yilun Chong committed
4
var benchmarkSuite = require("./benchmark_suite.js");
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32


function getNewPrototype(name) {
  var message = eval("root." + name);
  if (typeof(message) == "undefined") {
    throw "type " + name + " is undefined";
  }
  return message;
}


var results = [];

console.log("#####################################################");
console.log("ProtobufJs Benchmark: ");
process.argv.forEach(function(filename, index) {
  if (index < 2) {
    return;
  }
  var benchmarkDataset =
      root.benchmarks.BenchmarkDataset.decode(fs.readFileSync(filename));
  var messageList = [];
  var totalBytes = 0;
  benchmarkDataset.payload.forEach(function(onePayload) {
    var message = getNewPrototype(benchmarkDataset.messageName);
    messageList.push(message.decode(onePayload));
    totalBytes += onePayload.length;
  });
33

34 35 36 37 38 39 40
  var senarios = benchmarkSuite.newBenchmark(
    benchmarkDataset.messageName, filename, "protobufjs");
  senarios.suite
  .add("protobuf.js static decoding", function() {
    benchmarkDataset.payload.forEach(function(onePayload) {
      var protoType = getNewPrototype(benchmarkDataset.messageName);
      protoType.decode(onePayload);
41
    });
42 43 44 45 46 47
  })
  .add("protobuf.js static encoding", function() {
    var protoType = getNewPrototype(benchmarkDataset.messageName);
    messageList.forEach(function(message) {
      protoType.encode(message).finish();
    });
48
  })
49 50 51 52 53 54 55 56 57 58
  .run({"Async": false});

  results.push({
    filename: filename,
    benchmarks: {
      protobufjs_decoding: senarios.benches[0] * totalBytes,
      protobufjs_encoding: senarios.benches[1] * totalBytes
    }
  })

59
  console.log("Throughput for decoding: "
60
    + senarios.benches[0] * totalBytes / 1024 / 1024 + "MB/s" );
61
  console.log("Throughput for encoding: "
62 63 64
    + senarios.benches[1] * totalBytes / 1024 / 1024 + "MB/s" );
  console.log("");
});
Yilun Chong's avatar
Yilun Chong committed
65 66
console.log("#####################################################");