stopwatch.go 809 B

123456789101112131415161718192021222324252627282930313233343536
  1. package udptransfer
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. type watch struct {
  8. label string
  9. t1 time.Time
  10. }
  11. func StartWatch(s string) *watch {
  12. return &watch{
  13. label: s,
  14. t1: time.Now(),
  15. }
  16. }
  17. func (w *watch) StopLoops(loop int, size int) {
  18. tu := time.Now().Sub(w.t1).Nanoseconds()
  19. timePerLoop := float64(tu) / float64(loop)
  20. throughput := float64(loop*size) * 1e6 / float64(tu)
  21. tu_ms := float64(tu) / 1e6
  22. fmt.Fprintf(os.Stderr, "%s tu=%.2f ms tpl=%.0f ns throughput=%.2f K/s\n", w.label, tu_ms, timePerLoop, throughput)
  23. }
  24. var _kt = float64(1e9 / 1024)
  25. func (w *watch) Stop(size int) {
  26. tu := time.Now().Sub(w.t1).Nanoseconds()
  27. throughput := float64(size) * _kt / float64(tu)
  28. tu_ms := float64(tu) / 1e6
  29. fmt.Fprintf(os.Stderr, "%s tu=%.2f ms throughput=%.2f K/s\n", w.label, tu_ms, throughput)
  30. }