暫無描述

report_test.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 report
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "path/filepath"
  19. "regexp"
  20. "testing"
  21. "github.com/google/pprof/internal/binutils"
  22. "github.com/google/pprof/internal/proftest"
  23. "github.com/google/pprof/profile"
  24. )
  25. type testcase struct {
  26. rpt *Report
  27. want string
  28. }
  29. func TestSource(t *testing.T) {
  30. const path = "testdata/"
  31. sampleValue1 := func(v []int64) int64 {
  32. return v[1]
  33. }
  34. for _, tc := range []testcase{
  35. {
  36. rpt: New(
  37. testProfile.Copy(),
  38. &Options{
  39. OutputFormat: List,
  40. Symbol: regexp.MustCompile(`.`),
  41. Title: filepath.Base(testProfile.Mapping[0].File),
  42. SampleValue: sampleValue1,
  43. SampleUnit: testProfile.SampleType[1].Unit,
  44. },
  45. ),
  46. want: path + "source.rpt",
  47. },
  48. {
  49. rpt: New(
  50. testProfile.Copy(),
  51. &Options{
  52. OutputFormat: Dot,
  53. CallTree: true,
  54. Symbol: regexp.MustCompile(`.`),
  55. Title: filepath.Base(testProfile.Mapping[0].File),
  56. SampleValue: sampleValue1,
  57. SampleUnit: testProfile.SampleType[1].Unit,
  58. },
  59. ),
  60. want: path + "source.dot",
  61. },
  62. } {
  63. b := bytes.NewBuffer(nil)
  64. if err := Generate(b, tc.rpt, &binutils.Binutils{}); err != nil {
  65. t.Fatalf("%s: %v", tc.want, err)
  66. }
  67. gold, err := ioutil.ReadFile(tc.want)
  68. if err != nil {
  69. t.Fatalf("%s: %v", tc.want, err)
  70. }
  71. if string(b.String()) != string(gold) {
  72. d, err := proftest.Diff(gold, b.Bytes())
  73. if err != nil {
  74. t.Fatalf("%s: %v", "source", err)
  75. }
  76. t.Error("source" + "\n" + string(d) + "\n" + "gold:\n" + tc.want)
  77. }
  78. }
  79. }
  80. var testM = []*profile.Mapping{
  81. {
  82. ID: 1,
  83. HasFunctions: true,
  84. HasFilenames: true,
  85. HasLineNumbers: true,
  86. HasInlineFrames: true,
  87. },
  88. }
  89. var testF = []*profile.Function{
  90. {
  91. ID: 1,
  92. Name: "main",
  93. Filename: "testdata/source1",
  94. },
  95. {
  96. ID: 2,
  97. Name: "foo",
  98. Filename: "testdata/source1",
  99. },
  100. {
  101. ID: 3,
  102. Name: "bar",
  103. Filename: "testdata/source1",
  104. },
  105. {
  106. ID: 4,
  107. Name: "tee",
  108. Filename: "testdata/source2",
  109. },
  110. }
  111. var testL = []*profile.Location{
  112. {
  113. ID: 1,
  114. Mapping: testM[0],
  115. Line: []profile.Line{
  116. {
  117. Function: testF[0],
  118. Line: 2,
  119. },
  120. },
  121. },
  122. {
  123. ID: 2,
  124. Mapping: testM[0],
  125. Line: []profile.Line{
  126. {
  127. Function: testF[1],
  128. Line: 4,
  129. },
  130. },
  131. },
  132. {
  133. ID: 3,
  134. Mapping: testM[0],
  135. Line: []profile.Line{
  136. {
  137. Function: testF[2],
  138. Line: 10,
  139. },
  140. },
  141. },
  142. {
  143. ID: 4,
  144. Mapping: testM[0],
  145. Line: []profile.Line{
  146. {
  147. Function: testF[3],
  148. Line: 2,
  149. },
  150. },
  151. },
  152. {
  153. ID: 5,
  154. Mapping: testM[0],
  155. Line: []profile.Line{
  156. {
  157. Function: testF[3],
  158. Line: 8,
  159. },
  160. },
  161. },
  162. }
  163. var testProfile = &profile.Profile{
  164. PeriodType: &profile.ValueType{Type: "cpu", Unit: "millisecond"},
  165. Period: 10,
  166. DurationNanos: 10e9,
  167. SampleType: []*profile.ValueType{
  168. {Type: "samples", Unit: "count"},
  169. {Type: "cpu", Unit: "cycles"},
  170. },
  171. Sample: []*profile.Sample{
  172. {
  173. Location: []*profile.Location{testL[0]},
  174. Value: []int64{1, 1},
  175. },
  176. {
  177. Location: []*profile.Location{testL[2], testL[1], testL[0]},
  178. Value: []int64{1, 10},
  179. },
  180. {
  181. Location: []*profile.Location{testL[4], testL[2], testL[0]},
  182. Value: []int64{1, 100},
  183. },
  184. {
  185. Location: []*profile.Location{testL[3], testL[0]},
  186. Value: []int64{1, 1000},
  187. },
  188. {
  189. Location: []*profile.Location{testL[4], testL[3], testL[0]},
  190. Value: []int64{1, 10000},
  191. },
  192. },
  193. Location: testL,
  194. Function: testF,
  195. Mapping: testM,
  196. }