merger_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package app_test
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "testing"
  6. "github.com/weaveworks/common/test"
  7. "github.com/weaveworks/scope/app"
  8. "github.com/weaveworks/scope/report"
  9. "github.com/weaveworks/scope/test/reflect"
  10. )
  11. func TestMerger(t *testing.T) {
  12. // Use 3 reports to check the pair-wise merging in SmartMerger
  13. report1 := report.MakeReport()
  14. report1.Endpoint.AddNode(report.MakeNode("foo"))
  15. report2 := report.MakeReport()
  16. report2.Endpoint.AddNode(report.MakeNode("bar"))
  17. report3 := report.MakeReport()
  18. report3.Endpoint.AddNode(report.MakeNode("baz"))
  19. reports := []report.Report{
  20. report1, report2, report3,
  21. }
  22. want := report.MakeReport()
  23. want.Endpoint.AddNode(report.MakeNode("foo"))
  24. want.Endpoint.AddNode(report.MakeNode("bar"))
  25. want.Endpoint.AddNode(report.MakeNode("baz"))
  26. for _, merger := range []app.Merger{app.NewFastMerger()} {
  27. // Test the empty list case
  28. if have := merger.Merge([]report.Report{}); !reflect.DeepEqual(have, report.MakeReport()) {
  29. t.Errorf("Bad merge: %s", test.Diff(have, want))
  30. }
  31. if have := merger.Merge(reports); !reflect.DeepEqual(have, want) {
  32. t.Errorf("Bad merge: %s", test.Diff(have, want))
  33. }
  34. // Repeat the above test to ensure caching works
  35. if have := merger.Merge(reports); !reflect.DeepEqual(have, want) {
  36. t.Errorf("Bad merge: %s", test.Diff(have, want))
  37. }
  38. }
  39. }
  40. func BenchmarkFastMerger(b *testing.B) {
  41. benchmarkMerger(b, app.NewFastMerger())
  42. }
  43. const numHosts = 15
  44. func benchmarkMerger(b *testing.B, merger app.Merger) {
  45. makeReport := func() report.Report {
  46. rpt := report.MakeReport()
  47. for i := 0; i < 100; i++ {
  48. rpt.Endpoint.AddNode(report.MakeNode(fmt.Sprintf("%x", rand.Int63())))
  49. }
  50. return rpt
  51. }
  52. reports := []report.Report{}
  53. for i := 0; i < numHosts*5; i++ {
  54. reports = append(reports, makeReport())
  55. }
  56. replacements := []report.Report{}
  57. for i := 0; i < numHosts/3; i++ {
  58. replacements = append(replacements, makeReport())
  59. }
  60. b.ResetTimer()
  61. for i := 0; i < b.N; i++ {
  62. // replace 1/3 of hosts work of reports & merge them all
  63. for i := 0; i < len(replacements); i++ {
  64. reports[rand.Intn(len(reports))] = replacements[i]
  65. }
  66. merger.Merge(reports)
  67. }
  68. }