1 /* eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */
4 var Benchmark
= require('benchmark');
5 var benchmarks
= require('beautify-benchmark');
6 var metrics
= require('datadog-metrics');
7 var os
= require('os');
8 var path
= require('path');
10 var benchmarkName
= path
.basename(module
.parent
.filename
, '.js');
12 var submitToDatadog
= process
.env
.DATADOG_API_KEY
;
14 if (submitToDatadog
) {
15 metrics
.init({ prefix
: 'build.benchmarks.' });
18 function submitDatadogMetrics(stats
) {
19 var tags
= ['benchmark_host:' + os
.hostname()]; // Can't use host
20 if (process
.env
.GIT_BRANCH
) tags
.push('branch:' + process
.env
.GIT_BRANCH
);
22 metrics
.gauge(benchmarkName
+ '.' + stats
.name
, stats
.mean
, tags
);
26 function targetStats(target
) {
30 size
: target
.stats
.sample
.length
,
31 mean
: target
.stats
.mean
35 process
.on('uncaughtException', function(err
) {
37 console
.error(err
.stack
);
41 module
.exports
= function makeBenchmark(options
) {
42 var suite
= new Benchmark
.Suite();
44 Object
.keys(options
.tests
).forEach(function(name
) {
45 var fn
= options
.tests
[name
];
47 if (fn
.length
=== 0) {
49 maxTime
: options
.maxTime
|| 2,
50 initCount
: options
.initCount
|| 1,
55 maxTime
: options
.maxTime
|| 2,
56 initCount
: options
.initCount
|| 1,
58 fn: function(deferred
) {
68 suite
.on('cycle', function(event
) {
69 var target
= event
.target
;
70 if (submitToDatadog
) {
71 submitDatadogMetrics(targetStats(target
));
74 benchmarks
.add(target
);
77 suite
.on('complete', function() {
78 function doAfter(callback
) {
79 if (!options
.after
) return callback();
81 if (options
.after
.length
=== 0) {
90 options
.after(callback
);
96 if (submitToDatadog
) {
97 // Give datadog a second to flush
98 setTimeout(function() {
107 var before
= options
.before
;
110 suite
.run({ async
: true });
114 if (before
.length
=== 0) {
116 suite
.run({ async
: true });
120 before(function(err
) {
122 suite
.run({ async
: true });