js_benchmark.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. require('./datasets/google_message1/proto2/benchmark_message1_proto2_pb.js');
  2. require('./datasets/google_message1/proto3/benchmark_message1_proto3_pb.js');
  3. require('./datasets/google_message2/benchmark_message2_pb.js');
  4. require('./datasets/google_message3/benchmark_message3_pb.js');
  5. require('./datasets/google_message4/benchmark_message4_pb.js');
  6. require('./benchmarks_pb.js');
  7. var fs = require('fs');
  8. var benchmarkSuite = require("./benchmark_suite.js");
  9. function getNewPrototype(name) {
  10. var message = eval("proto." + name);
  11. if (typeof(message) == "undefined") {
  12. throw "type " + name + " is undefined";
  13. }
  14. return message;
  15. }
  16. var results = [];
  17. var json_file = "";
  18. console.log("#####################################################");
  19. console.log("Js Benchmark: ");
  20. process.argv.forEach(function(filename, index) {
  21. if (index < 2) {
  22. return;
  23. }
  24. if (filename.indexOf("--json_output") != -1) {
  25. json_file = filename.replace(/^--json_output=/, '');
  26. return;
  27. }
  28. var benchmarkDataset =
  29. proto.benchmarks.BenchmarkDataset.deserializeBinary(fs.readFileSync(filename));
  30. var messageList = [];
  31. var totalBytes = 0;
  32. benchmarkDataset.getPayloadList().forEach(function(onePayload) {
  33. var message = getNewPrototype(benchmarkDataset.getMessageName());
  34. messageList.push(message.deserializeBinary(onePayload));
  35. totalBytes += onePayload.length;
  36. });
  37. var scenarios = benchmarkSuite.newBenchmark(
  38. benchmarkDataset.getMessageName(), filename, "js");
  39. scenarios.suite
  40. .add("js deserialize", function() {
  41. benchmarkDataset.getPayloadList().forEach(function(onePayload) {
  42. var protoType = getNewPrototype(benchmarkDataset.getMessageName());
  43. protoType.deserializeBinary(onePayload);
  44. });
  45. })
  46. .add("js serialize", function() {
  47. var protoType = getNewPrototype(benchmarkDataset.getMessageName());
  48. messageList.forEach(function(message) {
  49. message.serializeBinary();
  50. });
  51. })
  52. .run({"Async": false});
  53. results.push({
  54. filename: filename,
  55. benchmarks: {
  56. protobufjs_decoding: scenarios.benches[0] * totalBytes / 1024 / 1024,
  57. protobufjs_encoding: scenarios.benches[1] * totalBytes / 1024 / 1024
  58. }
  59. })
  60. console.log("Throughput for deserialize: "
  61. + scenarios.benches[0] * totalBytes / 1024 / 1024 + "MB/s" );
  62. console.log("Throughput for serialize: "
  63. + scenarios.benches[1] * totalBytes / 1024 / 1024 + "MB/s" );
  64. console.log("");
  65. });
  66. console.log("#####################################################");
  67. if (json_file != "") {
  68. fs.writeFile(json_file, JSON.stringify(results), (err) => {
  69. if (err) throw err;
  70. });
  71. }