Skip to content

Commit

Permalink
Automatic profile generation and processing [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 6, 2016
1 parent 2a2f6dc commit fc90148
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ Documentation

* [protobuf.js API Documentation](http://dcode.io/protobuf.js/)

### Data type recommendations

| Value type | protobuf Type | Size / Notes
|--------------------------|---------------|-----------------------------------------------------------------------------------
| Unsigned 32 bit integers | uint32 | 1 to 5 bytes.
| Signed 32 bit integers | sint32 | 1 to 5 bytes. Do not use int32 (always encodes negative values as 10 bytes).
| Unsigned 52 bit integers | uint64 | 1 to 10 bytes.
| Signed 52 bit integers | sint64 | 1 to 10 bytes. Do not use int64 (always encodes negative values as 10 bytes).
| Unsigned 64 bit integers | uint64 | Use with long.js. 1 to 10 bytes.
| Signed 64 bit integers | sint64 | Use with long.js. 1 to 10 bytes. Do not use int64 (always encodes negative values as 10 bytes).
| 32 bit precision floats | float | 4 bytes.
| 64 bit precision floats | double | 8 bytes. Use float if 32 bits of precision are enough.
| Boolean values | bool | 1 byte.
| Strings | string | 1 byte + utf8 byte length.
| Buffers | bytes | 1 byte + byte length.

Command line
------------

Expand Down
44 changes: 38 additions & 6 deletions bench/prof.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
var fs = require("fs"),
path = require("path");
var protobuf = require("..");

// A profiling stub to measure just raw encoding / decoding performance using benchmark data.
// 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);
}

// 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 ...");
var child_process = require("child_process");
var logRe = /^isolate\-[0-9A-F]+\-v8\.log$/;
fs.readdirSync(process.cwd()).forEach(function(file) {
if (logRe.test(file))
fs.unlink(file);
});
console.log("generating profile (may take a while) ...");
child_process.execSync("node --prof --trace-deopt " + process.argv.slice(1).join(' '), {
cwd: process.cwd(),
stdio: 'inherit'
});
console.log("processing profile ...");
fs.readdirSync(process.cwd()).forEach(function(file) {
if (logRe.test(file)) {
child_process.execSync("node --prof-process " + file, {
cwd: process.cwd(),
stdio: 'inherit'
});
fs.unlink(file);
}
});
console.log("done.");
process.exit(0);
}

// Actual profiling code
var protobuf = require("..");

var root = protobuf.parse(fs.readFileSync(require.resolve("../bench/bench.proto")).toString("utf8")).root;
var Test = root.lookup("Test");
Expand All @@ -16,10 +52,6 @@ function setupBrowser() {
}

switch (process.argv[2]) {
default:
console.error("usage: " + path.basename(process.argv[1]) + " <encode|decode|encode-browser|decode-browser> [iterations=10000000]");
process.exit(1);
return;
case "encode-browser":
setupBrowser();
case "encode":
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
"bench": "node bench",
"bench-read": "node bench/read",
"bench-write": "node bench/write",
"all": "npm run lint && npm run test && npm run build && npm run types && npm run docs && npm run bench",
"prof": "node bench/prof",
"prepublish": "node scripts/prepublish"
},
"optionalDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ function BufferWriter() {
BufferWriter.alloc = function alloc_buffer(size) {
BufferWriter.alloc = util.Buffer.allocUnsafe
? util.Buffer.allocUnsafe
: function allocUnsafe(size) { return new util.Buffer(size); };
: function allocUnsafeNew(size) { return new util.Buffer(size); };
return BufferWriter.alloc(size);
};

Expand Down
17 changes: 17 additions & 0 deletions tests/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var tape = require("tape");

var protobuf = require("..");

tape.test("empty messages", function(test) {
var root = new protobuf.Root().addJSON({
"Test": {
fields: {}
}
});

var Test = root.lookup("Test");
var buf = Test.encodeDelimited({}).finish();
test.equal(buf.length, 1, "should encodeDelimited to a buffer of length 1");
test.equal(buf[0], 0, "should encodeDelimited a length of 0");
test.end();
});

0 comments on commit fc90148

Please sign in to comment.