dropbox / godropbox

Common libraries for writing Go services/applications.
BSD 3-Clause "New" or "Revised" License
4.17k stars 427 forks source link

Default value of MaxIdleConnections in net2.ConnectionPool is extremely slow #261

Open gugu opened 1 year ago

gugu commented 1 year ago

Benchmark results:

Fake dealer:
BenchmarkGetConnection-16                1492227           803.2 ns/op
BenchmarkGetConnectionMaxIdle-16         1888982           631.4 ns/op
Real dealer:
BenchmarkGetConnection-16                   9052        113356 ns/op
BenchmarkGetConnectionMaxIdle-16         1901071           626.9 ns/op

Code example:

func BenchmarkGetConnection(b *testing.B) {
        // dialer := fakeDialer{}
        // mockClock := time2.MockClock{}

        options := ConnectionOptions{
                // MaxIdleConnections: 1,
                // Dial:               dialer.FakeDial,
                // NowFunc:            mockClock.Now,
        }

        pool := NewSimpleConnectionPool(options)
        pool.Register("tcp", "localhost:11211")
        b.ResetTimer();
        for i := 0; i < b.N; i++ {
                c, err := pool.Get("tcp", "localhost:6379")
                if err != nil {
                        b.Fatal(err)
                }
                c.ReleaseConnection()
        }
}

func BenchmarkGetConnectionMaxIdle(b *testing.B) {
        // dialer := fakeDialer{}
        // mockClock := time2.MockClock{}

        options := ConnectionOptions{
                MaxIdleConnections: 1,
                // Dial:               dialer.FakeDial,
                // NowFunc:            mockClock.Now,
        }

        pool := NewSimpleConnectionPool(options)
        pool.Register("tcp", "localhost:11211")
        b.ResetTimer();
        for i := 0; i < b.N; i++ {
                c, err := pool.Get("tcp", "localhost:6379")
                if err != nil {
                        b.Fatal(err)
                }
                c.ReleaseConnection()
        }
}