protobufjs_benchmark.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var root = require("./generated_bundle_code.js");
  2. var fs = require('fs');
  3. var benchmark = require("./node_modules/benchmark");
  4. var benchmarkSuite = require("./benchmark_suite.js");
  5. function getNewPrototype(name) {
  6. var message = eval("root." + name);
  7. if (typeof(message) == "undefined") {
  8. throw "type " + name + " is undefined";
  9. }
  10. return message;
  11. }
  12. var results = [];
  13. console.log("#####################################################");
  14. console.log("ProtobufJs Benchmark: ");
  15. process.argv.forEach(function(filename, index) {
  16. if (index < 2) {
  17. return;
  18. }
  19. var benchmarkDataset =
  20. root.benchmarks.BenchmarkDataset.decode(fs.readFileSync(filename));
  21. var messageList = [];
  22. var totalBytes = 0;
  23. benchmarkDataset.payload.forEach(function(onePayload) {
  24. var message = getNewPrototype(benchmarkDataset.messageName);
  25. messageList.push(message.decode(onePayload));
  26. totalBytes += onePayload.length;
  27. });
  28. var senarios = benchmarkSuite.newBenchmark(
  29. benchmarkDataset.messageName, filename, "protobufjs");
  30. senarios.suite
  31. .add("protobuf.js static decoding", function() {
  32. benchmarkDataset.payload.forEach(function(onePayload) {
  33. var protoType = getNewPrototype(benchmarkDataset.messageName);
  34. protoType.decode(onePayload);
  35. });
  36. })
  37. .add("protobuf.js static encoding", function() {
  38. var protoType = getNewPrototype(benchmarkDataset.messageName);
  39. messageList.forEach(function(message) {
  40. protoType.encode(message).finish();
  41. });
  42. })
  43. .run({"Async": false});
  44. results.push({
  45. filename: filename,
  46. benchmarks: {
  47. protobufjs_decoding: senarios.benches[0] * totalBytes,
  48. protobufjs_encoding: senarios.benches[1] * totalBytes
  49. }
  50. })
  51. console.log("Throughput for decoding: "
  52. + senarios.benches[0] * totalBytes / 1024 / 1024 + "MB/s" );
  53. console.log("Throughput for encoding: "
  54. + senarios.benches[1] * totalBytes / 1024 / 1024 + "MB/s" );
  55. console.log("");
  56. });
  57. console.log("#####################################################");