docker_api_stats_test.go 4.1 KB

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