From a4e0d5b5dd3fa47dd031f565d8bdae4ff89233c1 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 31 Jul 2024 14:25:34 +0100 Subject: [PATCH] 2024-07-31/02: add "Troubleshoting: terminal lag and solutions with Nix" as draft --- ...ing-terminal-lag-and-solutions-with-nix.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 2024-07-31/.02-troubleshooting-terminal-lag-and-solutions-with-nix.md diff --git a/2024-07-31/.02-troubleshooting-terminal-lag-and-solutions-with-nix.md b/2024-07-31/.02-troubleshooting-terminal-lag-and-solutions-with-nix.md new file mode 100644 index 0000000..2fe4d09 --- /dev/null +++ b/2024-07-31/.02-troubleshooting-terminal-lag-and-solutions-with-nix.md @@ -0,0 +1,54 @@ +# Troubleshoting: terminal lag and solutions with Nix + +Inspired by this [blog post from +Tavis](https://lock.cmpxchg8b.com/slowterm.html), I decided to document my own +recent journey of reducing terminal lag startup. This post is way less +interesting than the one from Tavis that uses a debugger to patch applications +on the fly, but should still be interesting for some. And it also shows how +powerful Nix can be for some things. + +For context, I have basically 3 systems where I interact with terminal +frequently: + +- Thinkpad P14s Gen 1 running NixOS, with a reasonable fast CPU and disk +- [MacBook Pro "M1 + Pro"](https://everymac.com/systems/apple/macbook_pro/specs/macbook-pro-m1-pro-10-core-cpu-16-core-gpu-16-2021-specs.html) + (what an awful name scheme Apple) with a really fast CPU and disk, but of + course running macOS + + Sadly this is being phased-out since this is a job owned machine and I am + changing jobs right now, but will be replaced with another one soon™ +- [Chromebook Duet + 3](https://chromeunboxed.com/lenovo-chromebook-duet-3-review-perfect-sequel) + running ChromeOS, with slow CPU and disk + +My experience is similar to Tavis, at around 300ms of startup time I don't care +too much, but around 500ms+ is where I start to notice. I never had any issues +with startup time in NixOS itself (I had issues with macOS before, but it was +not actually the fault of macOS), but in the Chromebook it was awful: 600ms+ +with [hot +start](https://www.instabug.com/blog/understanding-cold-hot-and-warm-app-launch-time), +while cold start it could be multiple seconds. + +The shell that I used is ZSH, and we check how long it takes to start by using: + +``` +$ time zsh -ci exit +zsh -ic exit 0.04s user 0.10s system 100% cpu 0.143 total +``` + +The `-i` flag here is important, because we are interested in the interactive +use of ZSH. Without this flag ZSH will ignore your `~/.zshrc` file, and the +results will be meaningless. + +To do a more interesting benchmark, we can use [`hyperfine`](https://github.com/sharkdp/hyperfine): + +``` +$ hyperfine "zsh -ic exit" +Benchmark 1: zsh -ic exit + Time (mean ± σ): 145.4 ms ± 4.2 ms [User: 49.8 ms, System: 97.3 ms] + Range (min … max): 138.6 ms … 155.3 ms 19 runs +``` + +Hyperfine will run the command multiple times and take care of things like +shell startup time. A really great tool to have in your toolbox by the way, but +I digress.