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.
pull/184/head
Simon Let 3 years ago
parent bc78fed469
commit cb5e01bd07
  1. 9
      .goreleaser.yml
  2. 2
      Makefile
  3. 17
      cmd/get-epochtime/main.go
  4. 5
      scripts/hooks.sh
  5. 42
      scripts/util.sh

@ -111,6 +111,15 @@ builds:
- amd64 - amd64
- arm - arm
- arm64 - arm64
-
id: "get-epochtime"
main: ./cmd/get-epochtime
binary: bin/resh-get-epochtime
goarch:
- 386
- amd64
- arm
- arm64
# signs: # signs:
# - artifacts: checksum # - artifacts: checksum

@ -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\ 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-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 install: build
scripts/install.sh scripts/install.sh

@ -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)
}

@ -20,8 +20,7 @@ __resh_collect() {
local __RESH_SHLVL="$SHLVL" local __RESH_SHLVL="$SHLVL"
local __RESH_GIT_REMOTE; __RESH_GIT_REMOTE="$(git remote get-url origin 2>/dev/null)" local __RESH_GIT_REMOTE; __RESH_GIT_REMOTE="$(git remote get-url origin 2>/dev/null)"
# __RESH_RT_BEFORE="$EPOCHREALTIME" __RESH_RT_BEFORE=$(resh-get-epochtime)
__RESH_RT_BEFORE=$(__resh_get_epochrealtime)
if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then
# shellcheck source=shellrc.sh # shellcheck source=shellrc.sh
@ -61,7 +60,7 @@ __resh_precmd() {
local __RESH_EXIT_CODE=$? local __RESH_EXIT_CODE=$?
local __RESH_RT_AFTER local __RESH_RT_AFTER
local __RESH_SHLVL="$SHLVL" local __RESH_SHLVL="$SHLVL"
__RESH_RT_AFTER=$(__resh_get_epochrealtime) __RESH_RT_AFTER=$(resh-get-epochtime)
if [ -n "${__RESH_COLLECT}" ]; then if [ -n "${__RESH_COLLECT}" ]; then
if [ "$__RESH_VERSION" != "$(resh-postcollect -version)" ]; then if [ "$__RESH_VERSION" != "$(resh-postcollect -version)" ]; then
# shellcheck source=shellrc.sh # shellcheck source=shellrc.sh

@ -2,48 +2,6 @@
# util.sh - resh utility functions # 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 # FIXME: figure out if stdout/stderr should be discarded
__resh_run_daemon() { __resh_run_daemon() {
if [ -n "${ZSH_VERSION-}" ]; then if [ -n "${ZSH_VERSION-}" ]; then

Loading…
Cancel
Save