Permalink
Browse files

Other: Some cleanup and added a logo

  • Loading branch information...
dcodeIO committed Jan 3, 2017
1 parent cba46c3 commit 0643f93f5c0d96ed0ece5b47f54993ac3a827f1b
Showing with 71 additions and 63 deletions.
  1. +2 −2 README.md
  2. +15 −13 bench/alloc.js
  3. +13 −7 bench/index.js
  4. +17 −14 bench/prof.js
  5. +6 −7 bench/read.js
  6. +8 −12 bench/suite.js
  7. +8 −8 bench/write.js
  8. BIN pbjs.png
  9. +2 −0 src/parse.js
@@ -1,5 +1,5 @@
protobuf.js
===========
# <p align="center"><img src="./pbjs.png" /></p>
[![travis][travis-image]][travis-url] [![david][david-image]][david-url] [![npm][npm-dl-image]][npm-url] [![npm][npm-image]][npm-url] [![donate][paypal-image]][paypal-url]
**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)).
@@ -1,10 +1,12 @@
var protobuf = require(".."),
newSuite = require("./suite");
/*eslint-disable no-new*//*global ArrayBuffer*/
"use strict";
var pool = require("../src/util/pool"),
poolAlloc = pool(function(size) {
return new Uint8Array(size);
}, Uint8Array.prototype.subarray);
var newSuite = require("./suite"),
pool = require("../src/util/pool");
var poolAlloc = pool(function(size) {
return new Uint8Array(size);
}, Uint8Array.prototype.subarray);
[
64,
@@ -15,30 +17,30 @@ var pool = require("../src/util/pool"),
newSuite("buffer[" + size + "]")
.add("new Uint8Array", function() {
var buf = new Uint8Array(size);
new Uint8Array(size);
})
.add("Buffer.alloc", function() {
var buf = Buffer.alloc(size);
Buffer.alloc(size);
})
.add("poolAlloc<Uint8Array>", function() {
var buf = poolAlloc(size);
poolAlloc(size);
})
.add("Buffer.allocUnsafe", function() {
var buf = Buffer.allocUnsafe(size);
Buffer.allocUnsafe(size);
})
.add("new Buffer", function() {
var buf = new Buffer(size);
new Buffer(size);
})
.run();
var ab = new ArrayBuffer(size);
newSuite("wrap[" + size + "]")
.add("new Uint8Array(ArrayBuffer)", function() {
var buf = new Uint8Array(ab);
new Uint8Array(ab);
})
.add("Buffer.from(ArrayBuffer)", function() {
var buf = Buffer.from(ab);
Buffer.from(ab);
})
.run();
@@ -1,3 +1,5 @@
"use strict";
var protobuf = require(".."),
newSuite = require("./suite"),
data = require("./bench.json");
@@ -19,21 +21,22 @@ var protobuf = require(".."),
//
// To experience the impact by yourself, increase string lengths within bench.json.
var root = protobuf.loadSync(require.resolve("./bench.proto"));
var Test = root.resolveAll().lookup("Test");
var root = protobuf.loadSync(require.resolve("./bench.proto")),
Test = root.resolveAll().lookup("Test");
// protobuf.util.codegen.verbose = true;
var buf = Test.encode(data).finish();
// warm up
for (var i = 0; i < 500000; ++i)
var i;
for (i = 0; i < 500000; ++i)
Test.encode(data).finish();
for (var i = 0; i < 1000000; ++i)
for (i = 0; i < 1000000; ++i)
Test.decode(buf);
for (var i = 0; i < 500000; ++i)
for (i = 0; i < 500000; ++i)
Test.verify(data);
console.log("");
process.stdout.write("\n");
if (!Buffer.from)
Buffer.from = function from(str, enc) {
@@ -92,10 +95,13 @@ setTimeout(function() {
var dataMessage = Test.from(data);
var dataJson = dataMessage.asJSON();
newSuite("converting")
newSuite("converting from JSON")
.add("Message.from", function() {
Test.from(dataJson);
})
.run();
newSuite("converting to JSON")
.add("Message#asJSON", function() {
dataMessage.asJSON();
})
@@ -1,40 +1,42 @@
"use strict";
var fs = require("fs"),
path = require("path");
// A profiling stub to measure encoding / decoding performance using benchmark data.
var commands = ["encode", "decode", "encode-browser", "decode-browser"];
if (commands.indexOf(process.argv[2]) < 0) { // 0: node, 1: prof.js
console.error("usage: " + path.basename(process.argv[1]) + " <" + commands.join('|') + "> [iterations=10000000]");
process.exit(0);
process.stderr.write("usage: " + path.basename(process.argv[1]) + " <" + commands.join("|") + "> [iterations=10000000]\n");
return;
}
// Spin up a node process with profiling enabled and process the generated log
if (process.execArgv.indexOf("--prof") < 0) {
console.log("cleaning up old logs ...");
process.stdout.write("cleaning up old logs ...\n");
var child_process = require("child_process");
var logRe = /^isolate\-[0-9A-F]+\-v8\.log$/;
var logRe = /^isolate-[0-9A-F]+-v8\.log$/;
fs.readdirSync(process.cwd()).forEach(function readdirSync_it(file) {
if (logRe.test(file))
fs.unlink(file);
});
console.log("generating profile (may take a while) ...");
var child = child_process.execSync("node --prof --trace-deopt " + process.execArgv.join(" ") + " " + process.argv.slice(1).join(' '), {
process.stdout.write("generating profile (may take a while) ...\n");
child_process.execSync("node --prof --trace-deopt " + process.execArgv.join(" ") + " " + process.argv.slice(1).join(' '), {
cwd: process.cwd(),
stdio: 'inherit'
stdio: "inherit"
});
console.log("processing profile ...");
process.stdout.write("processing profile ...\n");
fs.readdirSync(process.cwd()).forEach(function readdirSync_it(file) {
if (logRe.test(file)) {
child_process.execSync("node --prof-process " + file, {
cwd: process.cwd(),
stdio: 'inherit'
stdio: "inherit"
});
// fs.unlink(file);
}
});
console.log("done.");
process.exit(0);
process.stdout.write("done.\n");
return;
}
// Actual profiling code
@@ -59,7 +61,7 @@ if (process.argv.indexOf("--alt") < 0) {
if (process.argv.length > 3 && /^\d+$/.test(process.argv[3]))
count = parseInt(process.argv[3], 10);
console.log(" x " + count);
process.stdout.write(" x " + count + "\n");
function setupBrowser() {
protobuf.Writer.create = function create_browser() { return new protobuf.Writer(); };
@@ -69,16 +71,17 @@ function setupBrowser() {
switch (process.argv[2]) {
case "encode-browser":
setupBrowser();
// eslint-disable-line no-fallthrough
case "encode":
for (var i = 0; i < count; ++i)
Test.encode(data).finish();
break;
case "decode-browser":
setupBrowser();
// eslint-disable-line no-fallthrough
case "decode":
var buf = Test.encode(data).finish();
for (var i = 0; i < count; ++i)
for (var j = 0; j < count; ++j)
Test.decode(buf);
break;
}
process.exit(0);
@@ -1,11 +1,10 @@
var protobuf = require(".."),
newSuite = require("./suite"),
"use strict";
var newSuite = require("./suite"),
ieee754 = require("../lib/ieee754");
// This benchmark compares raw data type performance of Uint8Array and Buffer.
var data = [ 0xCD, 0xCC, 0xCC, 0x3D ]; // ~0.10000000149011612 LE
var array = new Uint8Array(data);
var buffer = Buffer.from(data);
@@ -42,10 +41,9 @@ newSuite("float")
})
.run();
var data = [ 0x9A, 0x99, 0x99, 0x99, 0x99, 0x99, 0xB9, 0x3F ]; // 0.1 LE
var array = new Uint8Array(data);
var buffer = Buffer.from(data);
data = [ 0x9A, 0x99, 0x99, 0x99, 0x99, 0x99, 0xB9, 0x3F ]; // 0.1 LE
array = new Uint8Array(data);
buffer = Buffer.from(data);
// raw double read speed
newSuite("double")
@@ -103,6 +101,7 @@ function readString(bytes) {
}
return String.fromCharCode.apply(String, out.slice(0, c));
}
return "";
}
// raw string read speed
@@ -13,29 +13,25 @@ function newSuite(name) {
benches.push(event.target);
})
.on("start", function() {
console.log("benchmarking " + name + " performance ...\n");
})
.on("error", function(err) {
console.log("ERROR:", err);
process.stdout.write("benchmarking " + name + " performance ...\n\n");
})
.on("cycle", function(event) {
console.log(String(event.target));
process.stdout.write(String(event.target) + "\n");
})
.on("complete", function(event) {
.on("complete", function() {
if (benches.length > 1) {
var fastest = this.filter('fastest'),
slowest = this.filter('slowest');
var fastestHz = getHz(fastest[0]);
console.log("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest"));
var fastest = this.filter("fastest"), // eslint-disable-line no-invalid-this
fastestHz = getHz(fastest[0]);
process.stdout.write("\n" + chalk.white(pad(fastest[0].name, padSize)) + " was " + chalk.green("fastest") + "\n");
benches.forEach(function(bench) {
if (fastest.indexOf(bench) === 0)
return;
var hz = hz = getHz(bench);
var percent = (1 - (hz / fastestHz)) * 100;
console.log(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1)+'% slower'));
process.stdout.write(chalk.white(pad(bench.name, padSize)) + " was " + chalk.red(percent.toFixed(1) + "% slower") + "\n");
});
}
console.log();
process.stdout.write("\n");
});
}
@@ -1,5 +1,5 @@
var protobuf = require(".."),
newSuite = require("./suite"),
"use strict";
var newSuite = require("./suite"),
ieee754 = require("../lib/ieee754");
// This benchmark compares raw data type performance of Uint8Array and Buffer.
@@ -96,8 +96,8 @@ newSuite("double")
.run();
var source = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
var array = new Uint8Array(16);
var buffer = new Buffer(16);
array = new Uint8Array(16);
buffer = new Buffer(16);
// raw bytes write speed
newSuite("bytes")
@@ -143,7 +143,7 @@ function writeString(buf, pos, val) {
}
}
function byteLength(val) {
/* function byteLength(val) {
var strlen = val.length >>> 0;
var len = 0;
for (var i = 0; i < strlen; ++i) {
@@ -159,10 +159,10 @@ function byteLength(val) {
len += 3;
}
return len;
}
} */
var array = new Uint8Array(1000);
var buffer = new Buffer(1000);
array = new Uint8Array(1000);
buffer = new Buffer(1000);
[
"Lorem ipsu",
BIN +3.69 KB pbjs.png
Binary file not shown.
@@ -667,5 +667,7 @@ function parse(source, root, options) {
* @param {string} source Source contents
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {ParserResult} Parser result
* @property {string} filename=null Currently processing file name for error reporting, if known
* @property {ParseOptions} defaults Default {@link ParseOptions}
* @variation 2
*/

0 comments on commit 0643f93

Please sign in to comment.