summaryrefslogtreecommitdiff
path: root/cli/bench/lsp_bench_standalone.rs
blob: e8dc29073097012330b2250f154b3dd3aeaa5a10 (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
101
102
103
104
105
106
107
108
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use deno_bench_util::bencher::benchmark_group;
use deno_bench_util::bencher::benchmark_main;
use deno_bench_util::bencher::Bencher;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use test_util::lsp::LspClient;

// Intended to match the benchmark in quick-lint-js
// https://github.com/quick-lint/quick-lint-js/blob/35207e6616267c6c81be63f47ce97ec2452d60df/benchmark/benchmark-lsp/lsp-benchmarks.cpp#L223-L268
fn incremental_change_wait(bench: &mut Bencher) {
  let deno_exe = test_util::deno_exe_path();
  let mut client = LspClient::new(&deno_exe, false).unwrap();

  static FIXTURE_INIT_JSON: &[u8] =
    include_bytes!("testdata/initialize_params.json");
  let params: Value = serde_json::from_slice(FIXTURE_INIT_JSON).unwrap();
  let (_, maybe_err) = client
    .write_request::<_, _, Value>("initialize", params)
    .unwrap();
  assert!(maybe_err.is_none());
  client.write_notification("initialized", json!({})).unwrap();

  client
    .write_notification(
      "textDocument/didOpen",
      json!({
        "textDocument": {
          "uri": "file:///testdata/express-router.js",
          "languageId": "javascript",
          "version": 0,
          "text": include_str!("testdata/express-router.js")
        }
      }),
    )
    .unwrap();

  let (id, method, _): (u64, String, Option<Value>) =
    client.read_request().unwrap();
  assert_eq!(method, "workspace/configuration");
  client
    .write_response(
      id,
      json!({
        "enable": true
      }),
    )
    .unwrap();

  let (method, _maybe_diag): (String, Option<Value>) =
    client.read_notification().unwrap();
  assert_eq!(method, "textDocument/publishDiagnostics");

  let mut document_version: u64 = 0;
  bench.iter(|| {
    let text = format!("m{document_version:05}");
    client
      .write_notification(
        "textDocument/didChange",
        json!({
            "textDocument": {
                "version": document_version,
                "uri":"file:///testdata/express-router.js"
            },
            "contentChanges": [
              {"text": text, "range":{"start":{"line":506,"character":39},"end":{"line":506,"character":45}}},
              {"text": text, "range":{"start":{"line":507,"character":8},"end":{"line":507,"character":14}}},
              {"text": text, "range":{"start":{"line":509,"character":10},"end":{"line":509,"character":16}}}
            ]
        })
    ).unwrap();

     wait_for_deno_lint_diagnostic(document_version, &mut client);

    document_version += 1;
  })
}

fn wait_for_deno_lint_diagnostic(
  document_version: u64,
  client: &mut LspClient,
) {
  loop {
    let (method, maybe_diag): (String, Option<Value>) =
      client.read_notification().unwrap();
    if method == "textDocument/publishDiagnostics" {
      let d = maybe_diag.unwrap();
      let msg = d.as_object().unwrap();
      let version = msg.get("version").unwrap().as_u64().unwrap();
      if document_version == version {
        let diagnostics = msg.get("diagnostics").unwrap().as_array().unwrap();
        for diagnostic in diagnostics {
          let source = diagnostic.get("source").unwrap().as_str().unwrap();
          if source == "deno-lint" {
            return;
          }
        }
      }
    } else {
      todo!() // handle_misc_message
    }
  }
}

benchmark_group!(benches, incremental_change_wait);
benchmark_main!(benches);