Visual Studio Code, rebuilt in Rust
Rust
//! Frame time of a trackpad scroll on each scrolling surface of a 1440x900 workbench with every built-in extension
//! installed and running: each frame is one 30px pixel wheel event, then the draw. Text is laid out by gpui's
//! NoopTextSystem (CoreText needs the main thread, which libtest never gives) and nothing reaches the GPU. The gate
//! is 8 ms at the median and p95, a 120 Hz frame. Run in release:
//! `cargo gc test --release -p app -- --ignored scroll_frame_time --nocapture`.
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use config::registry::Registry;
use config::service::Settings;
use config::workspace::Workspace;
use exthost::extension_management::UserExtensions;
use gpui::{AppContext, HeadlessAppContext, InputEvent, Modifiers, Pixels, Point, ScrollDelta, ScrollWheelEvent, TouchPhase, WindowHandle, point, px, size};
use serde_json::Value;
use super::Workbench;
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
const FRAMES: usize = 600;
struct Fixture {
dir: PathBuf,
cx: HeadlessAppContext,
window: WindowHandle<Workbench>,
}
/// A folder of 800 files and a 20000-line TypeScript file, opened with the built-in extensions, as `code <folder>`.
fn fixture(name: &str) -> Fixture {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let extensions = exthost::scan::scan_builtin_extensions(&app_root.join("extensions")).unwrap();
let theme = load_theme(&extensions, "Dark Modern").unwrap();
let described: Vec<(&str, &Value)> = extensions.iter().map(|extension| (extension.id.as_str(), &extension.description)).collect();
let dir = std::env::temp_dir().join(format!("code-gpui-scroll-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let folder = dir.join("folder");
std::fs::create_dir_all(&folder).unwrap();
std::fs::create_dir_all(dir.join("User")).unwrap();
for index in 0..800 {
std::fs::write(folder.join(format!("file{index:03}.ts")), "").unwrap();
}
let source: String = (0..20000).map(|line| format!("export function f{line}(value: number): string {{ return `${{value + {line}}}`; }}\n")).collect();
std::fs::write(folder.join("large.ts"), source).unwrap();
let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&described), dir.join("User"), Some(Workspace::folder(folder.clone())))));
let languages = Languages::new(&extensions);
let mut cx = HeadlessAppContext::new(Arc::new(gpui::NoopTextSystem));
let user = UserExtensions::new(dir.join("extensions"), "1.147.0");
let window = cx
.open_window(size(px(1440.), px(900.)), |_, cx| {
cx.new(|cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), Some(Workspace::folder(folder.clone())), languages, settings, HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.skip_dialogs = true;
workbench.load_keybindings(crate::keybindings::default_keybindings(&extensions), dir.join("User/keybindings.json"));
workbench.host.set_running_extensions(&extensions);
workbench.extensions = extensions.clone();
workbench.set_installed_extensions(extensions.clone(), user, Vec::new(), cx);
workbench
})
})
.unwrap();
cx.run_until_parked();
Fixture { dir, cx, window }
}
impl Fixture {
fn run(&mut self, command: &str, args: Vec<Value>) {
self.window.update(&mut self.cx, |workbench, window, cx| drop(workbench.execute_command(command, args, window, cx))).unwrap();
self.cx.run_until_parked();
}
fn frame(&mut self, scroll: Option<Point<Pixels>>) -> f64 {
self.cx
.update_window(self.window.into(), |_, window, cx| {
let start = Instant::now();
if let Some(position) = scroll {
let event = ScrollWheelEvent { position, delta: ScrollDelta::Pixels(point(px(0.), px(-30.))), modifiers: Modifiers::none(), touch_phase: TouchPhase::Moved, source: gpui::ScrollSource::Pointer };
window.dispatch_event(event.to_platform_input(), cx);
}
window.draw(cx).clear(cx);
start.elapsed().as_secs_f64() * 1000.
})
.unwrap()
}
/// Scrolls at `position` for `FRAMES` frames and checks the frame times against the gate; `scrolled` reads how
/// far the surface moved, which must pass `min_scrolled`.
fn measure(mut self, surface: &str, position: Point<Pixels>, min_scrolled: f32, scrolled: impl Fn(&Workbench) -> f32) {
self.frame(None);
let mut times: Vec<f64> = (0..FRAMES).map(|_| self.frame(Some(position))).collect();
let moved = self.window.update(&mut self.cx, |workbench, _, _| scrolled(workbench)).unwrap();
std::fs::remove_dir_all(&self.dir).unwrap();
assert!(moved > min_scrolled, "{surface} scrolled {moved}");
times.sort_by(f64::total_cmp);
let (median, p95, max) = (times[times.len() / 2], times[times.len() * 95 / 100], times[times.len() - 1]);
eprintln!("{surface}, scrolled {moved}: median {median:.2} ms, p95 {p95:.2} ms, max {max:.2} ms");
assert!(median < 8. && p95 < 8., "{surface}: median {median:.2} ms, p95 {p95:.2} ms, the gate is 8 ms");
}
}
#[test]
#[ignore]
fn explorer_scroll_frame_time() {
let mut fixture = fixture("explorer");
fixture.run("workbench.view.explorer", Vec::new());
fixture.frame(None);
let position = fixture.window.update(&mut fixture.cx, |workbench, _, _| workbench.explorer.scroll.bounds().center()).unwrap();
fixture.measure("Explorer, 801 files", position, 5000., |workbench| f32::from(-workbench.explorer.scroll.offset().y));
}
#[test]
#[ignore]
fn extensions_scroll_frame_time() {
let mut fixture = fixture("extensions");
fixture.run("workbench.extensions.search", vec![Value::from("@builtin")]);
fixture.frame(None);
let position = fixture.window.update(&mut fixture.cx, |workbench, _, _| workbench.extensions_viewlet.panes[0].scroll.0.borrow().base_handle.bounds().center()).unwrap();
fixture.measure("Extensions @builtin", position, 1000., |workbench| f32::from(-workbench.extensions_viewlet.panes[0].scroll.0.borrow().base_handle.offset().y));
}
#[test]
#[ignore]
fn editor_scroll_frame_time() {
let mut fixture = fixture("editor");
let file = fixture.dir.join("folder/large.ts");
fixture.window.update(&mut fixture.cx, |workbench, window, cx| workbench.open_file(file, true, window, cx)).unwrap();
fixture.cx.run_until_parked();
fixture.measure("Editor, 20000 lines", point(px(900.), px(500.)), 5000., |workbench| workbench.editors.active().unwrap().scroll.top);
}
#[test]
#[ignore]
fn settings_scroll_frame_time() {
let mut fixture = fixture("settings");
fixture.run("workbench.action.openSettings2", Vec::new());
fixture.window
.update(&mut fixture.cx, |workbench, _, cx| {
// The whole tree, as after a cleared search, not Commonly Used alone.
workbench.settings_editor.category = None;
workbench.render_settings(cx);
})
.unwrap();
fixture.cx.run_until_parked();
fixture.measure("Settings, whole tree", point(px(900.), px(500.)), 100., |workbench| workbench.settings_editor.tree.logical_scroll_top().item_ix as f32);
}
/// A key typed in the terminal panel, echoed by the shell, then drawn: each frame is one byte of output through
/// `terminal_data` and the draw, over a full 10000-line scrollback. The gate is 16 ms at p95, a 60 Hz frame.
#[test]
#[ignore]
fn terminal_typing_frame_time() {
let mut fixture = fixture("terminal");
fixture.run("workbench.action.terminal.new", Vec::new());
let id = fixture.window.update(&mut fixture.cx, |workbench, _, _| workbench.terminal_view.active_instance_id().unwrap()).unwrap();
let output = |fixture: &mut Fixture, bytes: &[u8]| {
fixture.window.update(&mut fixture.cx, |workbench, window, cx| {
workbench.terminal_data(id, bytes, window, cx);
})
.unwrap();
};
let history: String = (0..12000).map(|line| format!("\x1b[32m-rw-r--r--\x1b[0m 1 user staff {line:>8} \x1b[1;34mfile_{line}.txt\x1b[0m\r\n")).collect();
output(&mut fixture, history.as_bytes());
output(&mut fixture, b"$ ");
fixture.frame(None);
let mut times = Vec::new();
for index in 0..FRAMES {
let start = Instant::now();
output(&mut fixture, if index % 80 == 79 { b"\r\n$ " } else { b"x" });
times.push(start.elapsed().as_secs_f64() * 1000. + fixture.frame(None));
}
std::fs::remove_dir_all(&fixture.dir).unwrap();
times.sort_by(f64::total_cmp);
let (median, p95, max) = (times[times.len() / 2], times[times.len() * 95 / 100], times[times.len() - 1]);
eprintln!("Terminal typing: median {median:.2} ms, p95 {p95:.2} ms, max {max:.2} ms");
assert!(p95 < 16., "terminal typing: p95 {p95:.2} ms, the gate is 16 ms");
}