Skip to content

Commit 1a0cd74

Browse files
authored
Fix stas feedback on perf tests (#349)
* Fix stas feedback on perf tests * Address feedback
1 parent 7265847 commit 1a0cd74

File tree

6 files changed

+79
-68
lines changed

6 files changed

+79
-68
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"prettyjson": "^1.2.1",
2828
"rollup": "^0.59.1",
2929
"rollup-plugin-babel": "^4.0.0-beta.4",
30-
"rollup-plugin-node-resolve": "^3.0.3"
30+
"rollup-plugin-node-resolve": "^3.0.3",
31+
"intl-pluralrules": "^1.0.0"
3132
}
3233
}

tools/perf/benchmark.common.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
2-
function runTests(env) {
3-
const testData = JSON.parse(env.readFile("./fixtures/benchmarks.json"));
4-
1+
function runTest(env) {
52
const results = {};
6-
7-
for (let testName in testData) {
8-
let test = testData[testName];
9-
runTest(env, testName, test.args, test.functions, results);
10-
}
11-
12-
return results;
13-
}
14-
15-
function runTest(env, name, args, fncs, results) {
16-
const ftlCode = env.readFile(`./fixtures/${name}.ftl`);
3+
const testData = JSON.parse(env.readFile("./fixtures/benchmarks.json"));
4+
const {args, functions} = testData[env.benchmarkName];
5+
const ftlCode = env.readFile(`./fixtures/${env.benchmarkName}.ftl`);
176

187
{
198
const testName = "parse-syntax";
@@ -25,7 +14,7 @@ function runTest(env, name, args, fncs, results) {
2514
throw Error("Junk in syntax parser result!");
2615
}
2716

28-
results[`${testName}/"${name}"`] = env.ms(end) - env.ms(start);
17+
results[`${testName}/${env.benchmarkName}`] = env.ms(end) - env.ms(start);
2918
}
3019

3120
let resource;
@@ -39,18 +28,18 @@ function runTest(env, name, args, fncs, results) {
3928
// we'll rely on the syntax parser to verify that
4029
// the sample test syntax is correct.
4130

42-
results[`${testName}/"${name}"`] = env.ms(end) - env.ms(start);
31+
results[`${testName}/${env.benchmarkName}`] = env.ms(end) - env.ms(start);
4332
}
4433

4534
{
4635
const testName = "resolve-runtime";
47-
const functions = {};
48-
for (let fnName in fncs) {
49-
let body = fncs[fnName];
50-
functions[fnName] = new Function(body);
36+
const fncs = {};
37+
for (let fnName in functions) {
38+
let body = functions[fnName];
39+
fncs[fnName] = new Function(body);
5140
}
5241
const bundle = new env.Fluent.FluentBundle('en-US', {
53-
functions
42+
functions: fncs
5443
});
5544
const errors = bundle.addResource(resource);
5645

@@ -69,11 +58,12 @@ function runTest(env, name, args, fncs, results) {
6958
throw new Error(`Errors accumulated while resolving ${name}.`);
7059
}
7160

72-
results[`${testName}/"${name}"`] = env.ms(end) - env.ms(start);
61+
results[`${testName}/${env.benchmarkName}`] = env.ms(end) - env.ms(start);
7362
}
63+
64+
return results;
7465
}
7566

7667
if (typeof exports !== "undefined") {
7768
exports.runTest = runTest;
78-
exports.runTests = runTests;
7969
}

tools/perf/benchmark.d8.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ const env = {
99
ms: (milliseconds) => {
1010
return milliseconds;
1111
},
12+
benchmarkName: arguments[0],
1213
now: Date.now,
1314
FluentSyntax,
1415
Fluent,
1516
};
1617

17-
const results = runTests(env);
18+
const results = runTest(env);
1819

1920
print(JSON.stringify(results));

tools/perf/benchmark.jsshell.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ const env = {
99
ms: (milliseconds) => {
1010
return milliseconds;
1111
},
12+
benchmarkName: scriptArgs[0],
1213
now: performance.now,
1314
FluentSyntax,
1415
Fluent,
1516
};
1617

17-
const results = runTests(env);
18+
const results = runTest(env);
1819

1920
print(JSON.stringify(results));

tools/perf/benchmark.node.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const fs = require('fs');
22

33
const Fluent = require('../../fluent');
44
const FluentSyntax = require('../../fluent-syntax');
5-
const { runTest, runTests } = require('./benchmark.common');
5+
const { runTest } = require('./benchmark.common');
6+
require('intl-pluralrules');
67

78
const env = {
89
readFile: (path) => {
@@ -11,11 +12,12 @@ const env = {
1112
ms: ([seconds, nanoseconds]) => {
1213
return seconds * 1e3 + nanoseconds / 1e6;
1314
},
15+
benchmarkName: process.argv[2],
1416
now: process.hrtime,
1517
FluentSyntax,
1618
Fluent,
1719
};
1820

19-
const results = runTests(env);
21+
const results = runTest(env);
2022

2123
console.log(JSON.stringify(results));

tools/perf/test.js

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var commands = {
2828
"node --harmony-async-iteration benchmark.node.js" :
2929
"node benchmark.node.js",
3030
jsshell: "js benchmark.jsshell.js",
31-
d8: "d8 benchmark.d8.js",
31+
d8: "d8 benchmark.d8.js -- ",
3232
};
3333

3434
var command = program.args.length ?
@@ -47,47 +47,63 @@ function color(str, col) {
4747
return str;
4848
}
4949

50-
function runAll(sample, callback) {
51-
var results = {};
52-
var times = {};
53-
// run is recursive and thus sequential so that node doesn't spawn all the
54-
// processes at once
55-
run();
56-
57-
function run() {
58-
exec(command, { cwd: __dirname }, function (error, stdout, stderr) {
59-
if (!program.raw && program.progress) {
60-
process.stdout.write(color('.', INDICATOR));
61-
}
62-
if (error) {
63-
console.log(error.toString());
64-
}
65-
var data = JSON.parse(stdout);
66-
for (var scenario in data) {
67-
if (!times[scenario]) {
68-
times[scenario] = [];
69-
}
70-
times[scenario].push(data[scenario]);
71-
}
72-
if (times[scenario].length !== sample) {
73-
run();
74-
} else {
75-
for (scenario in times) {
76-
var mean = util.mean(times[scenario]);
77-
results[scenario] = {
78-
mean: mean,
79-
stdev: util.stdev(times[scenario], mean),
80-
sample: sample
81-
};
82-
}
83-
callback(results);
84-
}
85-
});
50+
runAll(parseInt(program.sample)).then(printResults);
51+
52+
async function runAll(sample) {
53+
const results = {};
54+
55+
const testData = JSON.parse(fs.readFileSync(`${__dirname}/fixtures/benchmarks.json`).toString());
56+
for (let benchmarkName in testData) {
57+
if (!program.raw && program.progress) {
58+
process.stdout.write(color(`\n${benchmarkName}: `, INDICATOR));
59+
}
60+
await runBenchmark(benchmarkName, sample, results);
8661
}
62+
return results;
8763
}
8864

89-
runAll(parseInt(program.sample), function(res) {
65+
function runBenchmark(benchmarkName, sample, results) {
66+
return new Promise((resolve, reject) => {
67+
const times = {};
68+
const execCommand = `${command} ${benchmarkName}`;
69+
// run is recursive and thus sequential so that node doesn't spawn all the
70+
// processes at once
71+
run();
9072

73+
function run() {
74+
exec(execCommand, { cwd: __dirname }, function (error, stdout, stderr) {
75+
if (!program.raw && program.progress) {
76+
process.stdout.write(color('.', INDICATOR));
77+
}
78+
if (error) {
79+
console.log(error.toString());
80+
}
81+
var data = JSON.parse(stdout);
82+
for (var scenario in data) {
83+
if (!times[scenario]) {
84+
times[scenario] = [];
85+
}
86+
times[scenario].push(data[scenario]);
87+
}
88+
if (times[scenario].length !== sample) {
89+
run();
90+
} else {
91+
for (scenario in times) {
92+
var mean = util.mean(times[scenario]);
93+
results[scenario] = {
94+
mean: mean,
95+
stdev: util.stdev(times[scenario], mean),
96+
sample: sample
97+
};
98+
}
99+
resolve(results);
100+
}
101+
});
102+
}
103+
});
104+
}
105+
106+
function printResults(res) {
91107
for (var scenario in res) {
92108
if (program.compare) {
93109
var ref = JSON.parse(fs.readFileSync(program.compare).toString());
@@ -123,4 +139,4 @@ runAll(parseInt(program.sample), function(res) {
123139
dashColor: 'cyan',
124140
}));
125141
}
126-
});
142+
}

0 commit comments

Comments
 (0)