Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 2e48ef9

Browse files
authored
Merge pull request #4 from lib-ruby-parser/add-benchmarks
2 parents 8d5a660 + 84a7d32 commit 2e48ef9

File tree

9 files changed

+108
-3
lines changed

9 files changed

+108
-3
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ build/nodejs-lib-ruby-parser.js
1818

1919
# gh pages
2020
gh-pages/lib-ruby-parser.js
21+
22+
# benchmark assets
23+
benchmark/repos
24+
benchmark/filelist
25+
benchmark/repos.zip
26+
benchmark/ruby-parser.rb
27+
benchmark/rust-parser
28+
benchmark/stats

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include codegen/build.mk
22
include build/build.mk
33
include tests/build.mk
44
include gh-pages/build.mk
5+
include benchmark/build.mk
56

67
clean:
78
rm -rf $(CLEAN)

benchmark/build.mk

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
define download_latest_bench_asset
2+
ASSET_NAME=$(1) SAVE_AS=$(2) ruby benchmark/download_asset.rb
3+
endef
4+
5+
benchmark/download:
6+
@echo "Downloading repos.zip..."
7+
$(call download_latest_bench_asset,repos.zip,benchmark/repos.zip)
8+
@echo "Unpacking repos.zip..."
9+
unzip --help
10+
ls -l benchmark/repos.zip
11+
unzip -q benchmark/repos.zip -d benchmark
12+
13+
@echo "Downloading Rust parser..."
14+
$(call download_latest_bench_asset,$(BENCHMARK_RUNNER_ASSET_NAME),benchmark/rust-parser)
15+
chmod +x benchmark/rust-parser
16+
17+
@echo "Downloading Ruby parser..."
18+
$(call download_latest_bench_asset,ruby-parser.rb,benchmark/ruby-parser.rb)
19+
20+
BENCHMARK_ASSETS = \
21+
benchmark/repos \
22+
benchmark/filelist \
23+
benchmark/repos.zip \
24+
benchmark/ruby-parser.rb \
25+
benchmark/rust-parser \
26+
benchmark/stats
27+
28+
benchmark/clear:
29+
rm -rf $(BENCHMARK_ASSETS)
30+
31+
define run_benchmark
32+
cd benchmark && FILELIST_PATH=filelist $(1)
33+
endef
34+
35+
benchmark/compare:
36+
$(call run_benchmark, ./rust-parser)
37+
$(call run_benchmark, ruby ruby-parser.rb)
38+
$(call run_benchmark, NODE_DISABLE_COLORS=1 node node-wasm-parser.js)
39+
40+
BENCHMARK_RECORDING = $(TARGET).benchmark-out
41+
benchmark/record:
42+
echo "Rust:" > $(BENCHMARK_RECORDING)
43+
$(call run_benchmark, ./rust-parser >> ../$(BENCHMARK_RECORDING))
44+
echo "Ruby:" >> $(BENCHMARK_RECORDING)
45+
$(call run_benchmark, ruby ruby-parser.rb >> ../$(BENCHMARK_RECORDING))
46+
echo "WASM (Node.js):" >> $(BENCHMARK_RECORDING)
47+
$(call run_benchmark, NODE_DISABLE_COLORS=1 node node-wasm-parser.js >> ../$(BENCHMARK_RECORDING))

benchmark/download_asset.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'open-uri'
2+
require 'json'
3+
4+
def abort(message)
5+
$stderr.puts("Error: #{message}\n\nAborting.")
6+
exit(1)
7+
end
8+
9+
asset_name = ENV.fetch('ASSET_NAME') { abort 'ASSET_NAME env var must be provided.' }
10+
asset_url = "https://github.com/lib-ruby-parser/bench/releases/download/v0.0.1/#{asset_name}"
11+
save_as = ENV.fetch('SAVE_AS') { abort 'SAVE_AS env var must be provided' }
12+
13+
File.open(save_as, 'wb') do |file|
14+
URI.open(asset_url, 'rb') do |asset|
15+
file.write(asset.read)
16+
end
17+
end

benchmark/node-wasm-parser.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fs = require('fs');
2+
const LibRubyParser = require('../build/nodejs-lib-ruby-parser.js');
3+
4+
const filelist = fs.readFileSync(process.env.FILELIST_PATH).toString().split("\n");
5+
const files = filelist.map(filepath => fs.readFileSync(filepath).toString());
6+
7+
const start = performance.now();
8+
9+
for (let file of files) {
10+
LibRubyParser.parse(file);
11+
}
12+
13+
const end = performance.now();
14+
console.log((end - start) / 1000);

bindings/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ default = ["console_error_panic_hook"]
1212

1313
[dependencies]
1414
js-sys = "0.3.55"
15-
lib-ruby-parser = {version = "3.0.12", features = ["debug-all"]}
15+
lib-ruby-parser = {version = "3.0.12"}
1616
wasm-bindgen = "0.2.63"
1717

1818
# The `console_error_panic_hook` crate provides better debugging of panics by
@@ -33,4 +33,5 @@ wasm-bindgen-test = "0.3.13"
3333

3434
[profile.release]
3535
# Tell `rustc` to optimize for small code size.
36+
lto = true
3637
opt-level = "s"

bindings/src/lib.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ pub fn parse(
352352
buffer_name: Option<String>,
353353
decoder: js_sys::Function,
354354
) -> JsParserResult {
355+
// let start = lib_ruby_parser__now();
355356
let options = RustParserOptions {
356357
buffer_name: buffer_name.unwrap_or_else(|| String::from("(eval)")),
357358
decoder: fn_based_decoder(decoder),
@@ -360,6 +361,18 @@ pub fn parse(
360361
};
361362
let parser = RustParser::new(input, options);
362363
let output = parser.do_parse();
364+
// let end = lib_ruby_parser__now();
365+
// log(&format!("Parsing took {:.10}", end - start));
363366

364-
output.into_js()
367+
// let start = lib_ruby_parser__now();
368+
let output = output.into_js();
369+
// let end = lib_ruby_parser__now();
370+
// log(&format!("Conversion took {:.10}", end - start));
371+
372+
output
373+
}
374+
375+
#[wasm_bindgen]
376+
extern "C" {
377+
fn lib_ruby_parser__now() -> f64;
365378
}

build/build.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ endif
2323
$(info TARGET = $(TARGET))
2424

2525
RUN_WASM_PACK = cd bindings && \
26-
wasm-pack build --target $(TARGET) && \
26+
wasm-pack build --release --target $(TARGET) && \
2727
cd .. && \
2828
cp bindings/pkg/lib_ruby_parser_wasm.js build/$(ENV)-lib-ruby-parser-wrapper.js && \
2929
cp bindings/pkg/lib_ruby_parser_wasm_bg.wasm build/$(ENV)-lib-ruby-parser.wasm

js/types.js

+4
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ root.MagicComment = MagicComment;
8585
root.DecodedInput = DecodedInput;
8686
root.SourceLine = SourceLine;
8787
root.ParserResult = ParserResult;
88+
89+
function lib_ruby_parser__now() {
90+
return performance.now();
91+
}

0 commit comments

Comments
 (0)