Repository navigation
Expand file tree
/
Copy pathbenchmark_scripts.rs
More file actions
85 lines (74 loc) · 2.5 KB
/
Copy pathbenchmark_scripts.rs
File metadata and controls
85 lines (74 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// cspell:ignore pyperformance nqueens pidigits
#![allow(clippy::tests_outside_test_module)]
use rustpython::{InterpreterBuilder, InterpreterBuilderExt};
use std::{fs, path::Path};
const PYPERFORMANCE_BENCHMARKS: &[&str] = &[
"base64.py",
"decimal_factorial.py",
"deepcopy.py",
"deltablue.py",
"float.py",
"generators.py",
"json_dumps.py",
"nqueens.py",
"pickle.py",
"pidigits.py",
"regex_effbot.py",
"richards.py",
"unpickle.py",
];
#[test]
fn pyperformance_benchmarks_run_in_rustpython() {
let benchmark_dir = Path::new("benches/benchmarks");
for name in PYPERFORMANCE_BENCHMARKS {
let path = benchmark_dir.join(name);
let source = fs::read_to_string(&path)
.unwrap_or_else(|err| panic!("failed to read {}: {err}", path.display()));
let source_path = path.to_string_lossy();
InterpreterBuilder::new()
.init_stdlib()
.interpreter()
.enter(|vm| {
let code = vm
.compile(&source, rustpython::vm::compiler::Mode::Exec, &*source_path)
.unwrap_or_else(|err| panic!("failed to compile {}: {err}", path.display()));
let scope = vm.new_scope_with_builtins();
if let Err(err) = vm.run_code_obj(code, scope) {
vm.print_exception(&err);
panic!("failed to execute {}", path.display());
}
});
}
}
#[test]
fn pyperformance_benchmarks_restore_interpreter_state() {
let source = r#"
import decimal
import pickle
import sys
missing = object()
pickle_module = sys.modules.get("pickle", missing)
pickle_accelerator = sys.modules.get("_pickle", missing)
decimal_precision = decimal.getcontext().prec
for path in (
"benches/benchmarks/pickle.py",
"benches/benchmarks/unpickle.py",
"benches/benchmarks/decimal_factorial.py",
):
with open(path) as benchmark_file:
benchmark_source = benchmark_file.read()
exec(compile(benchmark_source, path, "exec"), {})
assert sys.modules.get("pickle", missing) is pickle_module
assert sys.modules.get("_pickle", missing) is pickle_accelerator
assert decimal.getcontext().prec == decimal_precision
"#;
InterpreterBuilder::new()
.init_stdlib()
.interpreter()
.enter(|vm| {
if let Err(err) = vm.run_simple_string(source) {
vm.print_exception(&err);
panic!("pyperformance benchmarks leaked interpreter state");
}
});
}