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