usage_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package stat
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/wuntsong-org/go-zero-plus/core/logx/logtest"
  7. )
  8. func TestBToMb(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. bytes uint64
  12. expected float32
  13. }{
  14. {
  15. name: "Test 1: Convert 0 bytes to MB",
  16. bytes: 0,
  17. expected: 0,
  18. },
  19. {
  20. name: "Test 2: Convert 1048576 bytes to MB",
  21. bytes: 1048576,
  22. expected: 1,
  23. },
  24. {
  25. name: "Test 3: Convert 2097152 bytes to MB",
  26. bytes: 2097152,
  27. expected: 2,
  28. },
  29. }
  30. for _, test := range tests {
  31. t.Run(test.name, func(t *testing.T) {
  32. result := bToMb(test.bytes)
  33. assert.Equal(t, test.expected, result)
  34. })
  35. }
  36. }
  37. func TestPrintUsage(t *testing.T) {
  38. c := logtest.NewCollector(t)
  39. printUsage()
  40. output := c.String()
  41. assert.Contains(t, output, "CPU:")
  42. assert.Contains(t, output, "MEMORY:")
  43. assert.Contains(t, output, "Alloc=")
  44. assert.Contains(t, output, "TotalAlloc=")
  45. assert.Contains(t, output, "Sys=")
  46. assert.Contains(t, output, "NumGC=")
  47. lines := strings.Split(output, "\n")
  48. assert.Len(t, lines, 2)
  49. fields := strings.Split(lines[0], ", ")
  50. assert.Len(t, fields, 5)
  51. }