docker_api_stats_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello'; usleep 100000; done")
  15. id := strings.TrimSpace(out)
  16. err := waitRun(id)
  17. c.Assert(err, 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. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo 1")
  36. id := strings.TrimSpace(out)
  37. getGoRoutines := func() int {
  38. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/info"), nil, "")
  39. c.Assert(err, check.IsNil)
  40. info := types.Info{}
  41. err = json.NewDecoder(body).Decode(&info)
  42. c.Assert(err, check.IsNil)
  43. body.Close()
  44. return info.NGoroutines
  45. }
  46. // When the HTTP connection is closed, the number of goroutines should not increase.
  47. routines := getGoRoutines()
  48. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats", id), nil, "")
  49. c.Assert(err, check.IsNil)
  50. body.Close()
  51. t := time.After(30 * time.Second)
  52. for {
  53. select {
  54. case <-t:
  55. c.Assert(getGoRoutines() <= routines, check.Equals, true)
  56. return
  57. default:
  58. if n := getGoRoutines(); n <= routines {
  59. return
  60. }
  61. time.Sleep(200 * time.Millisecond)
  62. }
  63. }
  64. }
  65. func (s *DockerSuite) TestApiNetworkStats(c *check.C) {
  66. testRequires(c, SameHostDaemon)
  67. // Run container for 30 secs
  68. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  69. id := strings.TrimSpace(out)
  70. err := waitRun(id)
  71. c.Assert(err, check.IsNil)
  72. // Retrieve the container address
  73. contIP := findContainerIP(c, id)
  74. numPings := 10
  75. // Get the container networking stats before and after pinging the container
  76. nwStatsPre := getNetworkStats(c, id)
  77. countParam := "-c"
  78. if runtime.GOOS == "windows" {
  79. countParam = "-n" // Ping count parameter is -n on Windows
  80. }
  81. pingout, err := exec.Command("ping", contIP, countParam, strconv.Itoa(numPings)).Output()
  82. pingouts := string(pingout[:])
  83. c.Assert(err, check.IsNil)
  84. nwStatsPost := getNetworkStats(c, id)
  85. // Verify the stats contain at least the expected number of packets (account for ARP)
  86. expRxPkts := 1 + nwStatsPre.RxPackets + uint64(numPings)
  87. expTxPkts := 1 + nwStatsPre.TxPackets + uint64(numPings)
  88. c.Assert(nwStatsPost.TxPackets >= expTxPkts, check.Equals, true,
  89. check.Commentf("Reported less TxPackets than expected. Expected >= %d. Found %d. %s", expTxPkts, nwStatsPost.TxPackets, pingouts))
  90. c.Assert(nwStatsPost.RxPackets >= expRxPkts, check.Equals, true,
  91. check.Commentf("Reported less Txbytes than expected. Expected >= %d. Found %d. %s", expRxPkts, nwStatsPost.RxPackets, pingouts))
  92. }
  93. func getNetworkStats(c *check.C, id string) types.Network {
  94. var st *types.Stats
  95. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
  96. c.Assert(err, check.IsNil)
  97. err = json.NewDecoder(body).Decode(&st)
  98. c.Assert(err, check.IsNil)
  99. body.Close()
  100. return st.Network
  101. }