diff --git a/function/watcher.go b/function/watcher.go new file mode 100644 index 0000000..c7a0b6c --- /dev/null +++ b/function/watcher.go @@ -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) + } +} \ No newline at end of file diff --git a/function/watcher_test.go b/function/watcher_test.go new file mode 100644 index 0000000..dec36eb --- /dev/null +++ b/function/watcher_test.go @@ -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)) + } +} diff --git a/slice/slice_test.go b/slice/slice_test.go index 690f6f2..ae2076f 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -563,6 +563,7 @@ func TestWithout(t *testing.T) { func TestShuffle(t *testing.T) { s := []int{1, 2, 3, 4, 5} res := Shuffle(s) + t.Log("Shuffle result: ", res) if reflect.TypeOf(s) != reflect.TypeOf(res) { internal.LogFailedTestInfo(t, "Shuffle", s, res, res)