You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

231 regels
5.5 KiB

  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. "strings"
  17. "testing"
  18. )
  19. func TestPrune(t *testing.T) {
  20. for _, test := range []struct {
  21. in *Profile
  22. want string
  23. }{
  24. {in1, out1},
  25. {in2, out2},
  26. } {
  27. in := test.in.Copy()
  28. in.RemoveUninteresting()
  29. if err := in.CheckValid(); err != nil {
  30. t.Error(err)
  31. }
  32. w := strings.Split(test.want, "\n")
  33. for i, g := range strings.Split(in.String(), "\n") {
  34. if i >= len(w) {
  35. t.Fatalf("got trailing %s", g)
  36. }
  37. if strings.TrimSpace(g) != strings.TrimSpace(w[i]) {
  38. t.Fatalf(`%d: got: "%s" want:"%s"`, i, g, w[i])
  39. }
  40. }
  41. }
  42. }
  43. var funs = []*Function{
  44. {ID: 1, Name: "main", SystemName: "main", Filename: "main.c"},
  45. {ID: 2, Name: "fun1", SystemName: "fun1", Filename: "fun.c"},
  46. {ID: 3, Name: "fun2", SystemName: "fun2", Filename: "fun.c"},
  47. {ID: 4, Name: "fun3", SystemName: "fun3", Filename: "fun.c"},
  48. {ID: 5, Name: "fun4", SystemName: "fun4", Filename: "fun.c"},
  49. {ID: 6, Name: "fun5", SystemName: "fun5", Filename: "fun.c"},
  50. {ID: 7, Name: "unsimplified_fun(int)", SystemName: "unsimplified_fun(int)", Filename: "fun.c"},
  51. {ID: 8, Name: "Foo::(anonymous namespace)::Test::Bar", SystemName: "Foo::(anonymous namespace)::Test::Bar", Filename: "fun.c"},
  52. {ID: 9, Name: "Hello::(anonymous namespace)::World(const Foo::(anonymous namespace)::Test::Bar)", SystemName: "Hello::(anonymous namespace)::World(const Foo::(anonymous namespace)::Test::Bar)", Filename: "fun.c"},
  53. {ID: 10, Name: "Foo::operator()(::Bar)", SystemName: "Foo::operator()(::Bar)", Filename: "fun.c"},
  54. }
  55. var locs1 = []*Location{
  56. {
  57. ID: 1,
  58. Line: []Line{
  59. {Function: funs[0], Line: 1},
  60. },
  61. },
  62. {
  63. ID: 2,
  64. Line: []Line{
  65. {Function: funs[1], Line: 2},
  66. {Function: funs[2], Line: 1},
  67. },
  68. },
  69. {
  70. ID: 3,
  71. Line: []Line{
  72. {Function: funs[3], Line: 2},
  73. {Function: funs[1], Line: 1},
  74. },
  75. },
  76. {
  77. ID: 4,
  78. Line: []Line{
  79. {Function: funs[3], Line: 2},
  80. {Function: funs[1], Line: 2},
  81. {Function: funs[5], Line: 2},
  82. },
  83. },
  84. }
  85. var in1 = &Profile{
  86. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  87. Period: 1,
  88. DurationNanos: 10e9,
  89. SampleType: []*ValueType{
  90. {Type: "samples", Unit: "count"},
  91. {Type: "cpu", Unit: "milliseconds"},
  92. },
  93. Sample: []*Sample{
  94. {
  95. Location: []*Location{locs1[0]},
  96. Value: []int64{1, 1},
  97. },
  98. {
  99. Location: []*Location{locs1[1], locs1[0]},
  100. Value: []int64{1, 1},
  101. },
  102. {
  103. Location: []*Location{locs1[2], locs1[0]},
  104. Value: []int64{1, 1},
  105. },
  106. {
  107. Location: []*Location{locs1[3], locs1[0]},
  108. Value: []int64{1, 1},
  109. },
  110. {
  111. Location: []*Location{locs1[3], locs1[2], locs1[1], locs1[0]},
  112. Value: []int64{1, 1},
  113. },
  114. },
  115. Location: locs1,
  116. Function: funs,
  117. DropFrames: "fu.*[12]|banana",
  118. KeepFrames: ".*[n2][n2]",
  119. }
  120. const out1 = `PeriodType: cpu milliseconds
  121. Period: 1
  122. Duration: 10s
  123. Samples:
  124. samples/count cpu/milliseconds
  125. 1 1: 1
  126. 1 1: 2 1
  127. 1 1: 1
  128. 1 1: 4 1
  129. 1 1: 2 1
  130. Locations
  131. 1: 0x0 main main.c:1 s=0
  132. 2: 0x0 fun2 fun.c:1 s=0
  133. 3: 0x0 fun3 fun.c:2 s=0
  134. fun1 fun.c:1 s=0
  135. 4: 0x0 fun5 fun.c:2 s=0
  136. Mappings
  137. `
  138. var locs2 = []*Location{
  139. {
  140. ID: 1,
  141. Line: []Line{
  142. {Function: funs[0], Line: 1},
  143. },
  144. },
  145. {
  146. ID: 2,
  147. Line: []Line{
  148. {Function: funs[6], Line: 1},
  149. },
  150. },
  151. {
  152. ID: 3,
  153. Line: []Line{
  154. {Function: funs[7], Line: 1},
  155. },
  156. },
  157. {
  158. ID: 4,
  159. Line: []Line{
  160. {Function: funs[8], Line: 1},
  161. },
  162. },
  163. {
  164. ID: 5,
  165. Line: []Line{
  166. {Function: funs[9], Line: 1},
  167. },
  168. },
  169. }
  170. var in2 = &Profile{
  171. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  172. Period: 1,
  173. DurationNanos: 10e9,
  174. SampleType: []*ValueType{
  175. {Type: "samples", Unit: "count"},
  176. {Type: "cpu", Unit: "milliseconds"},
  177. },
  178. Sample: []*Sample{
  179. // Unsimplified name with parameters shouldn't match.
  180. {
  181. Location: []*Location{locs2[1], locs2[0]},
  182. Value: []int64{1, 1},
  183. },
  184. // .*Foo::.*::Bar.* should (and will be dropped) regardless of the anonymous namespace.
  185. {
  186. Location: []*Location{locs2[2], locs2[0]},
  187. Value: []int64{1, 1},
  188. },
  189. // .*Foo::.*::Bar.* shouldn't match inside the parameter list.
  190. {
  191. Location: []*Location{locs2[3], locs2[0]},
  192. Value: []int64{1, 1},
  193. },
  194. // .*operator\(\) should match, regardless of parameters.
  195. {
  196. Location: []*Location{locs2[4], locs2[0]},
  197. Value: []int64{1, 1},
  198. },
  199. },
  200. Location: locs2,
  201. Function: funs,
  202. DropFrames: `unsimplified_fun\(int\)|.*Foo::.*::Bar.*|.*operator\(\)`,
  203. }
  204. const out2 = `PeriodType: cpu milliseconds
  205. Period: 1
  206. Duration: 10s
  207. Samples:
  208. samples/count cpu/milliseconds
  209. 1 1: 2 1
  210. 1 1: 1
  211. 1 1: 4 1
  212. 1 1: 1
  213. Locations
  214. 1: 0x0 main main.c:1 s=0
  215. 2: 0x0 unsimplified_fun(int) fun.c:1 s=0
  216. 3: 0x0 Foo::(anonymous namespace)::Test::Bar fun.c:1 s=0
  217. 4: 0x0 Hello::(anonymous namespace)::World(const Foo::(anonymous namespace)::Test::Bar) fun.c:1 s=0
  218. 5: 0x0 Foo::operator()(::Bar) fun.c:1 s=0
  219. Mappings
  220. `