Skip to content

Commit

Permalink
Other: Some cleanup and added a logo
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 3, 2017
1 parent cba46c3 commit 0643f93
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -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/)).
Expand Down
28 changes: 15 additions & 13 deletions bench/alloc.js
@@ -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,
Expand All @@ -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();

Expand Down
20 changes: 13 additions & 7 deletions bench/index.js
@@ -1,3 +1,5 @@
"use strict";

var protobuf = require(".."),
newSuite = require("./suite"),
data = require("./bench.json");
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
})
Expand Down
31 changes: 17 additions & 14 deletions bench/prof.js
@@ -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
Expand All @@ -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(); };
Expand All @@ -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);
13 changes: 6 additions & 7 deletions bench/read.js
@@ -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);

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -103,6 +101,7 @@ function readString(bytes) {
}
return String.fromCharCode.apply(String, out.slice(0, c));
}
return "";
}

// raw string read speed
Expand Down
20 changes: 8 additions & 12 deletions bench/suite.js
Expand Up @@ -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");
});
}

Expand Down
16 changes: 8 additions & 8 deletions bench/write.js
@@ -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.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand All @@ -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",
Expand Down
Binary file added pbjs.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/parse.js
Expand Up @@ -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.