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

     1  package load
     2  
     3  import "math/rand"
     4  
     5  type TagsGenerator struct {
     6  	seed    int
     7  	appName string
     8  
     9  	tags []testTag
    10  	ixs  []int
    11  }
    12  
    13  type testTag struct {
    14  	name   string
    15  	values []string
    16  }
    17  
    18  func NewTagGenerator(seed int, appName string) *TagsGenerator {
    19  	return &TagsGenerator{seed: seed, appName: appName}
    20  }
    21  
    22  func (g *TagsGenerator) Next() map[string]string {
    23  	k := map[string]string{"__name__": g.appName}
    24  	for i := 0; i < len(g.tags); i++ {
    25  		t := g.tags[i]
    26  		k[t.name] = t.values[g.ixs[i]%len(t.values)]
    27  		g.ixs[i]++
    28  	}
    29  	return k
    30  }
    31  
    32  func (g *TagsGenerator) Add(name string, card, min, max int) *TagsGenerator {
    33  	g.seed++
    34  	r := newRand(g.seed)
    35  	g.ixs = append(g.ixs, 0)
    36  	g.tags = append(g.tags, testTag{
    37  		name:   name,
    38  		values: g.values(r, card, min, max),
    39  	})
    40  	return g
    41  }
    42  
    43  func (*TagsGenerator) values(r *rand.Rand, count, min, max int) []string {
    44  	values := make([]string, count)
    45  	for i := 0; i < count; i++ {
    46  		values[i] = randString(r, min, max)
    47  	}
    48  	return values
    49  }