summaryrefslogtreecommitdiff
path: root/core/benches/op_baseline.rs
blob: 132d92f000084b18a3c74493913ebeb610166f53 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use bencher::{benchmark_group, benchmark_main, Bencher};

use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::op_sync;
use deno_core::serialize_op_result;
use deno_core::v8;
use deno_core::JsRuntime;
use deno_core::Op;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;

use std::cell::RefCell;
use std::rc::Rc;

fn create_js_runtime() -> JsRuntime {
  let mut runtime = JsRuntime::new(Default::default());
  runtime.register_op("pi_json", op_sync(|_, _: (), _| Ok(314159)));
  runtime.register_op("pi_async", op_async(op_pi_async));
  runtime.register_op("nop", |state, _, _| {
    Op::Sync(serialize_op_result(Ok(9), state))
  });

  // Init ops
  runtime
    .execute(
      "init",
      r#"
      Deno.core.ops();
      Deno.core.registerErrorClass('Error', Error);
    "#,
    )
    .unwrap();

  runtime
}

// this is a function since async closures aren't stable
async fn op_pi_async(
  _: Rc<RefCell<OpState>>,
  _: (),
  _: Option<ZeroCopyBuf>,
) -> Result<i64, AnyError> {
  Ok(314159)
}

pub fn bench_runtime_js(b: &mut Bencher, src: &str) {
  let mut runtime = create_js_runtime();
  let context = runtime.global_context();
  let scope = &mut v8::HandleScope::with_context(runtime.v8_isolate(), context);
  let code = v8::String::new(scope, src).unwrap();
  let script = v8::Script::compile(scope, code, None).unwrap();
  b.iter(|| {
    script.run(scope).unwrap();
  });
}

pub fn bench_runtime_js_async(b: &mut Bencher, src: &str) {
  let mut runtime = create_js_runtime();
  let tokio_runtime = tokio::runtime::Builder::new_current_thread()
    .enable_all()
    .build()
    .unwrap();

  b.iter(|| {
    runtime.execute("inner_loop", src).unwrap();
    let future = runtime.run_event_loop();
    tokio_runtime.block_on(future).unwrap();
  });
}

fn bench_op_pi_json(b: &mut Bencher) {
  bench_runtime_js(
    b,
    r#"for(let i=0; i < 1e3; i++) {
      Deno.core.opSync("pi_json", null);
    }"#,
  );
}

fn bench_op_nop(b: &mut Bencher) {
  bench_runtime_js(
    b,
    r#"for(let i=0; i < 1e3; i++) {
      Deno.core.dispatchByName("nop", null, null, null);
    }"#,
  );
}

fn bench_op_async(b: &mut Bencher) {
  bench_runtime_js_async(
    b,
    r#"for(let i=0; i < 1e3; i++) {
      Deno.core.opAsync("pi_async", null);
    }"#,
  );
}

benchmark_group!(benches, bench_op_pi_json, bench_op_nop, bench_op_async);
benchmark_main!(benches);