|
| 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_rustpy_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_parse_to_ast(b: &mut test::Bencher) { |
| 38 | + let source = include_str!("./benchmarks/minidom.py"); |
| 39 | + |
| 40 | + let gil = cpython::Python::acquire_gil(); |
| 41 | + let python = gil.python(); |
| 42 | + |
| 43 | + let globals = None; |
| 44 | + let locals = cpython::PyDict::new(python); |
| 45 | + |
| 46 | + locals.set_item(python, "SOURCE_CODE", source).unwrap(); |
| 47 | + |
| 48 | + let code = "compile(SOURCE_CODE, mode=\"exec\", filename=\"minidom.py\")"; |
| 49 | + |
| 50 | + b.bytes = source.len() as _; |
| 51 | + b.iter(|| { |
| 52 | + let res: cpython::PyResult<cpython::PyObject> = python.eval(code, globals, Some(&locals)); |
| 53 | + assert!(res.is_ok()); |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +#[bench] |
| 58 | +fn bench_cpython_nbody(b: &mut test::Bencher) { |
| 59 | + let source = include_str!("./benchmarks/nbody.py"); |
| 60 | + |
| 61 | + let gil = cpython::Python::acquire_gil(); |
| 62 | + let python = gil.python(); |
| 63 | + |
| 64 | + let globals = None; |
| 65 | + let locals = None; |
| 66 | + |
| 67 | + b.iter(|| { |
| 68 | + let res: cpython::PyResult<()> = python.run(source, globals, locals); |
| 69 | + assert!(res.is_ok()); |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +#[bench] |
| 74 | +fn bench_cpython_mandelbrot(b: &mut test::Bencher) { |
| 75 | + let source = include_str!("./benchmarks/mandelbrot.py"); |
| 76 | + |
| 77 | + let gil = cpython::Python::acquire_gil(); |
| 78 | + let python = gil.python(); |
| 79 | + |
| 80 | + let globals = None; |
| 81 | + let locals = None; |
| 82 | + |
| 83 | + b.iter(|| { |
| 84 | + let res: cpython::PyResult<()> = python.run(source, globals, locals); |
| 85 | + assert!(res.is_ok()); |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +#[bench] |
| 90 | +fn bench_rustpy_nbody(b: &mut test::Bencher) { |
| 91 | + // NOTE: Take long time. |
| 92 | + let source = include_str!("./benchmarks/nbody.py"); |
| 93 | + |
| 94 | + let vm = VirtualMachine::new(); |
| 95 | + |
| 96 | + let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) { |
| 97 | + Ok(code) => code, |
| 98 | + Err(e) => panic!("{:?}", e), |
| 99 | + }; |
| 100 | + |
| 101 | + b.iter(|| { |
| 102 | + let scope = vm.ctx.new_scope(); |
| 103 | + let res: PyResult = vm.run_code_obj(code.clone(), scope); |
| 104 | + assert!(res.is_ok()); |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +#[bench] |
| 109 | +fn bench_rustpy_mandelbrot(b: &mut test::Bencher) { |
| 110 | + // NOTE: Take long time. |
| 111 | + let source = include_str!("./benchmarks/mandelbrot.py"); |
| 112 | + |
| 113 | + let vm = VirtualMachine::new(); |
| 114 | + |
| 115 | + let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) { |
| 116 | + Ok(code) => code, |
| 117 | + Err(e) => panic!("{:?}", e), |
| 118 | + }; |
| 119 | + |
| 120 | + b.iter(|| { |
| 121 | + let scope = vm.ctx.new_scope(); |
| 122 | + let res: PyResult = vm.run_code_obj(code.clone(), scope); |
| 123 | + assert!(res.is_ok()); |
| 124 | + }) |
| 125 | +} |
0 commit comments