From cb5e01bd07e6f11fce5596224279cd8812cfeb35 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Sat, 31 Dec 2022 12:03:26 +0100 Subject: [PATCH] Use Go binary to get epochtime I have benchmarked: - 'date +%s.%N' -> 1.3ms - 'resh-get-epochtime' -> 2.1ms - 'date --version' -> 1.1ms This means that it's better to run the Go binary once than running 'date' multiple times to check that it is installed and then to get the time. Also it's just much simpler and robust to have one binary instead of shell function with multiple if-else branches. --- .goreleaser.yml | 9 +++++++++ Makefile | 2 +- cmd/get-epochtime/main.go | 17 ++++++++++++++++ scripts/hooks.sh | 5 ++--- scripts/util.sh | 42 --------------------------------------- 5 files changed, 29 insertions(+), 46 deletions(-) create mode 100644 cmd/get-epochtime/main.go diff --git a/.goreleaser.yml b/.goreleaser.yml index cefc5d7..b364a83 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -111,6 +111,15 @@ builds: - amd64 - arm - arm64 + - + id: "get-epochtime" + main: ./cmd/get-epochtime + binary: bin/resh-get-epochtime + goarch: + - 386 + - amd64 + - arm + - arm64 # signs: # - artifacts: checksum diff --git a/Makefile b/Makefile index fa33c86..7e23455 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ GOFLAGS=-ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.de build: submodules bin/resh-session-init bin/resh-collect bin/resh-postcollect\ bin/resh-daemon bin/resh-control bin/resh-config bin/resh-cli\ - bin/resh-install-utils bin/resh-generate-uuid + bin/resh-install-utils bin/resh-generate-uuid bin/resh-get-epochtime install: build scripts/install.sh diff --git a/cmd/get-epochtime/main.go b/cmd/get-epochtime/main.go new file mode 100644 index 0000000..e63c1533 --- /dev/null +++ b/cmd/get-epochtime/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "time" +) + +// Small utility to get epochtime in millisecond precision +// Doesn't check arguments +// Exits with status 1 on error +func main() { + fmt.Printf("%s", timeToEpochTime(time.Now())) +} + +func timeToEpochTime(t time.Time) string { + return fmt.Sprintf("%.2f", float64(t.UnixMilli())/1000) +} diff --git a/scripts/hooks.sh b/scripts/hooks.sh index 9372e20..6b34dee 100644 --- a/scripts/hooks.sh +++ b/scripts/hooks.sh @@ -20,8 +20,7 @@ __resh_collect() { local __RESH_SHLVL="$SHLVL" local __RESH_GIT_REMOTE; __RESH_GIT_REMOTE="$(git remote get-url origin 2>/dev/null)" - # __RESH_RT_BEFORE="$EPOCHREALTIME" - __RESH_RT_BEFORE=$(__resh_get_epochrealtime) + __RESH_RT_BEFORE=$(resh-get-epochtime) if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then # shellcheck source=shellrc.sh @@ -61,7 +60,7 @@ __resh_precmd() { local __RESH_EXIT_CODE=$? local __RESH_RT_AFTER local __RESH_SHLVL="$SHLVL" - __RESH_RT_AFTER=$(__resh_get_epochrealtime) + __RESH_RT_AFTER=$(resh-get-epochtime) if [ -n "${__RESH_COLLECT}" ]; then if [ "$__RESH_VERSION" != "$(resh-postcollect -version)" ]; then # shellcheck source=shellrc.sh diff --git a/scripts/util.sh b/scripts/util.sh index 428c5c6..abe354e 100644 --- a/scripts/util.sh +++ b/scripts/util.sh @@ -2,48 +2,6 @@ # util.sh - resh utility functions -__resh_get_pid() { - if [ -n "${ZSH_VERSION-}" ]; then - # assume Zsh - local __RESH_PID="$$" # current pid - elif [ -n "${BASH_VERSION-}" ]; then - # assume Bash - if [ "${BASH_VERSINFO[0]}" -ge "4" ]; then - # $BASHPID is only available in bash4+ - # $$ is fairly similar so it should not be an issue - local __RESH_PID="$BASHPID" # current pid - else - local __RESH_PID="$$" # current pid - fi - fi - echo "$__RESH_PID" -} - -__resh_get_epochrealtime() { - if date +%s.%N | grep -vq 'N'; then - # GNU date - date +%s.%N - elif gdate --version >/dev/null && gdate +%s.%N | grep -vq 'N'; then - # GNU date take 2 - gdate +%s.%N - elif [ -n "${ZSH_VERSION-}" ]; then - # zsh fallback using $EPOCHREALTIME - if [ -z "${__RESH_ZSH_LOADED_DATETIME+x}" ]; then - zmodload zsh/datetime - __RESH_ZSH_LOADED_DATETIME=1 - fi - echo "$EPOCHREALTIME" - else - # dumb date - # XXX: we lost precison beyond seconds - date +%s - if [ -z "${__RESH_DATE_WARN+x}" ]; then - echo "resh WARN: can't get precise time - consider installing GNU date!" - __RESH_DATE_WARN=1 - fi - fi -} - # FIXME: figure out if stdout/stderr should be discarded __resh_run_daemon() { if [ -n "${ZSH_VERSION-}" ]; then