docker_api_stats_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/docker/docker/api/types"
  10. "github.com/go-check/check"
  11. )
  12. func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) {
  13. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello'; usleep 100000; done")
  14. id := strings.TrimSpace(out)
  15. err := waitRun(id)
  16. c.Assert(err, check.IsNil)
  17. resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
  18. c.Assert(err, check.IsNil)
  19. c.Assert(resp.ContentLength > 0, check.Equals, true, check.Commentf("should not use chunked encoding"))
  20. c.Assert(resp.Header.Get("Content-Type"), check.Equals, "application/json")
  21. var v *types.Stats
  22. err = json.NewDecoder(body).Decode(&v)
  23. c.Assert(err, check.IsNil)
  24. var cpuPercent = 0.0
  25. cpuDelta := float64(v.CpuStats.CpuUsage.TotalUsage - v.PreCpuStats.CpuUsage.TotalUsage)
  26. systemDelta := float64(v.CpuStats.SystemUsage - v.PreCpuStats.SystemUsage)
  27. cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CpuStats.CpuUsage.PercpuUsage)) * 100.0
  28. if cpuPercent == 0 {
  29. c.Fatalf("docker stats with no-stream get cpu usage failed: was %v", cpuPercent)
  30. }
  31. }
  32. func (s *DockerSuite) TestStoppedContainerStatsGoroutines(c *check.C) {
  33. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo 1")
  34. id := strings.TrimSpace(out)
  35. getGoRoutines := func() int {
  36. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/info"), nil, "")
  37. c.Assert(err, check.IsNil)
  38. info := types.Info{}
  39. err = json.NewDecoder(body).Decode(&info)
  40. c.Assert(err, check.IsNil)
  41. body.Close()
  42. return info.NGoroutines
  43. }
  44. // When the HTTP connection is closed, the number of goroutines should not increase.
  45. routines := getGoRoutines()
  46. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats", id), nil, "")
  47. c.Assert(err, check.IsNil)
  48. body.Close()
  49. t := time.After(30 * time.Second)
  50. for {
  51. select {
  52. case <-t:
  53. c.Assert(getGoRoutines() <= routines, check.Equals, true)
  54. return
  55. default:
  56. if n := getGoRoutines(); n <= routines {
  57. return
  58. }
  59. time.Sleep(200 * time.Millisecond)
  60. }
  61. }
  62. }
  63. func (s *DockerSuite) TestApiNetworkStats(c *check.C) {
  64. // Run container for 30 secs
  65. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  66. id := strings.TrimSpace(out)
  67. err := waitRun(id)
  68. c.Assert(err, check.IsNil)
  69. // Retrieve the container address
  70. contIP := findContainerIP(c, id)
  71. numPings := 10
  72. // Get the container networking stats before and after pinging the container
  73. nwStatsPre := getNetworkStats(c, id)
  74. _, err = exec.Command("ping", contIP, "-c", strconv.Itoa(numPings)).Output()
  75. c.Assert(err, check.IsNil)
  76. nwStatsPost := getNetworkStats(c, id)
  77. // Verify the stats contain at least the expected number of packets (account for ARP)
  78. expRxPkts := 1 + nwStatsPre.RxPackets + uint64(numPings)
  79. expTxPkts := 1 + nwStatsPre.TxPackets + uint64(numPings)
  80. c.Assert(nwStatsPost.TxPackets >= expTxPkts, check.Equals, true,
  81. check.Commentf("Reported less TxPackets than expected. Expected >= %d. Found %d", expTxPkts, nwStatsPost.TxPackets))
  82. c.Assert(nwStatsPost.RxPackets >= expRxPkts, check.Equals, true,
  83. check.Commentf("Reported less Txbytes than expected. Expected >= %d. Found %d", expRxPkts, nwStatsPost.RxPackets))
  84. }
  85. func getNetworkStats(c *check.C, id string) types.Network {
  86. var st *types.Stats
  87. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
  88. c.Assert(err, check.IsNil)
  89. err = json.NewDecoder(body).Decode(&st)
  90. c.Assert(err, check.IsNil)
  91. return st.Network
  92. }