mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add watcher for record code excution time
This commit is contained in:
30
function/watcher.go
Normal file
30
function/watcher.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package function
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Watcher is used for record code excution time
|
||||||
|
type Watcher struct {
|
||||||
|
startTime int64
|
||||||
|
stopTime int64
|
||||||
|
excuting bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the watch timer.
|
||||||
|
func (w *Watcher) Start() {
|
||||||
|
w.startTime = time.Now().UnixNano()
|
||||||
|
w.excuting = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop the watch timer.
|
||||||
|
func (w *Watcher) Stop() {
|
||||||
|
w.stopTime = time.Now().UnixNano()
|
||||||
|
w.excuting = false
|
||||||
|
}
|
||||||
|
// GetElapsedTime get excute elapsed time.
|
||||||
|
func (w *Watcher) GetElapsedTime() time.Duration {
|
||||||
|
if w.excuting {
|
||||||
|
return time.Duration(time.Now().UnixNano() - w.startTime)
|
||||||
|
} else {
|
||||||
|
return time.Duration(w.stopTime - w.startTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
32
function/watcher_test.go
Normal file
32
function/watcher_test.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package function
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWatcher(t *testing.T) {
|
||||||
|
w := &Watcher{}
|
||||||
|
w.Start()
|
||||||
|
|
||||||
|
longRunningTask()
|
||||||
|
|
||||||
|
if !w.excuting {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Stop()
|
||||||
|
|
||||||
|
eapsedTime := w.GetElapsedTime().Milliseconds()
|
||||||
|
t.Log("Elapsed Time (milsecond)", eapsedTime)
|
||||||
|
|
||||||
|
if w.excuting {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func longRunningTask() {
|
||||||
|
var slice []int64
|
||||||
|
for i := 0; i < 10000000; i++ {
|
||||||
|
slice = append(slice, int64(i))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -563,6 +563,7 @@ func TestWithout(t *testing.T) {
|
|||||||
func TestShuffle(t *testing.T) {
|
func TestShuffle(t *testing.T) {
|
||||||
s := []int{1, 2, 3, 4, 5}
|
s := []int{1, 2, 3, 4, 5}
|
||||||
res := Shuffle(s)
|
res := Shuffle(s)
|
||||||
|
t.Log("Shuffle result: ", res)
|
||||||
|
|
||||||
if reflect.TypeOf(s) != reflect.TypeOf(res) {
|
if reflect.TypeOf(s) != reflect.TypeOf(res) {
|
||||||
internal.LogFailedTestInfo(t, "Shuffle", s, res, res)
|
internal.LogFailedTestInfo(t, "Shuffle", s, res, res)
|
||||||
|
|||||||
Reference in New Issue
Block a user