github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/profile/pyroscope/jfr/jfr_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 jfr
    16  
    17  import (
    18  	"bytes"
    19  	"compress/gzip"
    20  	"context"
    21  	"io"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	"google.golang.org/protobuf/proto"
    28  
    29  	"github.com/alibaba/ilogtail/pkg/helper/profile"
    30  )
    31  
    32  func TestRawProfile_Parse(t *testing.T) {
    33  
    34  }
    35  
    36  func TestParse(t *testing.T) {
    37  	jfr, err := readGzipFile("./testdata/example.jfr.gz")
    38  	if err != nil {
    39  		t.Fatalf("Unable to read JFR file: %s", err)
    40  	}
    41  
    42  	rp := RawProfile{
    43  		FormDataContentType: "",
    44  		RawData:             jfr,
    45  	}
    46  
    47  	logs, err := rp.Parse(context.Background(), &profile.Meta{
    48  		Tags:            map[string]string{"_app_name_": "12"},
    49  		SpyName:         "javaspy",
    50  		StartTime:       time.Now(),
    51  		EndTime:         time.Now(),
    52  		SampleRate:      99,
    53  		Units:           profile.SamplesUnits,
    54  		AggregationType: profile.SumAggType,
    55  	}, nil)
    56  	if err != nil {
    57  		t.Fatalf("Failed to parse JFR: %s", err)
    58  		return
    59  	}
    60  	require.Equal(t, len(logs), 323)
    61  }
    62  
    63  func TestParseJFR(t *testing.T) {
    64  	jfr, err := readRawFile("./testdata/jfr.raw")
    65  	if err != nil {
    66  		t.Fatalf("Unable to read JFR file: %s", err)
    67  	}
    68  
    69  	jfrLables, err := readRawFile("./testdata/jfr_labels.raw")
    70  	if err != nil {
    71  		t.Fatalf("Unable to read JFR label file: %s", err)
    72  	}
    73  	var labels LabelsSnapshot
    74  
    75  	if err = proto.Unmarshal(jfrLables, &labels); err != nil {
    76  		t.Fatalf("Unable to read JFR label file: %s", err)
    77  	}
    78  
    79  	var reader io.Reader = bytes.NewReader(jfr)
    80  
    81  	r := new(RawProfile)
    82  	meta := &profile.Meta{
    83  		Tags:            map[string]string{"_app_name_": "12"},
    84  		SpyName:         "javaspy",
    85  		StartTime:       time.Now(),
    86  		EndTime:         time.Now(),
    87  		SampleRate:      99,
    88  		Units:           profile.SamplesUnits,
    89  		AggregationType: profile.SumAggType,
    90  	}
    91  	cb := r.extractProfileV1(meta, nil)
    92  	r.ParseJFR(context.Background(), meta, reader, &labels, cb)
    93  	logs := r.logs
    94  	require.Equal(t, len(logs), 3)
    95  }
    96  
    97  func readGzipFile(fname string) ([]byte, error) {
    98  	f, err := os.Open(fname)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	defer f.Close()
   103  	r, err := gzip.NewReader(f)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	defer r.Close()
   108  	return io.ReadAll(r)
   109  }
   110  
   111  func readRawFile(fname string) ([]byte, error) {
   112  	f, err := os.Open(fname)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	defer f.Close()
   117  	return io.ReadAll(f)
   118  }