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.
 
 
 

80 lines
2.1 KiB

  1. // Copyright 2018 Google LLC
  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 profiler
  15. import (
  16. "bytes"
  17. "io"
  18. "testing"
  19. "cloud.google.com/go/profiler/testdata"
  20. "github.com/google/pprof/profile"
  21. )
  22. func TestGoHeapProfile(t *testing.T) {
  23. oldStartCPUProfile, oldStopCPUProfile, oldWriteHeapProfile, oldSleep := startCPUProfile, stopCPUProfile, writeHeapProfile, sleep
  24. defer func() {
  25. startCPUProfile, stopCPUProfile, writeHeapProfile, sleep = oldStartCPUProfile, oldStopCPUProfile, oldWriteHeapProfile, oldSleep
  26. }()
  27. tests := []struct {
  28. name string
  29. profile *profile.Profile
  30. wantErr bool
  31. }{
  32. {
  33. name: "valid heap profile",
  34. profile: testdata.HeapProfileCollected1,
  35. },
  36. {
  37. name: "profile with too few sample types",
  38. profile: testdata.AllocProfileUploaded,
  39. wantErr: true,
  40. },
  41. {
  42. name: "profile with incorrect sample types",
  43. profile: &profile.Profile{
  44. DurationNanos: 10e9,
  45. SampleType: []*profile.ValueType{
  46. {Type: "objects", Unit: "count"},
  47. {Type: "alloc_space", Unit: "bytes"},
  48. {Type: "inuse_objects", Unit: "count"},
  49. {Type: "inuse_space", Unit: "bytes"},
  50. },
  51. },
  52. wantErr: true,
  53. },
  54. }
  55. for _, tc := range tests {
  56. var profileBytes bytes.Buffer
  57. tc.profile.Write(&profileBytes)
  58. writeHeapProfile = func(w io.Writer) error {
  59. w.Write(profileBytes.Bytes())
  60. return nil
  61. }
  62. _, err := goHeapProfile()
  63. if tc.wantErr {
  64. if err == nil {
  65. t.Errorf("%s: goHeapProfile() got no error, want error", tc.name)
  66. }
  67. continue
  68. }
  69. if err != nil {
  70. t.Errorf("%s: goHeapProfile() got %q, want no error", tc.name, err)
  71. }
  72. }
  73. }