Ei kuvausta

proto_test.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. }
  51. var testL = []*Location{
  52. {
  53. ID: 1,
  54. Address: 1,
  55. Mapping: testM[0],
  56. Line: []Line{
  57. {
  58. Function: testF[0],
  59. Line: 2,
  60. },
  61. {
  62. Function: testF[1],
  63. Line: 2222222,
  64. },
  65. },
  66. },
  67. {
  68. ID: 2,
  69. Mapping: testM[1],
  70. Address: 11,
  71. Line: []Line{
  72. {
  73. Function: testF[2],
  74. Line: 2,
  75. },
  76. },
  77. },
  78. {
  79. ID: 3,
  80. Mapping: testM[1],
  81. Address: 12,
  82. },
  83. }
  84. var all = &Profile{
  85. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  86. Period: 10,
  87. DurationNanos: 10e9,
  88. SampleType: []*ValueType{
  89. {Type: "cpu", Unit: "cycles"},
  90. {Type: "object", Unit: "count"},
  91. },
  92. Sample: []*Sample{
  93. {
  94. Location: []*Location{testL[0], testL[1], testL[2], testL[1], testL[1]},
  95. Label: map[string][]string{
  96. "key1": {"value1"},
  97. "key2": {"value2"},
  98. },
  99. Value: []int64{10, 20},
  100. },
  101. {
  102. Location: []*Location{testL[1], testL[2], testL[0], testL[1]},
  103. Value: []int64{30, 40},
  104. Label: map[string][]string{
  105. "key1": {"value1"},
  106. "key2": {"value2"},
  107. },
  108. NumLabel: map[string][]int64{
  109. "key1": {1, 2},
  110. "key2": {3, 4},
  111. "bytes": {3, 4},
  112. "requests": {1, 1, 3, 4, 5},
  113. "alignment": {3, 4},
  114. },
  115. NumUnit: map[string][]string{
  116. "requests": {"", "", "seconds", "", "s"},
  117. "alignment": {"kilobytes", "kilobytes"},
  118. },
  119. },
  120. },
  121. Function: testF,
  122. Mapping: testM,
  123. Location: testL,
  124. Comments: []string{"Comment 1", "Comment 2"},
  125. }
  126. func TestMarshalUnmarshal(t *testing.T) {
  127. // Write the profile, parse it, and ensure they're equal.
  128. buf := bytes.NewBuffer(nil)
  129. all.Write(buf)
  130. all2, err := Parse(buf)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. js1 := proftest.EncodeJSON(&all)
  135. js2 := proftest.EncodeJSON(&all2)
  136. if string(js1) != string(js2) {
  137. t.Errorf("profiles differ")
  138. d, err := proftest.Diff(js1, js2)
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. t.Error("\n" + string(d))
  143. }
  144. }