Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add benchmarks for issue #39 #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Multi-parser benchmarks
=======================

`node run.js` will run all benchmarks.

`node run.js file.json ...` will run just the named benchmarks.

`node run.js parsers=a,b,...` will run just the named parsers. If
`parsers=` is specified more than once, the values are combined.

Each benchmark is a collection of files that are equivalent
representations of the same data in different formats:

- `{name}.json` is the expected result.
- `{name}.ini` is the data in ini format.
- `{name}.toml` is the data in toml format.
- `{name}.yaml` is the data in yaml format.

Only the `.json` file is required. A parser will only be
benchmarked if there's an appropriate input file for it.
126 changes: 126 additions & 0 deletions bench/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict';

const Benchmark = require('benchmark');
const deepEqual = require('deep-equal');
const fs = require('fs');
const glob = require('glob');
const upath = require('upath');

const ini = require('ini');
const jsYaml = require('js-yaml');
const tomlJ = require('toml-j0.4');
const tomlNode = require('../index.js');
const yaml = require('yaml');
const yamljs = require('yamljs');

const parsers = [
{
name: 'ini',
format: 'ini',
parse: (str) => ini.parse(str),
},
{
name: 'js-yaml',
format: 'yaml',
parse: (str) => jsYaml.safeLoad(str),
},
// js-yaml can reliably parse json. the others, not so much.
{
name: 'js-yaml(json)',
format: 'json',
parse: (str) => jsYaml.safeLoad(str),
},
{
name: 'json',
format: 'json',
parse: (str) => JSON.parse(str),
},
{
name: 'toml-j0.4',
format: 'toml',
parse: (str) => tomlJ.parse(str),
},
{
name: 'toml-node',
format: 'toml',
parse: (str) => tomlNode.parse(str),
},
{
name: 'yaml',
format: 'yaml',
parse: (str) => yaml.eval(str),
},
{
name: 'yamljs',
format: 'yaml',
parse: (str) => yamljs.parse(str),
},
];

const formats = new Set(parsers.map((p) => p.format));

const longestName = parsers.reduce((a, p) => Math.max(a, p.name.length), 0);

function benchAll(onlyParsers) {
for (let fname of glob.sync(upath.join(__dirname, '*.json'))) {
benchOne(fname, onlyParsers);
}
}

function benchOne(name, onlyParsers) {
if (onlyParsers && !onlyParsers.size) {
onlyParsers = null;
}

let suite = new Benchmark.Suite();
let example = loadExample(name);
for (let parser of parsers) {
if (onlyParsers && !onlyParsers.has(parser.name)) {
continue;
}

let benchName = `parse ${example.name} with ` + parser.name.padEnd(longestName, ' ');

let input = example[parser.format];
if (input === undefined) {
console.log(`${benchName} skipped`);
continue;
}

// Verify the parser works correctly.
let expected = JSON.parse(example.json);
let actual;
try {
actual = parser.parse(input);
} catch (e) {
actual = String(e);
}
if (!deepEqual(actual, expected)) {
console.log(`${benchName} broken`);
console.error(' actual:\n', actual);
continue;
}

suite.add(benchName, () => parser.parse(input));
}

suite.on('cycle', (event) => console.log(String(event.target)));
suite.run();
}

function loadExample(name) {
let path = upath.parse(name);
let example = {
name: path.name,
};
for (let format of formats) {
let f = upath.join(path.dir, path.name + '.' + format);
if (fs.existsSync(f)) {
example[format] = fs.readFileSync(f, 'utf-8');
}
}
return example;
}

