설명 없음

legacy_profile_test.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "fmt"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "testing"
  22. )
  23. func TestLegacyProfileType(t *testing.T) {
  24. type testcase struct {
  25. sampleTypes []string
  26. typeSet [][]string
  27. want bool
  28. setName string
  29. }
  30. heap := heapzSampleTypes
  31. cont := contentionzSampleTypes
  32. testcases := []testcase{
  33. // True cases
  34. {[]string{"allocations", "size"}, heap, true, "heapzSampleTypes"},
  35. {[]string{"objects", "space"}, heap, true, "heapzSampleTypes"},
  36. {[]string{"inuse_objects", "inuse_space"}, heap, true, "heapzSampleTypes"},
  37. {[]string{"alloc_objects", "alloc_space"}, heap, true, "heapzSampleTypes"},
  38. {[]string{"contentions", "delay"}, cont, true, "contentionzSampleTypes"},
  39. // False cases
  40. {[]string{"objects"}, heap, false, "heapzSampleTypes"},
  41. {[]string{"objects", "unknown"}, heap, false, "heapzSampleTypes"},
  42. {[]string{"contentions", "delay"}, heap, false, "heapzSampleTypes"},
  43. {[]string{"samples", "cpu"}, heap, false, "heapzSampleTypes"},
  44. {[]string{"samples", "cpu"}, cont, false, "contentionzSampleTypes"},
  45. }
  46. for _, tc := range testcases {
  47. p := profileOfType(tc.sampleTypes)
  48. if got := isProfileType(p, tc.typeSet); got != tc.want {
  49. t.Error("isProfileType({"+strings.Join(tc.sampleTypes, ",")+"},", tc.setName, "), got", got, "want", tc.want)
  50. }
  51. }
  52. }
  53. func TestCpuParse(t *testing.T) {
  54. // profileString is a legacy encoded profile, represnted by words separated by ":"
  55. // Each sample has the form value : N : stack1..stackN
  56. // EOF is represented as "0:1:0"
  57. profileString := "1:3:100:999:100:" // sample with bogus 999 and duplicate leaf
  58. profileString += "1:5:200:999:200:501:502:" // sample with bogus 999 and duplicate leaf
  59. profileString += "1:12:300:999:300:601:602:603:604:605:606:607:608:609:" // sample with bogus 999 and duplicate leaf
  60. profileString += "0:1:0000" // EOF -- must use 4 bytes for the final zero
  61. p, err := cpuProfile([]byte(profileString), 1, parseString)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if err := checkTestSample(p, []uint64{100}); err != nil {
  66. t.Error(err)
  67. }
  68. if err := checkTestSample(p, []uint64{200, 500, 501}); err != nil {
  69. t.Error(err)
  70. }
  71. if err := checkTestSample(p, []uint64{300, 600, 601, 602, 603, 604, 605, 606, 607, 608}); err != nil {
  72. t.Error(err)
  73. }
  74. }
  75. func parseString(b []byte) (uint64, []byte) {
  76. slices := bytes.SplitN(b, []byte(":"), 2)
  77. var value, remainder []byte
  78. if len(slices) > 0 {
  79. value = slices[0]
  80. }
  81. if len(slices) > 1 {
  82. remainder = slices[1]
  83. }
  84. v, _ := strconv.ParseUint(string(value), 10, 64)
  85. return v, remainder
  86. }
  87. func checkTestSample(p *Profile, want []uint64) error {
  88. for _, s := range p.Sample {
  89. got := []uint64{}
  90. for _, l := range s.Location {
  91. got = append(got, l.Address)
  92. }
  93. if reflect.DeepEqual(got, want) {
  94. return nil
  95. }
  96. }
  97. return fmt.Errorf("Could not find sample : %v", want)
  98. }
  99. // profileOfType creates an empty profile with only sample types set,
  100. // for testing purposes only.
  101. func profileOfType(sampleTypes []string) *Profile {
  102. p := new(Profile)
  103. p.SampleType = make([]*ValueType, len(sampleTypes))
  104. for i, t := range sampleTypes {
  105. p.SampleType[i] = new(ValueType)
  106. p.SampleType[i].Type = t
  107. }
  108. return p
  109. }