Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's up with the benchmarks? :( #48

Closed
ghost opened this issue Apr 22, 2016 · 2 comments
Closed

What's up with the benchmarks? :( #48

ghost opened this issue Apr 22, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 22, 2016

Here is code:


var avro = require('avsc');





var os = require('os');

console.log(" OS: " + os.type() + " " + os.release() + " (" + os.arch() + ")");
console.log("RAM: " + os.totalmem()/1048576 + " MB (total), " + os.freemem()/1048576 + " MB (free)");
console.log("CPU: " + os.cpus()[0].speed + " MHz " + os.cpus()[0].model);

for(var r = 1; r < 2; r++) {
  console.log("\nRun #" + r + ":");
  // var obj = {'abcdef' : 1, 'qqq' : 13, '19' : [1, 2, 3, 4]};

  var obj = {
  name: 'Pet',
  type: 'record',
  fields: [
    {name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
    {name: 'name', type: 'string'}
  ]
};



  var start = Date.now();
  for(var i = 0; i < 500000; i++) {
    (JSON.stringify(obj));
  }
  var stop = Date.now();
  console.log("\t      JSON: " + (stop - start) + "ms");

  start = Date.now();
  for(var i = 0; i < 500000; i++) {
     avro.parse(obj);


  }
  stop = Date.now();
  console.log("\t      Avro: " + (stop - start) + "ms");
}

Results:

Run #1:
JSON: 1216ms
Avro: 30162ms

I thought Avro was going to perform faster than JSON?

@mtth
Copy link
Owner

mtth commented Apr 22, 2016

There are several things wrong with your benchmark.

The first and most important is that you're not comparing equivalent operations: you should be comparing JSON.parse with type.toBuffer or type.encode. avro.parse generates a type from a schema, it's a pre-processing step which should only be done once per schema.

The other major problem is that micro-benchmarks are tricky to get right. v8 has many optimizations which can render your benchmark meaningless. If we tweak your code to remove any obvious pitfalls (see below), we get:

JSON: 2239ms
Avro: 2200ms

var avro = require('avsc');

var schema = {
  name: 'Pet',
  type: 'record',
  fields: [
    {name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
    {name: 'age', type: 'int'} // To simplify preventing getting hoisted out of the loop.
  ]
};

var type = avro.parse(schema); // Done once upfront.

var start = Date.now();
for (var i = 0; i < 5000000; i++) {
  if (JSON.stringify({age: i, kind: 'CAT'}).length < 2) {
    throw new Error();
  }
}
var stop = Date.now();
console.log("\t      JSON: " + (stop - start) + "ms");

start = Date.now();
for (var i = 0; i < 5000000; i++) {
  if (type.toBuffer({age: i, kind: 'CAT'}).length < 2) {
    throw new Error();
  }
}
stop = Date.now();
console.log("\t      Avro: " + (stop - start) + "ms");

@mtth mtth closed this as completed Apr 22, 2016
@ghost
Copy link
Author

ghost commented Apr 22, 2016

Awww. I'm an idiot. Well, I'll be using Avro from now on then :) Thanks for the explanation haha.

Cannot wait to get this puppy across our binary websockets, she will fly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant