php-lsp 0.22.2

A PHP Language Server Protocol implementation
Documentation
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Session-length memory guard (P1b.2): RSS across a sustained edit session.
//!
//! Drives the real LSP server against the pinned Laravel fixture, opens four
//! files, and simulates a long editing session — alternating body edits and
//! declaration edits (the kind that stale sibling analyses) across the open
//! set, with hover/completion/references fired along the way to exercise
//! every request cache. Samples process RSS throughout.
//!
//! The property under guard is P0.2/WS2's promise: memos, interners, and
//! request caches are bounded, so a long session plateaus instead of growing
//! linearly with keystrokes. Gate: RSS growth from the post-index baseline to
//! the end of the session stays under an absolute ceiling.
//!
//! Run with `cargo bench --bench rss_session`. Release mode matters.

use php_lsp::backend::Backend;
use serde_json::{Value, json};

// Match the production binary's allocator — RSS behavior is allocator
// behavior, so measuring with the system allocator would guard the wrong
// thing (src/main.rs sets mimalloc the same way).
#[cfg(not(feature = "dhat-heap"))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

// Heap-profile mode: `cargo bench --bench rss_session --features dhat-heap`
// writes dhat-heap.json attributing retained bytes at exit (use
// RSS_SESSION_ROUNDS to shorten the run — dhat is ~3-5x slower).
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt, DuplexStream, ReadHalf, WriteHalf};
use tower_lsp_server::{LspService, Server};

const EDIT_ROUNDS: usize = 75; // 4 edits per round (one per open file) → 300 edits
const RSS_SAMPLE_EVERY: usize = 5; // rounds
/// The session's first ~100 edits warm caches and thread-heap pools; the
/// property under guard is the *tail*: once warm, RSS must plateau. With the
/// production runtime config the tail measures ~40 MB over the last 200
/// edits (decelerating); the uncapped-blocking-pool regression this bench
/// caught grew ~15-20 MB per round here — loudly over either gate.
const TAIL_START_ROUND: usize = 25;
const TAIL_GROWTH_CEILING_MB: f64 = 120.0;
/// Absolute sanity ceiling on end RSS growth from the post-index baseline.
/// The baseline is captured *after* the analysis warm sweep completes (waited
/// via debugStats), so the warm memo table (~80 MB on this fixture) is in the
/// baseline, not counted as session growth; edit-idle re-warms during the run
/// replace memos rather than adding them.
const GROWTH_CEILING_MB: f64 = 400.0;
const INDEX_READY_TIMEOUT_SECS: u64 = 300;
const DIAG_TIMEOUT_SECS: u64 = 30;

// ---------- framing ----------

fn frame(msg: &Value) -> Vec<u8> {
    let body = serde_json::to_string(msg).unwrap();
    format!("Content-Length: {}\r\n\r\n{}", body.len(), body).into_bytes()
}

async fn read_msg(reader: &mut (impl AsyncReadExt + Unpin)) -> Value {
    let mut header_buf = Vec::new();
    loop {
        let b = reader.read_u8().await.expect("read byte");
        header_buf.push(b);
        if header_buf.ends_with(b"\r\n\r\n") {
            break;
        }
    }
    let header_str = std::str::from_utf8(&header_buf).unwrap();
    let content_length: usize = header_str
        .lines()
        .find(|l| l.to_lowercase().starts_with("content-length:"))
        .and_then(|l| l.split(':').nth(1))
        .and_then(|v| v.trim().parse().ok())
        .expect("Content-Length header");
    let mut body = vec![0u8; content_length];
    reader.read_exact(&mut body).await.expect("read body");
    serde_json::from_slice(&body).expect("parse JSON")
}

struct Client {
    write: WriteHalf<DuplexStream>,
    read: ReadHalf<DuplexStream>,
    next_id: u64,
}

impl Client {
    async fn request(&mut self, method: &str, params: Value) -> Value {
        let id = self.next_id;
        self.next_id += 1;
        let msg = json!({"jsonrpc":"2.0","id":id,"method":method,"params":params});
        self.write.write_all(&frame(&msg)).await.unwrap();
        let method_owned = method.to_string();
        tokio::time::timeout(Duration::from_secs(60), async {
            loop {
                let resp = read_msg(&mut self.read).await;
                if resp.get("method").is_some() {
                    if let Some(srv_id) = resp.get("id") {
                        let ack = json!({"jsonrpc":"2.0","id":srv_id,"result":null});
                        self.write.write_all(&frame(&ack)).await.unwrap();
                    }
                    continue;
                }
                if resp.get("id") == Some(&json!(id)) {
                    return resp;
                }
            }
        })
        .await
        .unwrap_or_else(|_| panic!("timed out: {method_owned}"))
    }

    async fn notify(&mut self, method: &str, params: Value) {
        let msg = json!({"jsonrpc":"2.0","method":method,"params":params});
        self.write.write_all(&frame(&msg)).await.unwrap();
    }

    async fn wait_for_diagnostics(&mut self, uri: &str) -> bool {
        let uri_val = json!(uri);
        tokio::time::timeout(Duration::from_secs(DIAG_TIMEOUT_SECS), async {
            loop {
                let msg = read_msg(&mut self.read).await;
                if msg.get("method") == Some(&json!("textDocument/publishDiagnostics"))
                    && msg["params"]["uri"] == uri_val
                {
                    return;
                }
                if msg.get("method").is_some()
                    && let Some(id) = msg.get("id")
                {
                    let ack = json!({"jsonrpc":"2.0","id":id,"result":null});
                    self.write.write_all(&frame(&ack)).await.unwrap();
                }
            }
        })
        .await
        .is_ok()
    }

    async fn wait_for_index_ready(&mut self) -> bool {
        tokio::time::timeout(Duration::from_secs(INDEX_READY_TIMEOUT_SECS), async {
            loop {
                let msg = read_msg(&mut self.read).await;
                if msg.get("method") == Some(&json!("$/php-lsp/indexReady")) {
                    return;
                }
                if msg.get("method").is_some()
                    && let Some(id) = msg.get("id")
                {
                    let ack = json!({"jsonrpc":"2.0","id":id,"result":null});
                    self.write.write_all(&frame(&ack)).await.unwrap();
                }
            }
        })
        .await
        .is_ok()
    }

    /// Like [`Self::request`] but omits `params` — required for handlers
    /// declared without a params argument (`$/php-lsp/debugStats`).
    async fn request_no_params(&mut self, method: &str) -> Value {
        let id = self.next_id;
        self.next_id += 1;
        let msg = json!({"jsonrpc":"2.0","id":id,"method":method});
        self.write.write_all(&frame(&msg)).await.unwrap();
        let method_owned = method.to_string();
        tokio::time::timeout(Duration::from_secs(60), async {
            loop {
                let resp = read_msg(&mut self.read).await;
                if resp.get("method").is_some() {
                    if let Some(srv_id) = resp.get("id") {
                        let ack = json!({"jsonrpc":"2.0","id":srv_id,"result":null});
                        self.write.write_all(&frame(&ack)).await.unwrap();
                    }
                    continue;
                }
                if resp.get("id") == Some(&json!(id)) {
                    return resp;
                }
            }
        })
        .await
        .unwrap_or_else(|_| panic!("timed out: {method_owned}"))
    }

    /// Poll `$/php-lsp/debugStats` until the post-index warm sweep completes,
    /// so the RSS baseline deterministically includes the warm memo table
    /// instead of racing it.
    async fn wait_for_warm_sweep(&mut self) -> bool {
        for _ in 0..600 {
            let resp = self.request_no_params("$/php-lsp/debugStats").await;
            if resp["result"]["warm_sweeps_completed"]
                .as_u64()
                .unwrap_or(0)
                >= 1
            {
                return true;
            }
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
        false
    }
}

fn spawn_server() -> Client {
    let (client_stream, server_stream) = tokio::io::duplex(1 << 20);
    let (server_read, server_write) = tokio::io::split(server_stream);
    let (client_read, client_write) = tokio::io::split(client_stream);
    let (service, socket) = LspService::build(Backend::new)
        .custom_method("$/php-lsp/debugStats", Backend::debug_stats)
        .finish();
    tokio::spawn(Server::new(server_read, server_write, socket).serve(service));
    Client {
        write: client_write,
        read: client_read,
        next_id: 1,
    }
}

fn rss_mb() -> f64 {
    let out = std::process::Command::new("ps")
        .args(["-o", "rss=", "-p", &std::process::id().to_string()])
        .output()
        .expect("ps");
    let kb: f64 = String::from_utf8_lossy(&out.stdout)
        .trim()
        .parse()
        .unwrap_or(0.0);
    kb / 1024.0
}

fn pick_open_files(root: &Path) -> Vec<PathBuf> {
    // Prefer medium-sized real files so the edits exercise real analysis.
    let candidates = [
        "src/Illuminate/Auth/AuthManager.php",
        "src/Illuminate/Cache/CacheManager.php",
        "src/Illuminate/Support/Str.php",
        "src/Illuminate/Collections/Arr.php",
    ];
    candidates
        .iter()
        .map(|p| root.join(p))
        .filter(|p| p.is_file())
        .collect()
}

fn main() {
    #[cfg(feature = "dhat-heap")]
    let _profiler = dhat::Profiler::new_heap();
    // Mirror the production runtime config (src/main.rs): the blocking-pool
    // cap is load-bearing for session RSS. RSS_SESSION_MAX_BLOCKING overrides
    // for leak-diagnosis runs.
    let cap = std::env::var("RSS_SESSION_MAX_BLOCKING")
        .ok()
        .and_then(|n| n.parse().ok())
        .unwrap_or(16);
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .max_blocking_threads(cap)
        .build()
        .unwrap()
        .block_on(run());
}

async fn run() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("benches/fixtures/laravel");
    if !root.is_dir() {
        eprintln!(
            "benches/fixtures/laravel missing — skipping (run scripts/setup_laravel_fixture.sh)"
        );
        return;
    }
    let files = pick_open_files(&root);
    assert!(
        files.len() >= 4,
        "expected the pinned fixture layout; found {} of 4 files",
        files.len()
    );

