Skip to content

Commit 7265847

Browse files
authored
Update perf tests and unify them with fluent-rs benches (#346)
* Update perf tests and unify them with fluent-rs benches * Address reviewers feedback * Add missing ftl files * Retain --harmny-async-iteration in node v8 * Address feedback * Move data to benchmarks.json and into fixtures dir
1 parent e84e231 commit 7265847

10 files changed

+1656
-117
lines changed

tools/perf/benchmark.common.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
function runTests(env) {
3+
const testData = JSON.parse(env.readFile("./fixtures/benchmarks.json"));
4+
5+
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`);
17+
18+
{
19+
const testName = "parse-syntax";
20+
let start = env.now();
21+
const ast = env.FluentSyntax.parse(ftlCode);
22+
let end = env.now();
23+
24+
if (ast.body.some(elem => elem.type == "Junk")) {
25+
throw Error("Junk in syntax parser result!");
26+
}
27+
28+
results[`${testName}/"${name}"`] = env.ms(end) - env.ms(start);
29+
}
30+
31+
let resource;
32+
{
33+
const testName = "parse-runtime";
34+
let start = env.now();
35+
resource = env.Fluent.FluentResource.fromString(ftlCode);
36+
let end = env.now();
37+
38+
// we don't report any runtime parser errors, so
39+
// we'll rely on the syntax parser to verify that
40+
// the sample test syntax is correct.
41+
42+
results[`${testName}/"${name}"`] = env.ms(end) - env.ms(start);
43+
}
44+
45+
{
46+
const testName = "resolve-runtime";
47+
const functions = {};
48+
for (let fnName in fncs) {
49+
let body = fncs[fnName];
50+
functions[fnName] = new Function(body);
51+
}
52+
const bundle = new env.Fluent.FluentBundle('en-US', {
53+
functions
54+
});
55+
const errors = bundle.addResource(resource);
56+
57+
let start = env.now();
58+
for (const [id, message] of bundle.messages) {
59+
bundle.format(message, args, errors);
60+
if (message.attrs) {
61+
for (const attrName in message.attrs) {
62+
bundle.format(message.attrs[attrName], args, errors)
63+
}
64+
}
65+
}
66+
let end = env.now();
67+
68+
if (errors.length > 0) {
69+
throw new Error(`Errors accumulated while resolving ${name}.`);
70+
}
71+
72+
results[`${testName}/"${name}"`] = env.ms(end) - env.ms(start);
73+
}
74+
}
75+
76+
if (typeof exports !== "undefined") {
77+
exports.runTest = runTest;
78+
exports.runTests = runTests;
79+
}

tools/perf/benchmark.d8.js

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
load('../../fluent/fluent.js');
22
load('../../fluent-syntax/fluent-syntax.js');
3-
4-
var ftlCode = read('./workload-low.ftl');
5-
var args = {}
6-
7-
function ms(time) {
8-
// time is in milliseconds with decimals
9-
return Math.round(time * 1e3) / 1e3;
10-
}
11-
12-
var times = {};
13-
14-
times.ftlParseStart = Date.now();
15-
var resource = FluentSyntax.parse(ftlCode);
16-
times.ftlParseEnd = Date.now();
17-
18-
times.ftlEntriesParseStart = Date.now();
19-
var resource = Fluent.FluentResource.fromString(ftlCode);
20-
times.ftlEntriesParseEnd = Date.now();
21-
22-
var bundle = new Fluent.FluentBundle('en-US');
23-
var errors = bundle.addMessages(ftlCode);
24-
25-
times.format = Date.now();
26-
for (const [id, message] of bundle.messages) {
27-
bundle.format(message, args, errors);
28-
if (message.attrs) {
29-
for (const name in message.attrs) {
30-
bundle.format(message.attrs[name], args, errors)
31-
}
32-
}
33-
}
34-
times.formatEnd = Date.now();
35-
36-
var results = {
37-
"parse full AST (ms)": ms(times.ftlParseEnd - times.ftlParseStart),
38-
"parse runtime AST (ms)": ms(times.ftlEntriesParseEnd - times.ftlEntriesParseStart),
39-
"format (ms)": ms(times.formatEnd - times.format),
3+
load('./benchmark.common.js');
4+
5+
const env = {
6+
readFile: (path) => {
7+
return read(path);
8+
},
9+
ms: (milliseconds) => {
10+
return milliseconds;
11+
},
12+
now: Date.now,
13+
FluentSyntax,
14+
Fluent,
4015
};
4116

17+
const results = runTests(env);
18+
4219
print(JSON.stringify(results));

tools/perf/benchmark.jsshell.js

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
load('../../fluent/fluent.js');
22
load('../../fluent-syntax/fluent-syntax.js');
3-
4-
var ftlCode = read('./workload-low.ftl');
5-
var args = {};
6-
7-
function ms(time) {
8-
// time is in milliseconds with decimals
9-
return Math.round(time * 1e3) / 1e3;
10-
}
11-
12-
var times = {};
13-
14-
times.ftlParseStart = dateNow();
15-
var resource = FluentSyntax.parse(ftlCode);
16-
times.ftlParseEnd = dateNow();
17-
18-
times.ftlEntriesParseStart = dateNow();
19-
var resource = Fluent.FluentResource.fromString(ftlCode);
20-
times.ftlEntriesParseEnd = dateNow();
21-
22-
var bundle = new Fluent.FluentBundle('en-US');
23-
var errors = bundle.addMessages(ftlCode);
24-
25-
times.format = dateNow();
26-
for (const [id, message] of bundle.messages) {
27-
bundle.format(message, args, errors);
28-
if (message.attrs) {
29-
for (const name in message.attrs) {
30-
bundle.format(message.attrs[name], args, errors)
31-
}
32-
}
33-
}
34-
times.formatEnd = dateNow();
35-
36-
var results = {
37-
"parse full AST (ms)": ms(times.ftlParseEnd - times.ftlParseStart),
38-
"parse runtime AST (ms)": ms(times.ftlEntriesParseEnd - times.ftlEntriesParseStart),
39-
"format (ms)": ms(times.formatEnd - times.format),
3+
load('./benchmark.common.js');
4+
5+
const env = {
6+
readFile: (path) => {
7+
return read(path);
8+
},
9+
ms: (milliseconds) => {
10+
return milliseconds;
11+
},
12+
now: performance.now,
13+
FluentSyntax,
14+
Fluent,
4015
};
4116

17+
const results = runTests(env);
18+
4219
print(JSON.stringify(results));

tools/perf/benchmark.node.js

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,21 @@
1-
var fs = require('fs');
2-
3-
require('../../fluent-intl-polyfill');
4-
var Fluent = require('../../fluent');
5-
var FluentSyntax = require('../../fluent-syntax');
6-
7-
var ftlCode = fs.readFileSync(__dirname + '/workload-low.ftl').toString();
8-
9-
var args = {};
10-
11-
function ms([seconds, nanoseconds]) {
12-
return Math.round((seconds * 1e9 + nanoseconds) / 1e3) / 1e3;
13-
}
14-
15-
var cumulative = {};
16-
var start = process.hrtime();
17-
18-
cumulative.ftlParseStart = process.hrtime(start);
19-
var resource = FluentSyntax.parse(ftlCode);
20-
cumulative.ftlParseEnd = process.hrtime(start);
21-
22-
cumulative.ftlEntriesParseStart = process.hrtime(start);
23-
var resource = Fluent.FluentResource.fromString(ftlCode);
24-
cumulative.ftlEntriesParseEnd = process.hrtime(start);
25-
26-
var bundle = new Fluent.FluentBundle('en-US');
27-
var errors = bundle.addMessages(ftlCode);
1+
const fs = require('fs');
2+
3+
const Fluent = require('../../fluent');
4+
const FluentSyntax = require('../../fluent-syntax');
5+
const { runTest, runTests } = require('./benchmark.common');
6+
7+
const env = {
8+
readFile: (path) => {
9+
return fs.readFileSync(`${__dirname}/${path}`).toString();
10+
},
11+
ms: ([seconds, nanoseconds]) => {
12+
return seconds * 1e3 + nanoseconds / 1e6;
13+
},
14+
now: process.hrtime,
15+
FluentSyntax,
16+
Fluent,
17+
};
2818

29-
cumulative.format = process.hrtime(start);
30-
for (const [id, message] of bundle.messages) {
31-
bundle.format(message, args, errors);
32-
if (message.attrs) {
33-
for (const name in message.attrs) {
34-
bundle.format(message.attrs[name], args, errors)
35-
}
36-
}
37-
}
38-
cumulative.formatEnd = process.hrtime(start);
19+
const results = runTests(env);
3920

40-
var results = {
41-
"parse full AST (ms)": ms(cumulative.ftlParseEnd) - ms(cumulative.ftlParseStart),
42-
"parse runtime AST (ms)": ms(cumulative.ftlEntriesParseEnd) - ms(cumulative.ftlEntriesParseStart),
43-
"format (ms)": ms(cumulative.formatEnd) - ms(cumulative.format),
44-
};
4521
console.log(JSON.stringify(results));

tools/perf/fixtures/benchmarks.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"simple": {
3+
"args": {},
4+
"functions": {}
5+
},
6+
"preferences": {
7+
"args": {
8+
"name": "John",
9+
"tabCount": 5,
10+
"count": 3,
11+
"version": "65.0",
12+
"path": "/tmp",
13+
"num": 4,
14+
"email": "[email protected]",
15+
"value": 4.5,
16+
"unit": "mb",
17+
"service-name": "Mozilla Disk"
18+
},
19+
"functions": {
20+
"PLATFORM": "return 'linux';"
21+
}
22+
},
23+
"menubar": {
24+
"args": {},
25+
"functions": {}
26+
},
27+
"workload-low": {
28+
"args": {},
29+
"functions": {}
30+
}
31+
}

0 commit comments

Comments
 (0)