Profiling `sh` invocations with `fakesh`

I’ve updated f8ksh (previous announcement) to enable invocation logging and counting (in addition to parsing shebang lines in commands — %sh{#! perl} …). Basically,

F8KSH_FAKESH_COUNT=true [F8KSH_FAKESH_DEBUG=true F8KSH_FAKESH_DATE="date +%s%N"] \
  KAKOUNE_POSIX_SHELL=~/src/fakesh.sh kak

… will generate a counter in /tmp/fakesh_cnt.log, and a timestamped log (including scripts being executed) in /tmp/fakesh.log (formatted itself as a shell script). You can even monitor plugins’ dirty work :laughing: in real time with

tail -f /tmp/fakesh.log

Then,

cd /tmp
fakesh-log-conv --create-sql --logfunc=2sql <fakesh.log >fakesh.sql
sqlite3 /tmp/fakesh.sqlite3 <fakesh.sql

will load the logs in an sqlite3 database where they can be further disected.

Or don’t enable _DEBUG and just use the counter, it will reduce the Heisenberg effect.

One thing I found out is that Kakoune runs considerably faster using busybox (KAKOUNE_POSIX_SHELL=/bin/static-sh, at least on Debians; you may want to change fakesh’s own shebang when profiling). This is because busybox provides sed, grep, awk, tail and many more as builtins.

Full docs at the project homepage; also see the scripting with external tools thread. I’m going to look into profiling kak startup and some common plugins as time permits.

Note that fakesh is not Kakoune-specific — you can use it with vim, or any other tool that makes shell calls.

2 Likes

This is great—hopefully, I’ll be able to use it to profile my plugins and make them run buttery smooth.
Going to install and try it now.

EDIT: This is brilliant both for powering up your kak scripting and for digging in deep and learning about the internal operations of shell scripting! Really glad I followed the link on this one.