    let mut c = spawn_server();
    c.request(
        "initialize",
        json!({"processId": null, "rootUri": format!("file://{}", root.display()), "capabilities": {}}),
    )
    .await;
    c.notify("initialized", json!({})).await;
    assert!(c.wait_for_index_ready().await, "indexReady timeout");
    assert!(c.wait_for_warm_sweep().await, "warm sweep timeout");

    let mut docs: Vec<(String, String)> = Vec::new();
    for path in &files {
        let text = std::fs::read_to_string(path).unwrap();
        let uri = format!("file://{}", path.display());
        c.notify(
            "textDocument/didOpen",
            json!({"textDocument": {"uri": uri, "languageId": "php", "version": 1, "text": text}}),
        )
        .await;
        assert!(c.wait_for_diagnostics(&uri).await, "didOpen diag timeout");
        docs.push((uri, text));
    }

    let rss_baseline = rss_mb();
    eprintln!("baseline RSS after index + 4 opens: {rss_baseline:.0} MB");

    let rounds: usize = std::env::var("RSS_SESSION_ROUNDS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(EDIT_ROUNDS);
    let mut peak = rss_baseline;
    let mut tail_baseline: Option<f64> = None;
    let mut version = 1i64;
    for round in 0..rounds {
        for (i, (uri, text)) in docs.iter_mut().enumerate() {
            version += 1;
            // Alternate body edits with declaration edits: appending a fresh
            // top-level function each time changes the file's declaration
            // fingerprint, which is what stales sibling analyses and grows
            // memo tables if anything is unbounded. RSS_SESSION_MODE narrows
            // the workload for leak diagnosis: `body` (body-only edits),
            // `samename` (decl edits reusing one name), `norequests` (mixed
            // edits, no hover/completion).
            let mode = std::env::var("RSS_SESSION_MODE").unwrap_or_default();
            if mode == "body" || (round + i) % 2 == 0 {
                text.push(' ');
            } else if mode == "samename" {
                text.push_str(&format!(
                    "\nfunction bench_tmp_{i}(): int {{ return 1; }}\n"
                ));
            } else {
                text.push_str(&format!(
                    "\nfunction bench_tmp_{round}_{i}(): int {{ return {round}; }}\n"
                ));
            }
            c.notify(
                "textDocument/didChange",
                json!({
                    "textDocument": {"uri": uri.as_str(), "version": version},
                    "contentChanges": [{"text": text.as_str()}],
                }),
            )
            .await;
            if !c.wait_for_diagnostics(uri).await {
                eprintln!("edit→diag timeout at round {round} — aborting");
                std::process::exit(1);
            }
            // Exercise the read caches the way a user would.
            if std::env::var("RSS_SESSION_MODE").unwrap_or_default() != "norequests" {
                c.request(
                    "textDocument/hover",
                    json!({"textDocument": {"uri": uri.as_str()}, "position": {"line": 12, "character": 12}}),
                )
                .await;
                c.request(
                    "textDocument/completion",
                    json!({"textDocument": {"uri": uri.as_str()}, "position": {"line": 12, "character": 12}}),
                )
                .await;
            }
        }
        if round == TAIL_START_ROUND {
            tail_baseline = Some(rss_mb());
        }
        if round % RSS_SAMPLE_EVERY == 0 {
            let rss = rss_mb();
            peak = peak.max(rss);
            eprintln!("round {round:>3}/{rounds}: rss {rss:.0} MB (baseline {rss_baseline:.0})");
        }
    }

    let rss_end = rss_mb();
    peak = peak.max(rss_end);
    let growth = rss_end - rss_baseline;
    println!(
        "\nrss_session: baseline {rss_baseline:.0} MB → end {rss_end:.0} MB (peak {peak:.0}), growth {growth:.0} MB over {} edits",
        rounds * 4
    );
    let tail_growth = tail_baseline.map(|t| rss_end - t);
    let tail_ok = match tail_growth {
        Some(t) => {
            println!(
                "tail growth (rounds {TAIL_START_ROUND}+): {t:.0} MB (ceiling {TAIL_GROWTH_CEILING_MB})"
            );
            t <= TAIL_GROWTH_CEILING_MB
        }
        // Short diagnosis runs never reach the tail window — skip that gate.
        None => true,
    };
    let ok = growth <= GROWTH_CEILING_MB && tail_ok;
    println!(
        "gates: {}",
        if ok {
            "OK"
        } else {
            "OVER — session-length memory growth is back!"
        }
    );
    if !ok {
        std::process::exit(1);
    }
}