github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/profile/pyroscope/pprof/pprof_test.go (about) 1 // Copyright 2023 iLogtail Authors 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 15 package pprof 16 17 import ( 18 "context" 19 "os" 20 "strconv" 21 "testing" 22 "time" 23 24 "github.com/pyroscope-io/pyroscope/pkg/convert/pprof" 25 "github.com/pyroscope-io/pyroscope/pkg/storage/tree" 26 "github.com/stretchr/testify/require" 27 28 "github.com/alibaba/ilogtail/pkg/helper/profile" 29 "github.com/alibaba/ilogtail/pkg/protocol" 30 ) 31 32 func readPprofFixture(path string) (*tree.Profile, error) { 33 f, err := os.Open(path) 34 if err != nil { 35 return nil, err 36 } 37 defer func() { 38 _ = f.Close() 39 }() 40 41 var p tree.Profile 42 if err = pprof.Decode(f, &p); err != nil { 43 return nil, err 44 } 45 return &p, nil 46 } 47 48 const ( 49 name = "runtime.kevent /opt/homebrew/Cellar/go/1.16.1/libexec/src/runtime/sys_darwin.go" 50 stack = "runtime.netpoll /opt/homebrew/Cellar/go/1.16.1/libexec/src/runtime/netpoll_kqueue.go\nruntime.findrunnable /opt/homebrew/Cellar/go/1.16.1/libexec/src/runtime/proc.go\nruntime.schedule /opt/homebrew/Cellar/go/1.16.1/libexec/src/runtime/proc.go\nruntime.park_m /opt/homebrew/Cellar/go/1.16.1/libexec/src/runtime/proc.go\nruntime.mcall /opt/homebrew/Cellar/go/1.16.1/libexec/src/runtime/asm_arm64.s" 51 stackID = "40fb694aa9506d0b" 52 startTime = 1619321948265140000 53 endTime = 1619321949365317167 54 lanuage = "go" 55 profileType = "profile_cpu" 56 dataType = "CallStack" 57 val = 250000000 58 valType = "cpu" 59 unitType = "nanoseconds" 60 aggType = "sum" 61 ) 62 63 var ( 64 tags = map[string]string{ 65 "_app_name_": "12", 66 } 67 ) 68 69 func TestRawProfile_Parse(t *testing.T) { 70 te, err := readPprofFixture("testdata/cpu.pb.gz") 71 require.NoError(t, err) 72 p := Parser{ 73 stackFrameFormatter: Formatter{}, 74 sampleTypesFilter: filterKnownSamples(DefaultSampleTypeMapping), 75 sampleTypes: DefaultSampleTypeMapping, 76 } 77 r := new(RawProfile) 78 meta := &profile.Meta{ 79 Tags: tags, 80 SpyName: "go", 81 StartTime: time.Now(), 82 EndTime: time.Now(), 83 SampleRate: 99, 84 Units: profile.NanosecondsUnit, 85 AggregationType: profile.SumAggType, 86 } 87 cb := r.extractProfileV1(meta, map[string]string{"cluster": "cluster2"}) 88 r.parser = &p 89 err = r.extractLogs(context.Background(), te, meta, cb) 90 require.NoError(t, err) 91 logs := r.logs 92 require.Equal(t, len(logs), 6) 93 picks := PickLogs(logs, "stackID", stackID) 94 require.Equal(t, len(picks), 1) 95 log := picks[0] 96 require.Equal(t, ReadLogVal(log, "name"), name) 97 require.Equal(t, ReadLogVal(log, "stack"), stack) 98 require.Equal(t, ReadLogVal(log, "language"), lanuage) 99 require.Equal(t, ReadLogVal(log, "type"), profileType) 100 require.Equal(t, ReadLogVal(log, "units"), unitType) 101 require.Equal(t, ReadLogVal(log, "valueTypes"), valType) 102 require.Equal(t, ReadLogVal(log, "aggTypes"), aggType) 103 require.Equal(t, ReadLogVal(log, "dataType"), dataType) 104 require.Equal(t, ReadLogVal(log, "durationNs"), strconv.Itoa(endTime-startTime)) 105 require.Equal(t, ReadLogVal(log, "labels"), "{\"_app_name_\":\"12\",\"cluster\":\"cluster2\"}") 106 require.Equal(t, ReadLogVal(log, "val"), "250000000.00") 107 } 108 109 // ReadLogVal returns the log content value for the input key, and returns empty string when not found. 110 func ReadLogVal(log *protocol.Log, key string) string { 111 for _, content := range log.Contents { 112 if content.Key == key { 113 return content.Value 114 } 115 } 116 return "" 117 } 118 119 // PickLogs select some of original logs to new res logs by the specific pickKey and pickVal. 120 func PickLogs(logs []*protocol.Log, pickKey string, pickVal string) (res []*protocol.Log) { 121 for _, log := range logs { 122 if ReadLogVal(log, pickKey) == pickVal { 123 res = append(res, log) 124 } 125 } 126 return res 127 }