docker_api_stats_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // Get the container networking stats before and after pinging the container
  77. nwStatsPre := getNetworkStats(c, id)
  78. countParam := "-c"
  79. if runtime.GOOS == "windows" {
  80. countParam = "-n" // Ping count parameter is -n on Windows
  81. }
  82. pingout, err := exec.Command("ping", contIP, countParam, strconv.Itoa(numPings)).Output()
  83. pingouts := string(pingout[:])
  84. c.Assert(err, check.IsNil)
  85. nwStatsPost := getNetworkStats(c, id)
  86. // Verify the stats contain at least the expected number of packets (account for ARP)
  87. expRxPkts := 1 + nwStatsPre.RxPackets + uint64(numPings)
  88. expTxPkts := 1 + nwStatsPre.TxPackets + uint64(numPings)
  89. c.Assert(nwStatsPost.TxPackets >= expTxPkts, check.Equals, true,
  90. check.Commentf("Reported less TxPackets than expected. Expected >= %d. Found %d. %s", expTxPkts, nwStatsPost.TxPackets, pingouts))
  91. c.Assert(nwStatsPost.RxPackets >= expRxPkts, check.Equals, true,
  92. check.Commentf("Reported less Txbytes than expected. Expected >= %d. Found %d. %s", expRxPkts, nwStatsPost.RxPackets, pingouts))
  93. }
  94. func getNetworkStats(c *check.C, id string) types.NetworkStats {
  95. var st *types.Stats
  96. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
  97. c.Assert(err, check.IsNil)
  98. err = json.NewDecoder(body).Decode(&st)
  99. c.Assert(err, check.IsNil)
  100. body.Close()
  101. return st.Network
  102. }