Skip to content

Commit

Permalink
added build constraints to hide unix specific code from win builds (#107
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sha1n authored Jun 12, 2021
1 parent 521c00a commit 9d10273
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 29 deletions.
43 changes: 43 additions & 0 deletions pkg/cpu_timer_unix.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions pkg/cpu_timer_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 23 additions & 29 deletions pkg/trace.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"syscall"
"time"

"github.com/sha1n/benchy/api"
Expand All @@ -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
Expand Down Expand Up @@ -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
// }

0 comments on commit 9d10273

Please sign in to comment.