bufpool_test.go 826 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package bufpool
  2. import (
  3. "math/rand"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/imgproxy/imgproxy/v3/config"
  8. )
  9. var (
  10. testData [][]byte
  11. testDataOnce sync.Once
  12. testMu sync.Mutex
  13. )
  14. func initTestData() {
  15. testData = make([][]byte, 1000)
  16. for i := 6; i < 1000; i++ {
  17. testData[i] = make([]byte, i*1271)
  18. }
  19. rand.Seed(time.Now().UnixNano())
  20. rand.Shuffle(len(testData), func(i, j int) { testData[i], testData[j] = testData[j], testData[i] })
  21. }
  22. func BenchmarkBufpool(b *testing.B) {
  23. testMu.Lock()
  24. defer testMu.Unlock()
  25. config.Reset()
  26. testDataOnce.Do(initTestData)
  27. pool := New("test", 16, 0)
  28. b.ResetTimer()
  29. b.SetParallelism(16)
  30. b.RunParallel(func(pb *testing.PB) {
  31. for pb.Next() {
  32. for _, bb := range testData {
  33. buf := pool.Get(len(bb), false)
  34. buf.Write(bb)
  35. pool.Put(buf)
  36. }
  37. }
  38. })
  39. }