Skip to content

Commit

Permalink
benchmark: apply additional fixes for zsh
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Nov 11, 2022
1 parent 0f7c04b commit 8cb9b84
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/ChangeLog.md
Expand Up @@ -313,7 +313,7 @@
- main. util: fix problems of readlink etc. found by test in macOS (reported by aiotter) `#D1849` fa955c1 `#D1855` a22e145
- progcomp: fix a bug that `COMP_WORDBREAKS` is ignored `#D1872` 4d2dd35
- global: quote `return $?` `#D1884` 801d14a
- benchmark (zsh): fix for `KSH_ARRAYS` `#D1886` a144ffa
- benchmark (zsh): fix for `KSH_ARRAYS` `#D1886` a144ffa xxxxxxx

## Documentation

Expand Down
30 changes: 30 additions & 0 deletions note.txt
Expand Up @@ -1855,6 +1855,33 @@ bash_tips
- leakvars
- keymap の移動 (これは別 commit にする?)

2022-10-28

* edit: display-shell-version
https://github.com/akinomyoga/ble.sh/issues/246#issuecomment-1294843777

直接 ble/widget/display-shell-version を実行すれば良い。
C-x C-v が効かない端末でもOK

* edit: display-shell-version
https://github.com/akinomyoga/ble.sh/issues/246#issuecomment-1294893636

git, gmake 及び gawk の version も表示する?

2022-10-24

* rename blerc.template
https://github.com/akinomyoga/ble.sh/issues/244

* 複数 widget を実行したい時: Q&A や wiki にも説明を書く
https://github.com/akinomyoga/ble.sh/discussions/241#discussioncomment-3937949

* cancel line by C-c in vi mode
https://github.com/akinomyoga/ble.sh/discussions/241

* delete word by M-backspace
https://github.com/akinomyoga/ble.sh/issues/243

2022-10-02

* prompt: dirty 状態の更新時にカーソル位置がずれる。
Expand Down Expand Up @@ -6722,6 +6749,9 @@ bash_tips
これは fe751acb の最初に benchmark.sh を追加した時からそのままである。
実際 zsh の振る舞いを調べてみると KSH_ARRAYS を設定していると match の中身はずれる 。

2022-11-11 実際に zsh の上で使おうとしたらたくさんの修正が必要になっ
た。取り敢えず現状で分かっている分を追加修正した。

2022-09-26

