mem_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // mem_test.go — contraste de memUsagePct con mediciones reales (issue #513).
  2. package adapters
  3. import "testing"
  4. func TestMemUsagePct(t *testing.T) {
  5. cases := []struct {
  6. name string
  7. total, free, buff, cach, av, usedProc float64
  8. want int
  9. }{
  10. {
  11. // Con usedProc (RSS de procesos) se usa ese valor directamente: es
  12. // lo que el usuario percibe como uso real (netgrip #279).
  13. name: "rt3 con usedProc rss", total: 414264, free: 57364, buff: 0, cach: 94136, av: 99684, usedProc: 78000, want: 19,
  14. },
  15. {
  16. // Sin usedProc pero con Cached (fórmula clásica) baja del 79% al 67%.
  17. name: "rt3 con cached", total: 414264, free: 45780, buff: 0, cach: 90464, av: 88088, want: 67,
  18. },
  19. {
  20. // Sin Cached (payload viejo/ubus): se mantiene el comportamiento
  21. // anterior basado en MemAvailable.
  22. name: "rt3 fallback available", total: 414264, free: 45780, buff: 0, cach: 0, av: 88088, want: 79,
  23. },
  24. {
  25. // rt4 AX6: 60% con la fórmula previa; la clásica baja.
  26. name: "rt4 con cached", total: 414264, free: 120000, buff: 0, cach: 48876, av: 165268, want: 59,
  27. },
  28. {
  29. // Sin available, cae a free+buffered.
  30. name: "sin available", total: 1000, free: 100, buff: 50, cach: 0, av: 0, want: 85,
  31. },
  32. {
  33. // usedProc mayor que total se ignora y cae a la fórmula clásica
  34. // (con Cached): used = total - free - buffered - cached.
  35. name: "usedProc invalido", total: 1000, free: 100, buff: 0, cach: 400, av: 0, usedProc: 5000, want: 50,
  36. },
  37. {name: "cached mayor que el resto", total: 100, free: 0, buff: 0, cach: 500, av: 0, want: 0},
  38. {name: "total cero", total: 0, free: 0, buff: 0, cach: 0, av: 0, want: 0},
  39. }
  40. for _, tc := range cases {
  41. got := memUsagePct(tc.total, tc.free, tc.buff, tc.cach, tc.av, tc.usedProc)
  42. if got != tc.want {
  43. t.Errorf("%s: got %d, want %d", tc.name, got, tc.want)
  44. }
  45. }
  46. }