Exemplo n.º 1
0
func benchmarkUsing(b *testing.B, pool FramePool) {
	const numGoroutines = 1000
	const maxHoldFrames = 1000

	var gotFrames atomic.Uint64

	var wg sync.WaitGroup
	for i := 0; i < numGoroutines; i++ {
		wg.Add(1)
		go func() {

			for {
				if gotFrames.Load() > uint64(b.N) {
					break
				}

				framesToHold := rand.Intn(maxHoldFrames)
				gotFrames.Add(uint64(framesToHold))

				frames := make([]*Frame, framesToHold)
				for i := 0; i < framesToHold; i++ {
					frames[i] = pool.Get()
				}

				for i := 0; i < framesToHold; i++ {
					pool.Release(frames[i])
				}
			}

			wg.Done()
		}()
	}

	wg.Wait()
}
Exemplo n.º 2
0
func createScoreStrategy(initial, delta int64) (calc ScoreCalculator, retCount *atomic.Uint64) {
	var (
		count atomic.Uint64
		score atomic.Uint64
	)

	return ScoreCalculatorFunc(func(p *Peer) uint64 {
		count.Add(1)
		return score.Add(uint64(delta))
	}), &count
}