benchmark_internal_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package app
  2. import (
  3. "context"
  4. "flag"
  5. "math/rand"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "github.com/weaveworks/scope/render"
  12. "github.com/weaveworks/scope/render/detailed"
  13. "github.com/weaveworks/scope/report"
  14. "github.com/weaveworks/scope/test/fixture"
  15. )
  16. var (
  17. benchReportPath = flag.String("bench-report-path", "", "report file, or dir with files, to use for benchmarking (relative to this package)")
  18. )
  19. func readReportFiles(b *testing.B, path string) []report.Report {
  20. reports := []report.Report{}
  21. if err := filepath.Walk(path,
  22. func(p string, info os.FileInfo, err error) error {
  23. if err != nil {
  24. return err
  25. }
  26. if info.IsDir() {
  27. return nil
  28. }
  29. rpt, err := report.MakeFromFile(context.Background(), p)
  30. if err != nil {
  31. return err
  32. }
  33. reports = append(reports, *rpt)
  34. return nil
  35. }); err != nil {
  36. b.Fatal(err)
  37. }
  38. return reports
  39. }
  40. func BenchmarkReportUnmarshal(b *testing.B) {
  41. b.ResetTimer()
  42. for i := 0; i < b.N; i++ {
  43. readReportFiles(b, *benchReportPath)
  44. }
  45. }
  46. func upgradeReports(reports []report.Report) []report.Report {
  47. upgraded := make([]report.Report, len(reports))
  48. for i, r := range reports {
  49. upgraded[i] = r.Upgrade()
  50. }
  51. return upgraded
  52. }
  53. func BenchmarkReportUpgrade(b *testing.B) {
  54. reports := readReportFiles(b, *benchReportPath)
  55. b.ResetTimer()
  56. for i := 0; i < b.N; i++ {
  57. upgradeReports(reports)
  58. }
  59. }
  60. func BenchmarkReportMerge(b *testing.B) {
  61. reports := upgradeReports(readReportFiles(b, *benchReportPath))
  62. rand.Shuffle(len(reports), func(i, j int) {
  63. reports[i], reports[j] = reports[j], reports[i]
  64. })
  65. merger := NewFastMerger()
  66. b.ResetTimer()
  67. for i := 0; i < b.N; i++ {
  68. merger.Merge(reports)
  69. }
  70. }
  71. func getReport(b *testing.B) report.Report {
  72. r := fixture.Report
  73. if *benchReportPath != "" {
  74. r = NewFastMerger().Merge(upgradeReports(readReportFiles(b, *benchReportPath)))
  75. }
  76. return r
  77. }
  78. func benchmarkRender(b *testing.B, f func(report.Report)) {
  79. r := getReport(b)
  80. r.UnsafeRemovePartMergedNodes(context.Background())
  81. b.ResetTimer()
  82. for i := 0; i < b.N; i++ {
  83. b.StopTimer()
  84. render.ResetCache()
  85. b.StartTimer()
  86. f(r)
  87. }
  88. }
  89. func renderForTopology(b *testing.B, topologyID string, report report.Report) report.Nodes {
  90. renderer, filter, err := topologyRegistry.RendererForTopology(topologyID, url.Values{}, report)
  91. if err != nil {
  92. b.Fatal(err)
  93. }
  94. return render.Render(context.Background(), report, renderer, filter).Nodes
  95. }
  96. func benchmarkRenderTopology(b *testing.B, topologyID string) {
  97. benchmarkRender(b, func(report report.Report) {
  98. renderForTopology(b, topologyID, report)
  99. })
  100. }
  101. func BenchmarkRenderList(b *testing.B) {
  102. benchmarkRender(b, func(report report.Report) {
  103. topologyRegistry.renderTopologies(context.Background(), report, &http.Request{Form: url.Values{}})
  104. })
  105. }
  106. func BenchmarkRenderHosts(b *testing.B) {
  107. benchmarkRenderTopology(b, "hosts")
  108. }
  109. func BenchmarkRenderControllers(b *testing.B) {
  110. benchmarkRenderTopology(b, "kube-controllers")
  111. }
  112. func BenchmarkRenderPods(b *testing.B) {
  113. benchmarkRenderTopology(b, "pods")
  114. }
  115. func BenchmarkRenderContainers(b *testing.B) {
  116. benchmarkRenderTopology(b, "containers")
  117. }
  118. func BenchmarkRenderProcesses(b *testing.B) {
  119. benchmarkRenderTopology(b, "processes")
  120. }
  121. func BenchmarkRenderProcessNames(b *testing.B) {
  122. benchmarkRenderTopology(b, "processes-by-name")
  123. }
  124. func benchmarkSummarizeTopology(b *testing.B, topologyID string) {
  125. ctx := context.Background()
  126. r := getReport(b)
  127. rc := detailed.RenderContext{Report: r}
  128. nodes := renderForTopology(b, topologyID, r)
  129. b.ResetTimer()
  130. for i := 0; i < b.N; i++ {
  131. detailed.Summaries(ctx, rc, nodes)
  132. }
  133. }
  134. func BenchmarkSummarizeHosts(b *testing.B) {
  135. benchmarkSummarizeTopology(b, "hosts")
  136. }
  137. func BenchmarkSummarizeControllers(b *testing.B) {
  138. benchmarkSummarizeTopology(b, "kube-controllers")
  139. }
  140. func BenchmarkSummarizePods(b *testing.B) {
  141. benchmarkSummarizeTopology(b, "pods")
  142. }
  143. func BenchmarkSummarizeContainers(b *testing.B) {
  144. benchmarkSummarizeTopology(b, "containers")
  145. }
  146. func BenchmarkSummarizeProcesses(b *testing.B) {
  147. benchmarkSummarizeTopology(b, "processes")
  148. }
  149. func BenchmarkSummarizeProcessNames(b *testing.B) {
  150. benchmarkSummarizeTopology(b, "processes-by-name")
  151. }