暫無描述

filter_test.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package profile
  15. import (
  16. "regexp"
  17. "testing"
  18. )
  19. func TestFilter(t *testing.T) {
  20. // Perform several forms of filtering on the test profile.
  21. type filterTestcase struct {
  22. focus, ignore, hide, show *regexp.Regexp
  23. fm, im, hm, hnm bool
  24. }
  25. for tx, tc := range []filterTestcase{
  26. {
  27. fm: true, // nil focus matches every sample
  28. },
  29. {
  30. focus: regexp.MustCompile("notfound"),
  31. },
  32. {
  33. ignore: regexp.MustCompile("foo.c"),
  34. fm: true,
  35. im: true,
  36. },
  37. {
  38. hide: regexp.MustCompile("lib.so"),
  39. fm: true,
  40. hm: true,
  41. },
  42. {
  43. show: regexp.MustCompile("foo.c"),
  44. fm: true,
  45. hnm: true,
  46. },
  47. {
  48. show: regexp.MustCompile("notfound"),
  49. fm: true,
  50. },
  51. } {
  52. prof := *testProfile1.Copy()
  53. gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
  54. if gf != tc.fm {
  55. t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
  56. }
  57. if gi != tc.im {
  58. t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
  59. }
  60. if gh != tc.hm {
  61. t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
  62. }
  63. if gnh != tc.hnm {
  64. t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
  65. }
  66. }
  67. }
  68. func TestTagFilter(t *testing.T) {
  69. // Perform several forms of tag filtering on the test profile.
  70. type filterTestcase struct {
  71. include, exclude *regexp.Regexp
  72. im, em bool
  73. count int
  74. }
  75. countTags := func(p *Profile) map[string]bool {
  76. tags := make(map[string]bool)
  77. for _, s := range p.Sample {
  78. for l := range s.Label {
  79. tags[l] = true
  80. }
  81. for l := range s.NumLabel {
  82. tags[l] = true
  83. }
  84. }
  85. return tags
  86. }
  87. for tx, tc := range []filterTestcase{
  88. {nil, nil, true, false, 3},
  89. {regexp.MustCompile("notfound"), nil, false, false, 0},
  90. {regexp.MustCompile("key1"), nil, true, false, 1},
  91. {nil, regexp.MustCompile("key[12]"), true, true, 1},
  92. } {
  93. prof := testProfile1.Copy()
  94. gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
  95. if gim != tc.im {
  96. t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
  97. }
  98. if gem != tc.em {
  99. t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
  100. }
  101. if tags := countTags(prof); len(tags) != tc.count {
  102. t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
  103. }
  104. }
  105. }