Skip to content

Commit

Permalink
benchmark (ble-measure): work around the locale for "EPOCHREALTIME"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Feb 20, 2021
1 parent 33c283e commit 1aa471b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
4 changes: 3 additions & 1 deletion memo/ChangeLog.md
Expand Up @@ -37,6 +37,7 @@
- complete (mandb): fix an encoding prpblem of utf8 manuals `#D1446` 7a4a480
- util (`ble/util/msleep`): fix hang in Cygwin by swithing from `/dev/udp/0.0.0.0/80` to `/dev/zero` `#D1452` d4d718a
- syntax: fix broken AST with `[[` keyword `#D1454` 69658ef
- benchmark (`ble-measure`): work around a locale-dependent decimal point of `EPOCHREALTIME` (reported by 3ximus) `#D1460` 0000000
- global:work around bash-4.2 bug of `declare -gA` (reported by 0xC0ncord) `#D1470` 8856a04
- global: fix declaration of associative arrays for `ble-reload` `#D1471` 3cae6e4

Expand All @@ -47,7 +48,8 @@
- util: add function `ble/string#quote-words` `#D1451` f03b87b
- syntax (`ble/syntax:bash/simple-word/eval`): cache `#D1453` 6d8311e
- global: refactor `setup => set up / set-up` `#D1456` c37a9dd
- global: clean up helps of user functions `#D1459` 0000000
- global: clean up helps of user functions `#D1459` 33c283e
- benchmark (`ble-measure`): support `-T TIME` and `-B TIME` option `#D1460` 0000000

<!---------------------------------------------------------------------------->
# ble-0.4.0-devel2
Expand Down
6 changes: 6 additions & 0 deletions note.txt
Expand Up @@ -3813,6 +3813,12 @@ bash_tips

2021-02-03

