Skip to content

Commit 80de542

Browse files
committed
Add benches
1 parent 319bb80 commit 80de542

File tree

6 files changed

+2121
-4
lines changed

6 files changed

+2121
-4
lines changed

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ edition = "2018"
77
[workspace]
88
members = [".", "derive", "vm", "wasm/lib", "parser"]
99

10+
[[bench]]
11+
name = "bench"
12+
path = "./benchmarks/bench.rs"
13+
14+
1015
[dependencies]
1116
log="0.4.1"
1217
env_logger="0.5.10"
@@ -15,3 +20,6 @@ rustpython_parser = {path = "parser"}
1520
rustpython_vm = {path = "vm"}
1621
rustyline = "2.1.0"
1722
xdg = "2.2.0"
23+
24+
[dev-dependencies.cpython]
25+
version = "0.2"

benchmarks/bench.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#![feature(test)]
2+
3+
extern crate cpython;
4+
extern crate rustpython_parser;
5+
extern crate rustpython_vm;
6+
extern crate test;
7+
8+
use rustpython_vm::pyobject::PyResult;
9+
use rustpython_vm::{compile, VirtualMachine};
10+
11+
#[bench]
12+
fn bench_tokenization(b: &mut test::Bencher) {
13+
use rustpython_parser::lexer::{make_tokenizer, Tok};
14+
15+
let source = include_str!("./benchmarks/minidom.py");
16+
17+
b.bytes = source.len() as _;
18+
b.iter(|| {
19+
let lexer = make_tokenizer(source);
20+
for res in lexer {
21+
let _token: Tok = res.unwrap().1;
22+
}
23+
})
24+
}
25+
26+
#[bench]
27+
fn bench_parse_to_ast(b: &mut test::Bencher) {
28+
use rustpython_parser::parser::parse_program;
29+
30+
let source = include_str!("./benchmarks/minidom.py");
31+
32+
b.bytes = source.len() as _;
33+
b.iter(|| parse_program(source).unwrap())
34+
}
35+
36+
#[bench]
37+
fn bench_cpython_nbody(b: &mut test::Bencher) {
38+
let source = include_str!("./benchmarks/nbody.py");
39+
40+
let gil = cpython::Python::acquire_gil();
41+
let python = gil.python();
42+
43+
let globals = None;
44+
let locals = None;
45+
46+
b.iter(|| {
47+
let res: cpython::PyResult<()> = python.run(source, globals, locals);
48+
assert_eq!(res.is_ok(), true);
49+
})
50+
}
51+
52+
#[bench]
53+
fn bench_cpython_mandelbrot(b: &mut test::Bencher) {
54+
let source = include_str!("./benchmarks/mandelbrot.py");
55+
56+
let gil = cpython::Python::acquire_gil();
57+
let python = gil.python();
58+
59+
let globals = None;
60+
let locals = None;
61+
62+
b.iter(|| {
63+
let res: cpython::PyResult<()> = python.run(source, globals, locals);
64+
assert_eq!(res.is_ok(), true);
65+
})
66+
}
67+
68+
#[bench]
69+
fn bench_rustpy_nbody(b: &mut test::Bencher) {
70+
// NOTE: Take long time.
71+
let source = include_str!("./benchmarks/nbody.py");
72+
73+
let vm = VirtualMachine::new();
74+
75+
let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
76+
Ok(code) => code,
77+
Err(e) => panic!("{:?}", e),
78+
};
79+
80+
b.iter(|| {
81+
let scope = vm.ctx.new_scope();
82+
let res: PyResult = vm.run_code_obj(code.clone(), scope);
83+
assert_eq!(res.is_ok(), true);
84+
})
85+
}
86+
87+
#[bench]
88+
fn bench_rustpy_mandelbrot(b: &mut test::Bencher) {
89+
// NOTE: Take long time.
90+
let source = include_str!("./benchmarks/mandelbrot.py");
91+
92+
let vm = VirtualMachine::new();
93+
94+
let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
95+
Ok(code) => code,
96+
Err(e) => panic!("{:?}", e),
97+
};
98+
99+
b.iter(|| {
100+
let scope = vm.ctx.new_scope();
101+
let res: PyResult = vm.run_code_obj(code.clone(), scope);
102+
assert_eq!(res.is_ok(), true);
103+
})
104+
}

benchmarks/benchmarks/mandelbrot.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
i = i+1
2222

2323
if Tr+Ti <= 4:
24-
print('*', end='')
24+
# print('*', end='')
25+
pass
2526
else:
26-
print('·', end='')
27+
# print('·', end='')
28+
pass
2729

2830
x = x+1
2931

30-
print()
32+
# print()
3133
y = y+1

0 commit comments

Comments
 (0)