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