(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmark)
| 108 | } |
| 109 | |
| 110 | fn bench_rustpython_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmark) { |
| 111 | let mut settings = Settings::default(); |
| 112 | settings.path_list.push("Lib/".to_string()); |
| 113 | settings.write_bytecode = false; |
| 114 | settings.user_site_directory = false; |
| 115 | |
| 116 | let builder = Interpreter::builder(settings); |
| 117 | let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx); |
| 118 | let interp = builder.add_native_modules(&defs).build(); |
| 119 | interp.enter(|vm| { |
| 120 | let setup_code = vm |
| 121 | .compile(&bench.setup, Mode::Exec, bench.name.to_owned()) |
| 122 | .expect("Error compiling setup code"); |
| 123 | let bench_code = vm |
| 124 | .compile(&bench.code, Mode::Exec, bench.name.to_owned()) |
| 125 | .expect("Error compiling bench code"); |
| 126 | |
| 127 | let bench_func = |scope| { |
| 128 | let res: PyResult = vm.run_code_obj(bench_code.clone(), scope); |
| 129 | vm.unwrap_pyresult(res); |
| 130 | }; |
| 131 | |
| 132 | let bench_setup = |iterations| { |
| 133 | let scope = vm.new_scope_with_builtins(); |
| 134 | if let Some(idx) = iterations { |
| 135 | scope |
| 136 | .locals |
| 137 | .as_ref() |
| 138 | .expect("new_scope_with_builtins always provides locals") |
| 139 | .as_object() |
| 140 | .set_item("ITERATIONS", vm.new_pyobj(idx), vm) |
| 141 | .expect("Error adding ITERATIONS local variable"); |
| 142 | } |
| 143 | let setup_result = vm.run_code_obj(setup_code.clone(), scope.clone()); |
| 144 | vm.unwrap_pyresult(setup_result); |
| 145 | scope |
| 146 | }; |
| 147 | |
| 148 | if bench.iterate { |
| 149 | for idx in (100..=1_000).step_by(200) { |
| 150 | group.throughput(Throughput::Elements(idx as u64)); |
| 151 | group.bench_with_input( |
| 152 | BenchmarkId::new("rustpython", &bench.name), |
| 153 | &idx, |
| 154 | |b, idx| { |
| 155 | b.iter_batched( |
| 156 | || bench_setup(Some(*idx)), |
| 157 | bench_func, |
| 158 | BatchSize::LargeInput, |
| 159 | ); |
| 160 | }, |
| 161 | ); |
| 162 | } |
| 163 | } else { |
| 164 | group.bench_function(BenchmarkId::new("rustpython", &bench.name), move |b| { |
| 165 | b.iter_batched(|| bench_setup(None), bench_func, BatchSize::LargeInput); |
| 166 | }); |
| 167 | } |
no test coverage detected