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