From 9d10273f2781be62c56214a35c0539a1661723d5 Mon Sep 17 00:00:00 2001 From: Shai Nagar Date: Sat, 12 Jun 2021 18:53:30 +0300 Subject: [PATCH] added build constraints to hide unix specific code from win builds (#107) --- pkg/cpu_timer_unix.go | 43 +++++++++++++++++++++++++++++++++ pkg/cpu_timer_windows.go | 25 +++++++++++++++++++ pkg/trace.go | 52 ++++++++++++++++++---------------------- 3 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 pkg/cpu_timer_unix.go create mode 100644 pkg/cpu_timer_windows.go diff --git a/pkg/cpu_timer_unix.go b/pkg/cpu_timer_unix.go new file mode 100644 index 0000000..fb32b08 --- /dev/null +++ b/pkg/cpu_timer_unix.go @@ -0,0 +1,43 @@ +// +build linux darwin +// +build amd64 arm64 + +package pkg + +import ( + "syscall" + "time" +) + +type unixChildrenCPUTimer struct { + r syscall.Rusage + sysTimeStart time.Time + usrTimeStart time.Time +} + +func newChildrenCPUTimer() CPUTimer { + return &unixChildrenCPUTimer{} +} + +func (t *unixChildrenCPUTimer) Start() func() (time.Duration, time.Duration) { + err := syscall.Getrusage(syscall.RUSAGE_CHILDREN, &t.r) + if err != nil { + panic(err) + } + + t.sysTimeStart = time.Unix(t.r.Stime.Unix()) + t.usrTimeStart = time.Unix(t.r.Utime.Unix()) + + return t.Elapsed +} + +func (t *unixChildrenCPUTimer) Elapsed() (usr time.Duration, sys time.Duration) { + err := syscall.Getrusage(syscall.RUSAGE_CHILDREN, &t.r) + if err != nil { + panic(err) + } + + sysTimeEnd := time.Unix(t.r.Stime.Unix()) + usrTimeEnd := time.Unix(t.r.Utime.Unix()) + + return usrTimeEnd.Sub(t.usrTimeStart), sysTimeEnd.Sub(t.sysTimeStart) +} diff --git a/pkg/cpu_timer_windows.go b/pkg/cpu_timer_windows.go new file mode 100644 index 0000000..5f67dad --- /dev/null +++ b/pkg/cpu_timer_windows.go @@ -0,0 +1,25 @@ +// +build windows +// +build amd64 arm + +package pkg + +import ( + "time" +) + +// NoopCPUTimer NOOP implementation of CPUTimer +type NoopCPUTimer struct{} + +func newChildrenCPUTimer() CPUTimer { + return NoopCPUTimer{} +} + +// Start ... +func (t NoopCPUTimer) Start() func() (time.Duration, time.Duration) { + return t.Elapsed +} + +// Elapsed return 0, 0 +func (t NoopCPUTimer) Elapsed() (usr time.Duration, sys time.Duration) { + return time.Nanosecond * 0, time.Nanosecond * 0 +} diff --git a/pkg/trace.go b/pkg/trace.go index 70de037..ba1a9df 100644 --- a/pkg/trace.go +++ b/pkg/trace.go @@ -1,7 +1,6 @@ package pkg import ( - "syscall" "time" "github.com/sha1n/benchy/api" @@ -13,7 +12,7 @@ type tracer struct { type trace struct { id string - cpuTimer *childrenCPUTimer + cpuTimer CPUTimer start time.Time perceivedTime time.Duration usrTime time.Duration @@ -77,36 +76,31 @@ func (tr *tracer) Stream() chan api.Trace { return tr.stream } -type childrenCPUTimer struct { - r syscall.Rusage - sysTimeStart time.Time - usrTimeStart time.Time +// CPUTimer an abstraction for a platform system CPU timer +type CPUTimer interface { + Start() func() (time.Duration, time.Duration) + Elapsed() (usr time.Duration, sys time.Duration) } -func newChildrenCPUTimer() *childrenCPUTimer { - return &childrenCPUTimer{} -} +// func newChildrenCPUTimer() CPUTimer { +// switch runtime.GOOS { +// case "windows": +// return NoopCPUTimer{} +// default: -func (t *childrenCPUTimer) Start() func() (time.Duration, time.Duration) { - err := syscall.Getrusage(syscall.RUSAGE_CHILDREN, &t.r) - if err != nil { - panic(err) - } +// return newUnixChildrenCPUTimer() +// } +// } - t.sysTimeStart = time.Unix(t.r.Stime.Unix()) - t.usrTimeStart = time.Unix(t.r.Utime.Unix()) +// // NoopCPUTimer NOOP implementation of CPUTimer +// type NoopCPUTimer struct{} - return t.Elapsed -} +// // Start ... +// func (t NoopCPUTimer) Start() func() (time.Duration, time.Duration) { +// return t.Elapsed +// } -func (t *childrenCPUTimer) Elapsed() (usr time.Duration, sys time.Duration) { - err := syscall.Getrusage(syscall.RUSAGE_CHILDREN, &t.r) - if err != nil { - panic(err) - } - - sysTimeEnd := time.Unix(t.r.Stime.Unix()) - usrTimeEnd := time.Unix(t.r.Utime.Unix()) - - return usrTimeEnd.Sub(t.usrTimeStart), sysTimeEnd.Sub(t.sysTimeStart) -} +// // Elapsed return 0, 0 +// func (t NoopCPUTimer) Elapsed() (usr time.Duration, sys time.Duration) { +// return time.Nanosecond * 0, time.Nanosecond * 0 +// }