exports.benchOne = benchOne;
exports.benchAll = benchAll;
108 changes: 108 additions & 0 deletions bench/cargo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"package": {
"name": "cargo",
"version": "0.23.0",
"authors": [
"Yehuda Katz <[email protected]>",
"Carl Lerche <[email protected]>",
"Alex Crichton <[email protected]>"
],
"license": "MIT/Apache-2.0",
"homepage": "https://crates.io",
"repository": "https://github.com/rust-lang/cargo",
"documentation": "https://docs.rs/cargo",
"description": "Cargo, a package manager for Rust.\n"
},
"lib": {
"name": "cargo",
"path": "src/cargo/lib.rs"
},
"dependencies": {
"atty": "0.2",
"crates-io": {
"path": "src/crates-io",
"version": "0.12"
},
"crossbeam": "0.2",
"curl": "0.4.6",
"docopt": "0.8.1",
"env_logger": "0.4",
"error-chain": "0.11.0-rc.2",
"filetime": "0.1",
"flate2": "0.2",
"fs2": "0.4",
"git2": "0.6",
"git2-curl": "0.7",
"glob": "0.2",
"hex": "0.2",
"home": "0.3",
"ignore": "^0.2.2",
"jobserver": "0.1.6",
"libc": "0.2",
"libgit2-sys": "0.6",
"log": "0.3",
"num_cpus": "1.0",
"same-file": "0.1",
"scoped-tls": "0.1",
"semver": {
"version": "0.7.0",
"features": [
"serde"
]
},
"serde": "1.0",
"serde_derive": "1.0",
"serde_ignored": "0.0.3",
"serde_json": "1.0",
"shell-escape": "0.1",
"tar": {
"version": "0.4",
"default-features": false
},
"tempdir": "0.3",
"termcolor": "0.3",
"toml": "0.4",
"url": "1.1"
},
"target": {
"cfg(unix)": {
"dependencies": {
"openssl": "0.9"
}
},
"cfg(target_os = 'macos')": {
"dependencies": {
"core-foundation": {
"version": "0.4.4",
"features": [
"mac_os_10_7_support"
]
}
}
},
"cfg(windows)": {
"dependencies": {
"advapi32-sys": "0.2",
"kernel32-sys": "0.2",
"miow": "0.2",
"psapi-sys": "0.1",
"winapi": "0.2"
}
}
},
"dev-dependencies": {
"bufstream": "0.1",
"cargotest": {
"path": "tests/cargotest"
},
"filetime": "0.1",
"hamcrest": "=0.1.1"
},
"bin": [
{
"name": "cargo",
"test": false,
"doc": false
}
]
}
77 changes: 77 additions & 0 deletions bench/cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[package]
name = "cargo"
version = "0.23.0"
authors = ["Yehuda Katz <[email protected]>",
"Carl Lerche <[email protected]>",
"Alex Crichton <[email protected]>"]
license = "MIT/Apache-2.0"
homepage = "https://crates.io"
repository = "https://github.com/rust-lang/cargo"
documentation = "https://docs.rs/cargo"
description = """
Cargo, a package manager for Rust.
"""

[lib]
name = "cargo"
path = "src/cargo/lib.rs"

[dependencies]
atty = "0.2"
crates-io = { path = "src/crates-io", version = "0.12" }
crossbeam = "0.2"
curl = "0.4.6"
docopt = "0.8.1"
env_logger = "0.4"
error-chain = "0.11.0-rc.2"
filetime = "0.1"
flate2 = "0.2"
fs2 = "0.4"
git2 = "0.6"
git2-curl = "0.7"
glob = "0.2"
hex = "0.2"
home = "0.3"
ignore = "^0.2.2"
jobserver = "0.1.6"
libc = "0.2"
libgit2-sys = "0.6"
log = "0.3"
num_cpus = "1.0"
same-file = "0.1"
scoped-tls = "0.1"
semver = { version = "0.7.0", features = ["serde"] }
serde = "1.0"
serde_derive = "1.0"
serde_ignored = "0.0.3"
serde_json = "1.0"
shell-escape = "0.1"
tar = { version = "0.4", default-features = false }
tempdir = "0.3"
termcolor = "0.3"
toml = "0.4"
url = "1.1"

[target."cfg(unix)".dependencies]
openssl = "0.9"

[target."cfg(target_os = 'macos')".dependencies]
core-foundation = { version = "0.4.4", features = ["mac_os_10_7_support"] }

[target."cfg(windows)".dependencies]
advapi32-sys = "0.2"
kernel32-sys = "0.2"
miow = "0.2"
psapi-sys = "0.1"
winapi = "0.2"

[dev-dependencies]
bufstream = "0.1"
cargotest = { path = "tests/cargotest" }
filetime = "0.1"
hamcrest = "=0.1.1"

[[bin]]
name = "cargo"
test = false
doc = false
Loading