Pass benchmark code through Babel before serving it to the browser

This will allow to use ES2015 constructs in benchmark code.

The change required introducing a small server, which serves both PEG.js
and benchmark code passed through Babel and bundled together. This
allowed to convert the benchmark to regular modules and to get rid of
the hackery that was previously needed to make it run both in Node.js
and in the browser.

Note the benchmark no longer exercises the browser version.

See #442.
redux
David Majda 8 years ago
parent 5c40fff136
commit 959f20f6e2

@ -25,6 +25,7 @@ BROWSER_FILE_DEV = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).js
BROWSER_FILE_MIN = $(BROWSER_DIR)/peg-$(PEGJS_VERSION).min.js
SPEC_SERVER_FILE = $(SPEC_DIR)/server
BENCHMARK_SERVER_FILE = $(BENCHMARK_DIR)/server
VERSION_FILE = VERSION
@ -108,6 +109,7 @@ lint:
$(SPEC_SERVER_FILE) \
$(BENCHMARK_DIR)/*.js \
$(BENCHMARK_RUN) \
$(BENCHMARK_SERVER_FILE) \
$(PEGJS)
.PHONY: all parser browser browserclean spec benchmark lint

@ -0,0 +1,5 @@
{
"env": {
"commonjs": true
}
}

@ -39,18 +39,12 @@ All commands in the following steps need to be executed in PEG.js root directory
$ npm install
```
3. Build browser version of PEG.js:
3. Serve the benchmark suite using a web server:
```console
$ make browser
$ benchmark/server
```
4. Serve PEG.js root directory using a web server:
4. Point your browser to the [benchmark suite](http://localhost:8000/).
```console
$ node_modules/.bin/http-server
```
5. Point your browser to the [benchmark suite](http://localhost:8080/benchmark/index.html).
6. Click the **Run** button and wait for results.
5. Click the **Run** button and wait for results.

@ -1,16 +1,6 @@
/* global module */
"use strict";
(function(root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else {
root.benchmarks = factory();
}
}(this, function() {
return [
var benchmarks = [
{
id: "json",
title: "JSON",
@ -47,6 +37,6 @@
{ file: "960.gs/min/960_24_col.css", title: "960.gs - 960_24_col.css (minified)" }
]
}
];
];
}));
module.exports = benchmarks;

@ -33,11 +33,8 @@
</tr>
</table>
<script src="../browser/peg-0.10.0.js"></script>
<script src="vendor/jquery/jquery.js"></script>
<script src="vendor/jquery.scrollto/jquery.scrollTo.js"></script>
<script src="benchmarks.js"></script>
<script src="runner.js"></script>
<script src="index.js"></script>
<script src="bundle.js"></script>
</body>
</html>

@ -1,5 +1,7 @@
/* eslint-env browser, jquery */
/* global benchmarks, Runner */
var benchmarks = require("./benchmarks.js"),
Runner = require("./runner.js");
$("#run").click(function() {
"use strict";

@ -6,10 +6,9 @@
"use strict";
var fs = require("fs");
var peg = require("../lib/peg");
var benchmarks = require("./benchmarks.js");
var Runner = require("./runner.js")(peg);
var Runner = require("./runner.js");
/* Results Table Manipulation */

@ -1,16 +1,10 @@
/* global module, setTimeout */
/* global setTimeout */
"use strict";
(function(root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory;
} else {
root.Runner = factory(root.peg);
}
}(this, function(peg) {
var peg = require("../lib/peg");
return {
var Runner = {
run: function(benchmarks, runCount, options, callbacks) {
/* Queue */
@ -126,6 +120,6 @@
Q.run();
}
};
};
}));
module.exports = Runner;

@ -0,0 +1,38 @@
#!/usr/bin/env node
/* eslint-env node */
/* eslint no-console: 0 */
/*
* Small server whose main purpose is to ensure that both the benchmarked code
* and the benchmark get passed through Babel & Browserify before they are
* served to the browser.
*/
var express = require("express"),
logger = require("morgan"),
glob = require("glob"),
browserify = require("browserify"),
babelify = require("babelify");
var app = express();
app.use(logger("dev"));
app.use(express.static(__dirname));
app.use("/examples", express.static(__dirname + "/../examples"));
app.get("/bundle.js", function(req, res) {
var files = glob.sync(__dirname + "/**/*.js", {
ignore: __dirname + "/vendor/**/*"
});
browserify(files)
.transform(babelify, { presets: "es2015", compact: false })
.bundle()
.pipe(res);
});
app.listen(8000, function() {
console.log("Benchmark server running at http://localhost:8000...");
});
Loading…
Cancel
Save