* vte で unsupported modifyOtherKeys のシーケンスが画面に表示されてしまう (reported by dongxi8) [#D1885]
Expand Down
56 changes: 35 additions & 21 deletions src/benchmark.sh
Expand Up @@ -21,16 +21,16 @@ function ble-measure/.loop {
if [[ ${ZSH_VERSION-} ]]; then
_ble_measure_resolution=1000 # [usec]
function ble-measure/.time {
local result
local result=
result=$({ time ( ble-measure/.loop "$n" "$1" ; ) } 2>&1 )
#local result=$({ time ( ble-measure/.loop "$n" "$1" &>/dev/null); } 2>&1)
result=${result##*cpu }
local rex='(([0-9]+):)?([0-9]+)\.([0-9]+) total$'
if [[ $result =~ $rex ]]; then
if [[ -o KSH_ARRAYS ]]; then
local m=${match[0]} s=${match[1]} ms=${match[2]}
else
local m=${match[1]} s=${match[2]} ms=${match[3]}
else
local m=${match[2]} s=${match[3]} ms=${match[4]}
fi
m=${m:-0} ms=${ms}000; ms=${ms:0:3}

Expand Down Expand Up @@ -79,8 +79,9 @@ else
fi

_ble_measure_base= # [nsec]
_ble_measure_base_real=()
_ble_measure_base_nestcost=0 # [nsec/10]
_ble_measure_base_real=()
_ble_measure_base_guess=()
_ble_measure_count=1 # 同じ倍率で _ble_measure_count 回計測して最小を取る。
_ble_measure_threshold=100000 # 一回の計測が threshold [usec] 以上になるようにする

Expand All @@ -97,14 +98,15 @@ function ble-measure/calibrate.8 { ble-measure/calibrate.7; }
function ble-measure/calibrate.9 { ble-measure/calibrate.8; }
function ble-measure/calibrate.A { ble-measure/calibrate.9; }
function ble-measure/calibrate {
local ret nsec
local ret= nsec=

local calibrate_count=1
_ble_measure_base=0
_ble_measure_base_nestcost=0

# nest0: calibrate.0 の ble-measure 内部での ${#FUNCNAME[*]}
local nest0=$((${#FUNCNAME[@]}+2))
[[ ${ZSH_VERSION-} ]] && nest0=$((${#funcstack[@]}+2))
ble-measure/calibrate.0; local x0=$nsec
ble-measure/calibrate.A; local xA=$nsec
local nest_cost=$((xA-x0))
Expand All @@ -118,7 +120,7 @@ function ble-measure/fit {

local calibrate_count=10

local c nest_level=${#FUNCNAME[@]}
local c= nest_level=${#FUNCNAME[@]}
for c in {0..9} A; do
"ble-measure/calibrate.$c"
ble/util/print "$((nest_level++)) $nsec"
Expand All @@ -132,10 +134,12 @@ EOF
}

## @fn ble-measure/.read-arguments.get-optarg
## @var[in,out] args iarg arg i c
## @var[in] args arg i c
## @var[in,out] iarg
## @var[out] optarg
function ble-measure/.read-arguments.get-optarg {
if ((i+1<${#arg})); then
optarg=${arg:i+1}
optarg=${arg:$((i+1))}
i=${#arg}
return 0
elif ((iarg<${#args[@]})); then
Expand All @@ -154,6 +158,7 @@ function ble-measure/.read-arguments.get-optarg {
function ble-measure/.read-arguments {
local -a args; args=("$@")
local iarg=0 optarg=
[[ ${ZSH_VERSION-} && ! -o KSH_ARRAYS ]] && iarg=1
while [[ ${args[iarg]} == -* ]]; do
local arg=${args[iarg++]}
case $arg in
Expand All @@ -164,9 +169,9 @@ function ble-measure/.read-arguments {
ble/util/print "ble-measure: unrecognized option '$arg'."
flags=E$flags ;;
(-?*)
local i c
local i= c= # Note: zsh prints the values with just "local i c"
for ((i=1;i<${#arg};i++)); do
c=${arg:i:1}
c=${arg:$i:1}
case $c in
(q) flags=qV$flags ;;
([ca])
Expand All @@ -189,7 +194,11 @@ function ble-measure/.read-arguments {
esac
done
local IFS=$' \t\n'
command="${args[*]:iarg}"
if [[ ${ZSH_VERSION-} ]]; then
command="$args[$iarg,-1]"
else
command="${args[*]:$iarg}"
fi
[[ $flags != *E* ]]
}

Expand All @@ -205,6 +214,7 @@ function ble-measure/.read-arguments {
## 実行時間を nsec 単位で返します。
function ble-measure {
local __level=${#FUNCNAME[@]} __base=
[[ ${ZSH_VERSION-} ]] && __level=${#funcstack[@]}
local flags= command= count=$_ble_measure_count
local measure_threshold=$_ble_measure_threshold
ble-measure/.read-arguments "$@" || return "$?"
Expand Down Expand Up @@ -237,24 +247,28 @@ function ble-measure {
__base=$((_ble_measure_base+_ble_measure_base_nestcost*__level/10))
else
# それ以外の時は __level 毎に計測
if [[ ! $ble_measure_calibrate && ! ${_ble_measure_base[__level]} ]]; then
if [[ ! $ble_measure_calibrate && ! ${_ble_measure_base_guess[__level]} ]]; then
if [[ ! ${_ble_measure_base_real[__level+1]} ]]; then
local ble_measure_calibrate=1
ble-measure -qc3 -B 0 ''
ble/util/unlocal ble_measure_calibrate
_ble_measure_base_real[__level+1]=$nsec
_ble_measure_base[__level+1]=$nsec
_ble_measure_base_guess[__level+1]=$nsec
fi

# linear-fit result in chatoyancy
# 65.9818 pm 2.945 (4.463%)
# 4356.75 pm 19.97 (0.4585%)
# 上の実測値は一つ上のレベル (__level+1) での結果になるので現在のレベル
# (__level) の値に補正する。レベル毎の時間が chatoyancy での線形フィッ
# トの結果に比例する仮定して補正を行う。
#
# linear-fit result with $f(x) = A x + B$ in chatoyancy
# A = 65.9818 pm 2.945 (4.463%)
# B = 4356.75 pm 19.97 (0.4585%)
local A=6598 B=435675
nsec=${_ble_measure_base_real[__level+1]}
_ble_measure_base[__level]=$((nsec*(B+A*(__level-1))/(B+A*__level)))
_ble_measure_base_guess[__level]=$((nsec*(B+A*(__level-1))/(B+A*__level)))
ble/util/unlocal A B
fi
__base=${_ble_measure_base[__level]:-0}
__base=${_ble_measure_base_guess[__level]:-0}
fi
fi

Expand Down Expand Up @@ -296,9 +310,9 @@ function ble-measure {
[[ ${_ble_measure_base_real[__level]} ]] &&
((__real<_ble_measure_base_real[__level])) &&
_ble_measure_base_real[__level]=$__real
[[ ${_ble_measure_base[__level]} ]] &&
((__real<_ble_measure_base[__level])) &&
_ble_measure_base[__level]=$__real
[[ ${_ble_measure_base_guess[__level]} ]] &&
((__real<_ble_measure_base_guess[__level])) &&
_ble_measure_base_guess[__level]=$__real
((__real<__base)) &&
__base=$__real
fi
Expand Down

0 comments on commit 8cb9b84

Please sign in to comment.