1 var ProtoBuf = require("../index.js");
\r
6 password: "helloworld"
\r
9 console.log("Sample: `"+JSON.stringify(sample, null, 4));
\r
12 var proto = " message Sample {" +
\r
13 "required uint32 id = 1;" +
\r
14 "required string name = 2;" +
\r
15 "required string password = 3;" +
\r
18 var builder = ProtoBuf.loadProto(proto, "bench.proto"),
\r
19 Sample = builder.build("Sample");
\r
22 console.log("Encoding size");
\r
23 console.log("-------------");
\r
24 var jsonData = new Buffer(JSON.stringify(sample), "utf8"),
\r
25 protoData = new Sample(sample).toBuffer();
\r
26 console.log("* Encoded sample size as JSON: "+jsonData.length+" bytes");
\r
27 console.log("* Encoded sample size as protocol buffer: "+protoData.length+" bytes");
\r
30 // Compare encoding speed
\r
31 console.log("Encoding speed");
\r
32 console.log("--------------");
\r
34 // Assuming that a receive buffer is used
\r
35 var buf = ProtoBuf.ByteBuffer.allocate(64);
\r
36 var protoSample = new Sample(sample),
\r
37 n = 100000, k = (n/1000)+'k';
\r
38 console.time("* ProtoBuf encode "+k);
\r
39 for (var i=0; i<n; ++i)
\r
40 protoSample.encode(buf, true),
\r
42 console.timeEnd("* ProtoBuf encode "+k);
\r
43 console.time("* ProtoBuf decode "+k);
\r
44 for (var i=0; i<n; ++i)
\r
47 console.timeEnd("* ProtoBuf decode "+k);
\r