docker_api_stats_test.go 3.8 KB

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