github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/testing/load/app.go (about)

     1  package load
     2  
     3  import (
     4  	"math/big"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/pyroscope-io/pyroscope/pkg/storage/metadata"
     9  	"github.com/pyroscope-io/pyroscope/pkg/storage/segment"
    10  	"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
    11  )
    12  
    13  type App struct {
    14  	Name            string
    15  	SpyName         string
    16  	SampleRate      uint32
    17  	Units           metadata.Units
    18  	AggregationType metadata.AggregationType
    19  
    20  	tags  *TagsGenerator
    21  	trees *TreeGenerator
    22  
    23  	m          sync.Mutex
    24  	mergedTree *tree.Tree
    25  }
    26  
    27  type AppConfig struct {
    28  	SpyName         string                   `yaml:"spyName"`
    29  	SampleRate      uint32                   `yaml:"sampleRate"`
    30  	Units           metadata.Units           `yaml:"units"`
    31  	AggregationType metadata.AggregationType `yaml:"aggregationType"`
    32  
    33  	Tags       []Tag `yaml:"tags"`
    34  	Trees      int   `yaml:"trees"`
    35  	TreeConfig `yaml:"treeConfig"`
    36  }
    37  
    38  type Tag struct {
    39  	Name        string `yaml:"name"`
    40  	Cardinality int    `yaml:"cardinality"`
    41  	MinLen      int    `yaml:"minLen"`
    42  	MaxLen      int    `yaml:"maxLen"`
    43  }
    44  
    45  func NewApp(seed int, name string, c AppConfig) *App {
    46  	a := App{
    47  		Name:            name,
    48  		SpyName:         c.SpyName,
    49  		SampleRate:      c.SampleRate,
    50  		Units:           c.Units,
    51  		AggregationType: c.AggregationType,
    52  		mergedTree:      tree.New(),
    53  	}
    54  	a.trees = NewTreeGenerator(seed, c.Trees, c.TreeConfig)
    55  	a.tags = NewTagGenerator(seed, name)
    56  	for _, t := range c.Tags {
    57  		a.tags.Add(t.Name, t.Cardinality, t.MinLen, t.MaxLen)
    58  	}
    59  	return &a
    60  }
    61  
    62  type Input struct {
    63  	StartTime       time.Time
    64  	EndTime         time.Time
    65  	Key             *segment.Key
    66  	Val             *tree.Tree
    67  	SpyName         string
    68  	SampleRate      uint32
    69  	Units           metadata.Units
    70  	AggregationType metadata.AggregationType
    71  }
    72  
    73  func (a *App) CreateInput(from, to time.Time) Input {
    74  	t := a.trees.Next()
    75  
    76  	a.m.Lock()
    77  	a.mergedTree.Merge(t.Clone(big.NewRat(1, 1)))
    78  	a.m.Unlock()
    79  
    80  	return Input{
    81  		StartTime:       from,
    82  		EndTime:         to,
    83  		Key:             segment.NewKey(a.tags.Next()),
    84  		Val:             t,
    85  		SpyName:         a.SpyName,
    86  		SampleRate:      a.SampleRate,
    87  		Units:           a.Units,
    88  		AggregationType: a.AggregationType,
    89  	}
    90  }
    91  
    92  func (a *App) MergedTree() *tree.Tree { return a.mergedTree }