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.
 
 
 

172 lines
3.6 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. "bytes"
  17. "testing"
  18. "github.com/google/pprof/internal/proftest"
  19. )
  20. var testM = []*Mapping{
  21. {
  22. ID: 1,
  23. Start: 1,
  24. Limit: 10,
  25. Offset: 0,
  26. File: "file1",
  27. BuildID: "buildid1",
  28. HasFunctions: true,
  29. HasFilenames: true,
  30. HasLineNumbers: true,
  31. HasInlineFrames: true,
  32. },
  33. {
  34. ID: 2,
  35. Start: 10,
  36. Limit: 30,
  37. Offset: 9,
  38. File: "file1",
  39. BuildID: "buildid2",
  40. HasFunctions: true,
  41. HasFilenames: true,
  42. HasLineNumbers: true,
  43. HasInlineFrames: true,
  44. },
  45. }
  46. var testF = []*Function{
  47. {ID: 1, Name: "func1", SystemName: "func1", Filename: "file1"},
  48. {ID: 2, Name: "func2", SystemName: "func2", Filename: "file1"},
  49. {ID: 3, Name: "func3", SystemName: "func3", Filename: "file2"},
  50. {ID: 4, Name: "func4", SystemName: "func4", Filename: "file3"},
  51. {ID: 5, Name: "func5", SystemName: "func5", Filename: "file4"},
  52. }
  53. var testL = []*Location{
  54. {
  55. ID: 1,
  56. Address: 1,
  57. Mapping: testM[0],
  58. Line: []Line{
  59. {
  60. Function: testF[0],
  61. Line: 2,
  62. },
  63. {
  64. Function: testF[1],
  65. Line: 2222222,
  66. },
  67. },
  68. },
  69. {
  70. ID: 2,
  71. Mapping: testM[1],
  72. Address: 11,
  73. Line: []Line{
  74. {
  75. Function: testF[2],
  76. Line: 2,
  77. },
  78. },
  79. },
  80. {
  81. ID: 3,
  82. Mapping: testM[1],
  83. Address: 12,
  84. },
  85. {
  86. ID: 4,
  87. Mapping: testM[1],
  88. Address: 12,
  89. Line: []Line{
  90. {
  91. Function: testF[4],
  92. Line: 6,
  93. },
  94. {
  95. Function: testF[4],
  96. Line: 6,
  97. },
  98. },
  99. IsFolded: true,
  100. },
  101. }
  102. var all = &Profile{
  103. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  104. Period: 10,
  105. DurationNanos: 10e9,
  106. SampleType: []*ValueType{
  107. {Type: "cpu", Unit: "cycles"},
  108. {Type: "object", Unit: "count"},
  109. },
  110. Sample: []*Sample{
  111. {
  112. Location: []*Location{testL[0], testL[1], testL[2], testL[1], testL[1]},
  113. Label: map[string][]string{
  114. "key1": {"value1"},
  115. "key2": {"value2"},
  116. },
  117. Value: []int64{10, 20},
  118. },
  119. {
  120. Location: []*Location{testL[1], testL[2], testL[0], testL[1]},
  121. Value: []int64{30, 40},
  122. Label: map[string][]string{
  123. "key1": {"value1"},
  124. "key2": {"value2"},
  125. },
  126. NumLabel: map[string][]int64{
  127. "key1": {1, 2},
  128. "key2": {3, 4},
  129. "bytes": {3, 4},
  130. "requests": {1, 1, 3, 4, 5},
  131. "alignment": {3, 4},
  132. },
  133. NumUnit: map[string][]string{
  134. "requests": {"", "", "seconds", "", "s"},
  135. "alignment": {"kilobytes", "kilobytes"},
  136. },
  137. },
  138. },
  139. Function: testF,
  140. Mapping: testM,
  141. Location: testL,
  142. Comments: []string{"Comment 1", "Comment 2"},
  143. }
  144. func TestMarshalUnmarshal(t *testing.T) {
  145. // Write the profile, parse it, and ensure they're equal.
  146. var buf bytes.Buffer
  147. all.Write(&buf)
  148. all2, err := Parse(&buf)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. js1 := proftest.EncodeJSON(&all)
  153. js2 := proftest.EncodeJSON(&all2)
  154. if string(js1) != string(js2) {
  155. t.Errorf("profiles differ")
  156. d, err := proftest.Diff(js1, js2)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. t.Error("\n" + string(d))
  161. }
  162. }