aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/protobuf.js
diff options
context:
space:
mode:
authorYilun Chong <yilunchong@google.com>2018-07-20 16:13:19 -0700
committerYilun Chong <yilunchong@google.com>2018-07-20 16:13:19 -0700
commita7071291c750fe2b1b8a20c155f4e3dd5a38db24 (patch)
tree5f5407575a5344bd7451b3544f3ffabd2a8963ea /benchmarks/protobuf.js
parent88795f64e4c5dc870c63b3d4d39924cc91590b2f (diff)
downloadprotobuf-a7071291c750fe2b1b8a20c155f4e3dd5a38db24.tar.gz
protobuf-a7071291c750fe2b1b8a20c155f4e3dd5a38db24.tar.bz2
protobuf-a7071291c750fe2b1b8a20c155f4e3dd5a38db24.zip
Add JS and Protobuf.js benchmark
Diffstat (limited to 'benchmarks/protobuf.js')
-rw-r--r--benchmarks/protobuf.js/GeneratePbjsFiles.js24
-rw-r--r--benchmarks/protobuf.js/ProtobufJsBenchmark.js65
2 files changed, 89 insertions, 0 deletions
diff --git a/benchmarks/protobuf.js/GeneratePbjsFiles.js b/benchmarks/protobuf.js/GeneratePbjsFiles.js
new file mode 100644
index 00000000..d0e9c66d
--- /dev/null
+++ b/benchmarks/protobuf.js/GeneratePbjsFiles.js
@@ -0,0 +1,24 @@
+var pbjs = require("./protobuf.js/cli").pbjs
+
+var argv = [];
+var protoFiles = [];
+var prefix = "";
+process.argv.forEach(function(val, index) {
+ var arg = val;
+ if (arg.length > 6 && arg.substring(arg.length - 6) == ".proto") {
+ protoFiles.push(arg);
+ } else if (arg.length > 15 && arg.substring(0, 15) == "--include_path=") {
+ prefix = arg.substring(15);
+ } else if (index >= 2) {
+ argv.push(arg);
+ }
+});
+protoFiles.forEach(function(val) {
+ argv.push(prefix + "/" + val);
+});
+
+pbjs.main(argv, function(err, output){
+ if (err) {
+ console.log(err);
+ }
+}); \ No newline at end of file
diff --git a/benchmarks/protobuf.js/ProtobufJsBenchmark.js b/benchmarks/protobuf.js/ProtobufJsBenchmark.js
new file mode 100644
index 00000000..7a1a7d56
--- /dev/null
+++ b/benchmarks/protobuf.js/ProtobufJsBenchmark.js
@@ -0,0 +1,65 @@
+var root = require("./GeneratedBundleCode.js");
+var fs = require('fs');
+var benchmark = require("./node_modules/benchmark");
+var benchmarkSuite = require("./BenchmarkSuite.js");
+
+
+function getNewPrototype(name) {
+ var message = eval("root." + name);
+ if (typeof(message) == "undefined") {
+ throw "type " + name + " is undefined";
+ }
+ return message;
+}
+
+
+var results = [];
+
+console.log("#####################################################");
+console.log("ProtobufJs Benchmark: ");
+process.argv.forEach(function(filename, index) {
+ if (index < 2) {
+ return;
+ }
+ var benchmarkDataset =
+ root.benchmarks.BenchmarkDataset.decode(fs.readFileSync(filename));
+ var messageList = [];
+ var totalBytes = 0;
+ benchmarkDataset.payload.forEach(function(onePayload) {
+ var message = getNewPrototype(benchmarkDataset.messageName);
+ messageList.push(message.decode(onePayload));
+ totalBytes += onePayload.length;
+ });
+
+ var senarios = benchmarkSuite.newBenchmark(
+ benchmarkDataset.messageName, filename, "protobufjs");
+ senarios.suite
+ .add("protobuf.js static decoding", function() {
+ benchmarkDataset.payload.forEach(function(onePayload) {
+ var protoType = getNewPrototype(benchmarkDataset.messageName);
+ protoType.decode(onePayload);
+ });
+ })
+ .add("protobuf.js static encoding", function() {
+ var protoType = getNewPrototype(benchmarkDataset.messageName);
+ messageList.forEach(function(message) {
+ protoType.encode(message).finish();
+ });
+ })
+ .run({"Async": false});
+
+ results.push({
+ filename: filename,
+ benchmarks: {
+ protobufjs_decoding: senarios.benches[0] * totalBytes,
+ protobufjs_encoding: senarios.benches[1] * totalBytes
+ }
+ })
+
+ console.log("Throughput for decoding: "
+ + senarios.benches[0] * totalBytes / 1024 / 1024 + "MB/s" );
+ console.log("Throughput for encoding: "
+ + senarios.benches[1] * totalBytes / 1024 / 1024 + "MB/s" );
+ console.log("");
+});
+console.log("#####################################################"); \ No newline at end of file