allocs.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package tstest
  4. import (
  5. "fmt"
  6. "runtime"
  7. "testing"
  8. "time"
  9. )
  10. // MinAllocsPerRun asserts that f can run with no more than target allocations.
  11. // It runs f up to 1000 times or 5s, whichever happens first.
  12. // If f has executed more than target allocations on every run, it returns a non-nil error.
  13. //
  14. // MinAllocsPerRun sets GOMAXPROCS to 1 during its measurement and restores
  15. // it before returning.
  16. func MinAllocsPerRun(t *testing.T, target uint64, f func()) error {
  17. defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
  18. var memstats runtime.MemStats
  19. var min, max, sum uint64
  20. start := time.Now()
  21. var iters int
  22. for {
  23. runtime.ReadMemStats(&memstats)
  24. startMallocs := memstats.Mallocs
  25. f()
  26. runtime.ReadMemStats(&memstats)
  27. mallocs := memstats.Mallocs - startMallocs
  28. // TODO: if mallocs < target, return an error? See discussion in #3204.
  29. if mallocs <= target {
  30. return nil
  31. }
  32. if min == 0 || mallocs < min {
  33. min = mallocs
  34. }
  35. if mallocs > max {
  36. max = mallocs
  37. }
  38. sum += mallocs
  39. iters++
  40. if iters == 1000 || time.Since(start) > 5*time.Second {
  41. break
  42. }
  43. }
  44. return fmt.Errorf("min allocs = %d, max allocs = %d, avg allocs/run = %f, want run with <= %d allocs", min, max, float64(sum)/float64(iters), target)
  45. }