Commit 8d3047df authored by Michal Berger's avatar Michal Berger Committed by Tomasz Zawadzki
Browse files

test/common: Add timing_cmd() function



This function is meant to return the actual execution time of a given
command without relaying on separate time accounting (i.e. what timing()
is doing with date).

Change-Id: I0ea31ca484aa0c7aa14bc47c72ba67c4a16f9a0a
Signed-off-by: default avatarMichal Berger <michal.berger@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/17995


Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
parent 9ad576c6
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -687,6 +687,24 @@ function timing() {
	fi
}

function timing_cmd() (
	# The use-case here is this: ts=$(timing_cmd echo bar). Since stdout is always redirected
	# to a pipe handling the $(), lookup the stdin's device and determine if it's sane to send
	# cmd's output to it. If not, just null it.

	[[ -t 0 ]] && exec {cmd_out}>&0 || exec {cmd_out}> /dev/null

	local time=0 TIMEFORMAT=%2R # seconds

	# We redirect cmd's std{out,err} to a separate fd dup'ed to stdin's device (or /dev/null) to
	# catch only output from the time builtin - output from the actual cmd would be still visible,
	# but $() will return just the time's data, hence making it possible to just do:
	#  time_of_super_verbose_cmd=$(timing_cmd super_verbose_cmd)
	time=$({ time "$@" >&"$cmd_out" 2>&1; } 2>&1)

	echo "$time"
)

function timing_enter() {
	xtrace_disable
	timing "enter" "$1"