Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Add a benchmark assert option to pre-run and test that it returns an expected result. #8

Closed
jdalton opened this issue Dec 9, 2011 · 10 comments

Comments

@jdalton
Copy link
Member

jdalton commented Dec 9, 2011

Add a Benchmark assert option to pre-run and test that it returns an expected result.
This can be useful to ensure snippets being tested at least return the same result.

@ghost ghost assigned jdalton Dec 9, 2011
@jdalton
Copy link
Member Author

jdalton commented Dec 10, 2011

Maybe something like

var bench = Benchmark('foo', {
  'assert': 6,
  'fn': function() {
    var a = 3 + 3;
    return a;
  }
});

If it didn't match it would error and call the onError event handlers for the benchmark and assign the error to bench.error.
The assert option would also be able to compare complex objects like arrays and objects with circular references.

@mathiasbynens
Copy link
Member

I’ve been asked to add this as a feature to jsPerf in the past. I’m not sure how to create a good UI for it (I’m afraid things will become too bloated) but this would be good to have in Benchmark.js for sure!

@subtleGradient
Copy link

I've been demanding this since day one. Just some basic sanity check to make sure that each test is actually doing what we expect

@jdalton
Copy link
Member Author

jdalton commented Dec 10, 2011

@subtleGradient Any preference on option name or is assert fine?

If no one as any major objection I will probably begin to add this into Benchmark.js.

@rvagg
Copy link

rvagg commented Dec 10, 2011

Nice, I always get an awkward feeling that perhaps my benchmarks are running code that doesn't quite make sense or are checking something other than intended.
I just wish I could offer better name than assert, it doesn't seem appropriate in this context, I'd rather it be left to the domain of unit testing, it's more of a sanityCheck or something like that.

@subtleGradient
Copy link

Assert sounds good to me

@mathiasbynens
Copy link
Member

+1 on naming it assert. </bikeshed>

@jdalton
Copy link
Member Author

jdalton commented Dec 12, 2011

Other names could be: preflight, precheck, or pretest.

I'll keep this ticket open for ideas.
Even if I don't end up adding it into Benchmark.js I think I'll still look into doing a deep object compare for the fun of it.

@andrewdeandrade
Copy link

Whatever approach is taken, it would need to work with async tests as well. The defer.resolve() method would have to accept an argument.

Another approach would be to forego adding an assert equality check at all and instead have the user provide a function where they can use the assertion library of their choice and if the assertion fails, the user can call a method to abort the Benchmark. A functional approach allows the user more flexibility on the types and quantity of assertions desired.

@jdalton
Copy link
Member Author

jdalton commented Mar 21, 2014

Yap, I'm punting this enhancement.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

5 participants