...

Source file src/github.com/redis/go-redis/v9/internal/pool/bench_test.go

Documentation: github.com/redis/go-redis/v9/internal/pool

     1  package pool_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/redis/go-redis/v9/internal/pool"
    10  )
    11  
    12  type poolGetPutBenchmark struct {
    13  	poolSize int
    14  }
    15  
    16  func (bm poolGetPutBenchmark) String() string {
    17  	return fmt.Sprintf("pool=%d", bm.poolSize)
    18  }
    19  
    20  func BenchmarkPoolGetPut(b *testing.B) {
    21  	ctx := context.Background()
    22  	benchmarks := []poolGetPutBenchmark{
    23  		{1},
    24  		{2},
    25  		{8},
    26  		{32},
    27  		{64},
    28  		{128},
    29  	}
    30  	for _, bm := range benchmarks {
    31  		b.Run(bm.String(), func(b *testing.B) {
    32  			connPool := pool.NewConnPool(&pool.Options{
    33  				Dialer:          dummyDialer,
    34  				PoolSize:        bm.poolSize,
    35  				PoolTimeout:     time.Second,
    36  				DialTimeout:     1 * time.Second,
    37  				ConnMaxIdleTime: time.Hour,
    38  			})
    39  
    40  			b.ResetTimer()
    41  
    42  			b.RunParallel(func(pb *testing.PB) {
    43  				for pb.Next() {
    44  					cn, err := connPool.Get(ctx)
    45  					if err != nil {
    46  						b.Fatal(err)
    47  					}
    48  					connPool.Put(ctx, cn)
    49  				}
    50  			})
    51  		})
    52  	}
    53  }
    54  
    55  type poolGetRemoveBenchmark struct {
    56  	poolSize int
    57  }
    58  
    59  func (bm poolGetRemoveBenchmark) String() string {
    60  	return fmt.Sprintf("pool=%d", bm.poolSize)
    61  }
    62  
    63  func BenchmarkPoolGetRemove(b *testing.B) {
    64  	ctx := context.Background()
    65  	benchmarks := []poolGetRemoveBenchmark{
    66  		{1},
    67  		{2},
    68  		{8},
    69  		{32},
    70  		{64},
    71  		{128},
    72  	}
    73  
    74  	for _, bm := range benchmarks {
    75  		b.Run(bm.String(), func(b *testing.B) {
    76  			connPool := pool.NewConnPool(&pool.Options{
    77  				Dialer:          dummyDialer,
    78  				PoolSize:        bm.poolSize,
    79  				PoolTimeout:     time.Second,
    80  				DialTimeout:     1 * time.Second,
    81  				ConnMaxIdleTime: time.Hour,
    82  			})
    83  
    84  			b.ResetTimer()
    85  
    86  			b.RunParallel(func(pb *testing.PB) {
    87  				for pb.Next() {
    88  					cn, err := connPool.Get(ctx)
    89  					if err != nil {
    90  						b.Fatal(err)
    91  					}
    92  					connPool.Remove(ctx, cn, nil)
    93  				}
    94  			})
    95  		})
    96  	}
    97  }
    98  

View as plain text