metrics_test.go 771 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package stat
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. )
  9. func TestMetrics(t *testing.T) {
  10. logx.Disable()
  11. DisableLog()
  12. defer logEnabled.Set(true)
  13. counts := []int{1, 5, 10, 100, 1000, 1000}
  14. for _, count := range counts {
  15. m := NewMetrics("foo")
  16. m.SetName("bar")
  17. for i := 0; i < count; i++ {
  18. m.Add(Task{
  19. Duration: time.Millisecond * time.Duration(i),
  20. Description: strconv.Itoa(i),
  21. })
  22. }
  23. m.AddDrop()
  24. var writer mockedWriter
  25. SetReportWriter(&writer)
  26. m.executor.Flush()
  27. assert.Equal(t, "bar", writer.report.Name)
  28. }
  29. }
  30. type mockedWriter struct {
  31. report *StatReport
  32. }
  33. func (m *mockedWriter) Write(report *StatReport) error {
  34. m.report = report
  35. return nil
  36. }