我如何分析 Fish shell init?

How can I profile the Fish shell init?

当我开始一个新的shell(例如运行等的一些函数)时,有没有办法查看每件事花费了多少时间。

可能吗?

我唯一能想到的帮助就是做这样的事情:

~
λ time fish -i -c exit

________________________________________________________
Executed in  142.29 millis    fish           external
   usr time   76.19 millis   68.00 micros   76.12 millis
   sys time   61.52 millis  469.00 micros   61.05 millis

然后尝试移除一些东西并再次测量...虽然不理想。

Fish 具有 built-in 分析功能。

从 fish 3.2 开始,它 --profile 只分析您给出的命令,--profile-startup 分析启动时间,在此之前 --profile 会同时分析两者。

点赞

fish --profile-startup /tmp/fish.profile -i -c exit

这将创建一个名为“/tmp/fish.profile”的文件,看起来像

Time    Sum Command
415 2505    > builtin source /usr/share/fish/config.fish
15  15  -> set -g IFS \n\ \t
6   6   -> set -qg __fish_added_user_paths
4   4   -> set -g __fish_added_user_paths
1   4   -> if not set -q __fish_initialized...
3   3   --> not set -q __fish_initialized
3   3   -> function __fish_default_command_not_found_handler...
4   72  -> if status --is-interactive...

第一列是命令本身所用的时间(以微秒为单位,所以 1000000 是 1 秒),第二列是它及其所有部分(包括函数调用和命令替换)所用的时间,第三列是命令加上调用堆栈中有多深的指示器。

查看最大的时间投资 sort -nk2 /tmp/fish.profile 很有用 - 这按第二列排序,因此它将最后显示大件商品(即最接近下一个提示)。

请注意,这也显示了 fish 的内部启动 - 例如 set -g IFS 是 fish 内部的东西,但往往很快,所以不会很突出。