Skip to content

性能优化问题

本章节汇总了开发环境性能优化过程中可能遇到的问题及其解决方案。

📋 问题分类

1. 终端启动慢

Oh My Zsh 启动慢

问题描述

终端启动时间过长,影响使用体验。

解决方案

bash
# 减少 Oh My Zsh 插件数量
nano ~/.zshrc
bash
# 只保留必要的插件
plugins=(
  git
  zsh-autosuggestions
  zsh-syntax-highlighting
)
bash
# 使用异步加载插件
git clone https://github.com/mafredri/zsh-async.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/async

环境变量加载慢

解决方案

bash
# 优化 PATH 配置
echo 'export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"' >> ~/.zshrc
bash
# 使用 lazy loading
echo 'export NVM_LAZY_LOAD=true' >> ~/.zshrc
bash
# 延迟加载 Node.js
echo 'nvm() { unset -f nvm; export NVM_DIR=~/.nvm; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; nvm "$@"; }' >> ~/.zshrc

历史记录问题

解决方案

bash
# 优化历史记录配置
echo 'HISTSIZE=1000' >> ~/.zshrc
echo 'SAVEHIST=1000' >> ~/.zshrc
echo 'HISTFILE=~/.zsh_history' >> ~/.zshrc
echo 'setopt SHARE_HISTORY' >> ~/.zshrc
bash
# 清理历史记录
rm ~/.zsh_history

2. 包管理器慢

npm 安装慢

解决方案

bash
# 配置淘宝镜像
npm config set registry https://registry.npmmirror.com
bash
# 使用 npm 缓存
npm config set cache ~/.npm-cache
bash
# 并行安装
npm install --maxsockets 8

pnpm 安装慢

解决方案

bash
# 配置镜像
pnpm config set registry https://registry.npmmirror.com
bash
# 使用并发安装
pnpm install --network-concurrency 8
bash
# 清理缓存
pnpm store prune

pip 安装慢

解决方案

bash
# 配置国内镜像
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
bash
# 使用并行安装
pip install -U pip
pip install --use-feature=fast-deps package_name
bash
# 使用缓存
pip install --cache-dir ~/.pip-cache package_name

Cargo 编译慢

解决方案

bash
# 配置并行编译
echo 'export RUSTFLAGS="-C target-cpu=native"' >> ~/.zshrc
bash
# 使用国内镜像
mkdir -p ~/.cargo
toml
# ~/.cargo/config
[build]
jobs = 8

[net]
git-fetch-with-cli = true

3. 编辑器卡顿

VS Code 卡顿

解决方案

bash
# 禁用不必要的扩展
code --list-extensions
code --uninstall-extension extension-id
bash
# 清理工作区缓存
rm -rf ~/.vscode/extensions
rm -rf ~/Library/Application\ Support/Code/CachedData
bash
# 优化设置
echo '{
  "files.watcherExclude": {
    "**/node_modules/**": true,
    "**/dist/**": true
  },
  "search.exclude": {
    "**/node_modules": true,
    "**/dist": true
  }
}' > ~/Library/Application\ Support/Code/User/settings.json

Neovim 卡顿

解决方案

bash
# 减少插件数量
nano ~/.config/nvim/init.lua
bash
# 使用异步插件
echo 'use {
  "nvim-telescope/telescope.nvim",
  requires = { "nvim-lua/plenary.nvim" },
  config = function()
    require("telescope").setup({
      defaults = {
        vimgrep_arguments = {
          "rg",
          "--color=never",
          "--no-heading",
          "--with-filename",
          "--line-number",
          "--column",
          "--smart-case",
        },
      },
    })
  end,
}' >> ~/.config/nvim/init.lua
bash
# 优化 Treesitter
nvim --headless -c 'TSUpdate'

4. 系统资源占用

内存占用高

解决方案

bash
# 查看内存使用情况
top -l 1 | head -n 10
bash
# 清理内存缓存
sudo purge
bash
# 关闭不必要的进程
ps aux | grep -E "(node|python|java)" | grep -v grep
kill -9 process_id

CPU 占用高

解决方案

bash
# 查看 CPU 使用情况
top -o cpu
bash
# 限制进程 CPU 使用
cpulimit -p process_id -l 50
bash
# 使用 nice 降低优先级
nice -n 19 command

磁盘空间不足

解决方案

bash
# 查看磁盘使用情况
df -h
bash
# 清理 Homebrew 缓存
brew cleanup
bash
# 清理 npm 缓存
npm cache clean --force
bash
# 清理 pip 缓存
pip cache purge

5. 网络性能问题

网络连接慢

解决方案

bash
# 配置 DNS
echo 'nameserver 8.8.8.8' | sudo tee /etc/resolv.conf
echo 'nameserver 8.8.4.4' | sudo tee -a /etc/resolv.conf
bash
# 使用代理
export https_proxy=http://127.0.0.1:7890
export http_proxy=http://127.0.0.1:7890
bash
# 配置 hosts 文件
echo "140.82.114.4 github.com" | sudo tee -a /etc/hosts

Git 操作慢

解决方案

bash
# 使用浅克隆
git clone --depth 1 https://github.com/username/repo.git
bash
# 配置 Git 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
bash
# 使用 Git 镜像
git config --global url."https://hub.fastgit.xyz/".insteadOf "https://github.com/"

🔍 性能诊断工具

系统监控

bash
# 安装监控工具
brew install htop
brew install iostat
bash
# 使用 htop 监控
htop
bash
# 监控 I/O
iostat 1

网络诊断

bash
# 测试网络速度
curl -o /dev/null -s -w "%{speed_download}\n" https://speed.cloudflare.com/__down
bash
# 测试延迟
ping -c 10 google.com
bash
# 网络路由追踪
traceroute github.com

进程分析

bash
# 查看进程树
pstree
bash
# 查看进程资源使用
ps aux --sort=-%cpu | head -10
ps aux --sort=-%mem | head -10

✅ 性能优化验证

完成性能优化后,验证以下指标:

bash
# 测试终端启动时间
time zsh -i -c exit
bash
# 测试包管理器速度
time npm install lodash
bash
# 测试编辑器启动时间
time code --version
bash
# 测试网络速度
curl -o /dev/null -s -w "下载速度: %{speed_download} bytes/sec\n" https://speed.cloudflare.com/__down

🎯 性能优化建议

系统级优化

  • 使用 SSD 存储
  • 增加内存容量
  • 定期清理系统

应用级优化

  • 减少启动项
  • 使用轻量级工具
  • 配置缓存策略

网络级优化

  • 使用国内镜像
  • 配置合适的代理
  • 优化 DNS 设置

开发环境优化

  • 使用版本管理工具
  • 配置代码缓存
  • 优化构建流程

其他问题? 查看 环境配置问题Shell 和编辑器问题

Released under the MIT License.