* benchmark: EPOCHREALTIME は LC_NUMERIC 依存 (reported by 3ximus) [#D1460]

報告の画像で出ているエラーが何だろうと思って ble-measure の実装を
見たが変な事はない。と思ったが此処で気づいた。EPOCHREALTIME の小数
点は locale 依存である。という訳で対策する事にした。

* global: help の類の整理 [#D1459]
* bleopt: --help に対応していない
* blehook: --help が単純すぎる
Expand Down
58 changes: 42 additions & 16 deletions src/benchmark.sh
Expand Up @@ -41,11 +41,16 @@ if [[ $ZSH_VERSION ]]; then
}
elif ((BASH_VERSINFO[0]>=5)); then
_ble_measure_resolution=1 # [usec]
function ble-measure/.get-realtime {
local LC_ALL= LC_NUMERIC=C
time=$EPOCHREALTIME
}
function ble-measure/.time {
local command="$*"
local time1=${EPOCHREALTIME//.}
local time
ble-measure/.get-realtime 2>/dev/null; local time1=${time//.}
ble-measure/.loop "$n" "$*" &>/dev/null
local time2=${EPOCHREALTIME//.}
ble-measure/.get-realtime 2>/dev/null; local time2=${time//.}
((utot=time2-time1,usec=utot/n))
((utot>0))
}
Expand Down Expand Up @@ -113,12 +118,31 @@ function ble-measure/calibrate {
_ble_measure_base_nestcost=$nest_cost
}

## @fn ble-measure/.read-arguments.get-optarg
## @var[in,out] args iarg arg i c
function ble-measure/.read-arguments.get-optarg {
if ((i+1<${#arg})); then
optarg=${arg:i+1}
i=${#arg}
return 0
elif ((iarg<${#args[@]})); then
optarg=${args[iarg++]}
return 0
else
ble/util/print "ble-measure: missing option argument for '-$c'."
flags=E$flags
return 1
fi
}

## @fn ble-measure/.read-arguments args
## @var[out] flags
## @var[out] command count
function ble-measure/.read-arguments {
while [[ $1 == -* ]]; do
local arg=$1; shift
local -a args; args=("$@")
local iarg=0 optarg=
while [[ ${args[iarg]} == -* ]]; do
local arg=${args[iarg++]}
case $arg in
(--) break ;;
(--help) flags=h$flags ;;
Expand All @@ -133,14 +157,13 @@ function ble-measure/.read-arguments {
(q) flags=q$flags ;;
([ca])
[[ $c == a ]] && flags=a$flags
if ((i+1<${#arg})); then
count=${arg:i+1}; break
elif (($#)); then
count=$1; shift
else
ble/util/print "ble-measure: missing option argument for '-$c'."
flags=E$flags
fi ;;
ble-measure/.read-arguments.get-optarg && count=$optarg ;;
(T)
ble-measure/.read-arguments.get-optarg &&
measure_threshold=$optarg ;;
(B)
ble-measure/.read-arguments.get-optarg &&
measure_base=$optarg measure_nestcost= ;;
(*)
ble/util/print "ble-measure: unrecognized option '-$c'."
flags=E$flags ;;
Expand All @@ -151,7 +174,7 @@ function ble-measure/.read-arguments {
flags=E$flags ;;
esac
done
command="$*"
command="${args[*]:iarg}"
[[ $flags != *E* ]]
}

Expand All @@ -177,6 +200,9 @@ function ble-measure {
fi

local flags= command= count=$_ble_measure_count
local measure_threshold=$_ble_measure_threshold
local measure_base=$_ble_measure_base
local measure_nestcost=$_ble_measure_base_nestcost
ble-measure/.read-arguments "$@" || return "$?"
if [[ $flags == *h* ]]; then
ble/util/print-lines \
Expand Down Expand Up @@ -204,7 +230,7 @@ function ble-measure {
local prev_n= prev_utot=
local -i n
for n in {1,10,100,1000,10000}\*{1,2,5}; do
[[ $prev_n ]] && ((n/prev_n<=10 && prev_utot*n/prev_n<_ble_measure_threshold*2/5 && n!=50000)) && continue
[[ $prev_n ]] && ((n/prev_n<=10 && prev_utot*n/prev_n<measure_threshold*2/5 && n!=50000)) && continue

local utot=0 usec=0
[[ $flags != *q* ]] && printf '%s (x%d)...' "$command" "$n" >&2
Expand All @@ -213,7 +239,7 @@ function ble-measure {

prev_n=$n prev_utot=$utot

((utot >= _ble_measure_threshold)) || continue
((utot >= measure_threshold)) || continue

# 繰り返し計測して最小値 (-a の時は平均値) を採用
if [[ $count ]]; then
Expand All @@ -233,7 +259,7 @@ function ble-measure {
fi
fi

local nsec0=$((_ble_measure_base+_ble_measure_base_nestcost*${#FUNCNAME[*]}/10))
local nsec0=$((measure_base+measure_nestcost*${#FUNCNAME[*]}/10))
if [[ $flags != *q* ]]; then
local reso=$_ble_measure_resolution
local awk=ble/bin/awk
Expand Down
5 changes: 4 additions & 1 deletion src/util.sh
Expand Up @@ -3563,17 +3563,20 @@ _ble_util_clock_base=
_ble_util_clock_reso=
_ble_util_clock_type=
function ble/util/clock/.initialize {
local LC_ALL= LC_NUMERIC=C
if ((_ble_bash>=50000)) && [[ $EPOCHREALTIME == *.???* ]]; then
# implementation with EPOCHREALTIME
_ble_util_clock_base=$((10#${EPOCHREALTIME%.*}))
_ble_util_clock_reso=1
_ble_util_clock_type=EPOCHREALTIME
function ble/util/clock {
local LC_ALL= LC_NUMERIC=C
local now=$EPOCHREALTIME
local integral=$((10#${now%%.*}-_ble_util_clock_base))
local mantissa=${now#*.}000; mantissa=${mantissa::3}
((ret=integral*1000+10#$mantissa))
}
ble/function#suppress-stderr ble/util/clock # locale
elif [[ -r /proc/uptime ]] && {
local uptime
ble/util/readfile uptime /proc/uptime
Expand Down Expand Up @@ -3609,7 +3612,7 @@ function ble/util/clock/.initialize {
}
fi
}
ble/util/clock/.initialize
ble/util/clock/.initialize 2>/dev/null

if ((_ble_bash>=40000)); then
## @fn[custom] ble/util/idle/IS_IDLE { ble/util/is-stdin-ready; }
Expand Down

0 comments on commit 1aa471b

Please sign in to comment.