github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/causality/tests/workload.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package tests
    15  
    16  import (
    17  	"sort"
    18  
    19  	"golang.org/x/exp/rand"
    20  )
    21  
    22  type workloadGenerator interface {
    23  	Next() []uint64
    24  }
    25  
    26  type uniformGenerator struct {
    27  	workingSetSize int64
    28  	batchSize      int
    29  	numSlots       uint64
    30  }
    31  
    32  func newUniformGenerator(workingSetSize int64, batchSize int, numSlots uint64) *uniformGenerator {
    33  	return &uniformGenerator{
    34  		workingSetSize: workingSetSize,
    35  		batchSize:      batchSize,
    36  		numSlots:       numSlots,
    37  	}
    38  }
    39  
    40  func (g *uniformGenerator) Next() []uint64 {
    41  	set := make(map[uint64]struct{}, g.batchSize)
    42  	for i := 0; i < g.batchSize; i++ {
    43  		key := uint64(rand.Int63n(g.workingSetSize))
    44  		set[key] = struct{}{}
    45  	}
    46  
    47  	ret := make([]uint64, 0, g.batchSize)
    48  	for key := range set {
    49  		ret = append(ret, key)
    50  	}
    51  
    52  	sort.Slice(ret, func(i, j int) bool { return ret[i]%g.numSlots < ret[j]%g.numSlots })
    53  	return ret
    54  }