bufpool_test.go 784 B

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