Skip to content

Commit fc90148

Browse files
committed
Automatic profile generation and processing [ci skip]
1 parent 2a2f6dc commit fc90148

File tree

5 files changed

+73
-10
lines changed

5 files changed

+73
-10
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,22 @@ Documentation
282282

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

285+
### Data type recommendations
286+
287+
| Value type | protobuf Type | Size / Notes
288+
|--------------------------|---------------|-----------------------------------------------------------------------------------
289+
| Unsigned 32 bit integers | uint32 | 1 to 5 bytes.
290+
| Signed 32 bit integers | sint32 | 1 to 5 bytes. Do not use int32 (always encodes negative values as 10 bytes).
291+
| Unsigned 52 bit integers | uint64 | 1 to 10 bytes.
292+
| Signed 52 bit integers | sint64 | 1 to 10 bytes. Do not use int64 (always encodes negative values as 10 bytes).
293+
| Unsigned 64 bit integers | uint64 | Use with long.js. 1 to 10 bytes.
294+
| Signed 64 bit integers | sint64 | Use with long.js. 1 to 10 bytes. Do not use int64 (always encodes negative values as 10 bytes).
295+
| 32 bit precision floats | float | 4 bytes.
296+
| 64 bit precision floats | double | 8 bytes. Use float if 32 bits of precision are enough.
297+
| Boolean values | bool | 1 byte.
298+
| Strings | string | 1 byte + utf8 byte length.
299+
| Buffers | bytes | 1 byte + byte length.
300+
285301
Command line
286302
------------
287303

bench/prof.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,44 @@
11
var fs = require("fs"),
22
path = require("path");
3-
var protobuf = require("..");
43

5-
// A profiling stub to measure just raw encoding / decoding performance using benchmark data.
4+
// A profiling stub to measure encoding / decoding performance using benchmark data.
5+
6+
var commands = ["encode", "decode", "encode-browser", "decode-browser"];
7+
if (commands.indexOf(process.argv[2]) < 0) { // 0: node, 1: prof.js
8+
console.error("usage: " + path.basename(process.argv[1]) + " <" + commands.join('|') + "> [iterations=10000000]");
9+
process.exit(0);
10+
}
11+
12+
// Spin up a node process with profiling enabled and process the generated log
13+
if (process.execArgv.indexOf("--prof") < 0) {
14+
console.log("cleaning up old logs ...");
15+
var child_process = require("child_process");
16+
var logRe = /^isolate\-[0-9A-F]+\-v8\.log$/;
17+
fs.readdirSync(process.cwd()).forEach(function(file) {
18+
if (logRe.test(file))
19+
fs.unlink(file);
20+
});
21+
console.log("generating profile (may take a while) ...");
22+
child_process.execSync("node --prof --trace-deopt " + process.argv.slice(1).join(' '), {
23+
cwd: process.cwd(),
24+
stdio: 'inherit'
25+
});
26+
console.log("processing profile ...");
27+
fs.readdirSync(process.cwd()).forEach(function(file) {
28+
if (logRe.test(file)) {
29+
child_process.execSync("node --prof-process " + file, {
30+
cwd: process.cwd(),
31+
stdio: 'inherit'
32+
});
33+
fs.unlink(file);
34+
}
35+
});
36+
console.log("done.");
37+
process.exit(0);
38+
}
39+
40+
// Actual profiling code
41+
var protobuf = require("..");
642

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

1854
switch (process.argv[2]) {
19-
default:
20-
console.error("usage: " + path.basename(process.argv[1]) + " <encode|decode|encode-browser|decode-browser> [iterations=10000000]");
21-
process.exit(1);
22-
return;
2355
case "encode-browser":
2456
setupBrowser();
2557
case "encode":

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
3434
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
3535
"bench": "node bench",
36-
"bench-read": "node bench/read",
37-
"bench-write": "node bench/write",
38-
"all": "npm run lint && npm run test && npm run build && npm run types && npm run docs && npm run bench",
36+
"prof": "node bench/prof",
3937
"prepublish": "node scripts/prepublish"
4038
},
4139
"optionalDependencies": {

src/writer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ function BufferWriter() {
563563
BufferWriter.alloc = function alloc_buffer(size) {
564564
BufferWriter.alloc = util.Buffer.allocUnsafe
565565
? util.Buffer.allocUnsafe
566-
: function allocUnsafe(size) { return new util.Buffer(size); };
566+
: function allocUnsafeNew(size) { return new util.Buffer(size); };
567567
return BufferWriter.alloc(size);
568568
};
569569

tests/empty.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var tape = require("tape");
2+
3+
var protobuf = require("..");
4+
5+
tape.test("empty messages", function(test) {
6+
var root = new protobuf.Root().addJSON({
7+
"Test": {
8+
fields: {}
9+
}
10+
});
11+
12+
var Test = root.lookup("Test");
13+
var buf = Test.encodeDelimited({}).finish();
14+
test.equal(buf.length, 1, "should encodeDelimited to a buffer of length 1");
15+
test.equal(buf[0], 0, "should encodeDelimited a length of 0");
16+
test.end();
17+
});

0 commit comments

Comments
 (0)