Skip to content

Commit cb5e01b

Browse files
committed
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.
1 parent bc78fed commit cb5e01b

File tree

5 files changed

+29
-46
lines changed

5 files changed

+29
-46
lines changed

.goreleaser.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ builds:
111111
- amd64
112112
- arm
113113
- arm64
114+
-
115+
id: "get-epochtime"
116+
main: ./cmd/get-epochtime
117+
binary: bin/resh-get-epochtime
118+
goarch:
119+
- 386
120+
- amd64
121+
- arm
122+
- arm64
114123

115124
# signs:
116125
# - artifacts: checksum

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ GOFLAGS=-ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.de
77

88
build: submodules bin/resh-session-init bin/resh-collect bin/resh-postcollect\
99
bin/resh-daemon bin/resh-control bin/resh-config bin/resh-cli\
10-
bin/resh-install-utils bin/resh-generate-uuid
10+
bin/resh-install-utils bin/resh-generate-uuid bin/resh-get-epochtime
1111

1212
install: build
1313
scripts/install.sh

cmd/get-epochtime/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// Small utility to get epochtime in millisecond precision
9+
// Doesn't check arguments
10+
// Exits with status 1 on error
11+
func main() {
12+
fmt.Printf("%s", timeToEpochTime(time.Now()))
13+
}
14+
15+
func timeToEpochTime(t time.Time) string {
16+
return fmt.Sprintf("%.2f", float64(t.UnixMilli())/1000)
17+
}

scripts/hooks.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ __resh_collect() {
2020
local __RESH_SHLVL="$SHLVL"
2121
local __RESH_GIT_REMOTE; __RESH_GIT_REMOTE="$(git remote get-url origin 2>/dev/null)"
2222

23-
# __RESH_RT_BEFORE="$EPOCHREALTIME"
24-
__RESH_RT_BEFORE=$(__resh_get_epochrealtime)
23+
__RESH_RT_BEFORE=$(resh-get-epochtime)
2524

2625
if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then
2726
# shellcheck source=shellrc.sh
@@ -61,7 +60,7 @@ __resh_precmd() {
6160
local __RESH_EXIT_CODE=$?
6261
local __RESH_RT_AFTER
6362
local __RESH_SHLVL="$SHLVL"
64-
__RESH_RT_AFTER=$(__resh_get_epochrealtime)
63+
__RESH_RT_AFTER=$(resh-get-epochtime)
6564
if [ -n "${__RESH_COLLECT}" ]; then
6665
if [ "$__RESH_VERSION" != "$(resh-postcollect -version)" ]; then
6766
# shellcheck source=shellrc.sh

scripts/util.sh

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,6 @@
22

33
# util.sh - resh utility functions
44

5-
__resh_get_pid() {
6-
if [ -n "${ZSH_VERSION-}" ]; then
7-
# assume Zsh
8-
local __RESH_PID="$$" # current pid
9-
elif [ -n "${BASH_VERSION-}" ]; then
10-
# assume Bash
11-
if [ "${BASH_VERSINFO[0]}" -ge "4" ]; then
12-
# $BASHPID is only available in bash4+
13-
# $$ is fairly similar so it should not be an issue
14-
local __RESH_PID="$BASHPID" # current pid
15-
else
16-
local __RESH_PID="$$" # current pid
17-
fi
18-
fi
19-
echo "$__RESH_PID"
20-
}
21-
22-
__resh_get_epochrealtime() {
23-
if date +%s.%N | grep -vq 'N'; then
24-
# GNU date
25-
date +%s.%N
26-
elif gdate --version >/dev/null && gdate +%s.%N | grep -vq 'N'; then
27-
# GNU date take 2
28-
gdate +%s.%N
29-
elif [ -n "${ZSH_VERSION-}" ]; then
30-
# zsh fallback using $EPOCHREALTIME
31-
if [ -z "${__RESH_ZSH_LOADED_DATETIME+x}" ]; then
32-
zmodload zsh/datetime
33-
__RESH_ZSH_LOADED_DATETIME=1
34-
fi
35-
echo "$EPOCHREALTIME"
36-
else
37-
# dumb date
38-
# XXX: we lost precison beyond seconds
39-
date +%s
40-
if [ -z "${__RESH_DATE_WARN+x}" ]; then
41-
echo "resh WARN: can't get precise time - consider installing GNU date!"
42-
__RESH_DATE_WARN=1
43-
fi
44-
fi
45-
}
46-
475
# FIXME: figure out if stdout/stderr should be discarded
486
__resh_run_daemon() {
497
if [ -n "${ZSH_VERSION-}" ]; then

0 commit comments

Comments
 (0)