Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pool): Add EmptyAcquireWaitTime stats #34

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type Pool[T any] struct {
acquireCount int64
acquireDuration time.Duration
emptyAcquireCount int64
emptyAcquireWaitTime time.Duration
canceledAcquireCount atomic.Int64

resetCount int
Expand Down Expand Up @@ -202,6 +203,7 @@ type Stat struct {
acquireCount int64
acquireDuration time.Duration
emptyAcquireCount int64
emptyAcquireWaitTime time.Duration
canceledAcquireCount int64
}

Expand Down Expand Up @@ -251,6 +253,13 @@ func (s *Stat) EmptyAcquireCount() int64 {
return s.emptyAcquireCount
}

// EmptyAcquireWaitTime returns the cumulative time waited for successful acquires
// from the pool for a resource to be released or constructed because the pool was
// empty.
func (s *Stat) EmptyAcquireWaitTime() time.Duration {
return s.emptyAcquireWaitTime
}

// CanceledAcquireCount returns the cumulative count of acquires from the pool
// that were canceled by a context.
func (s *Stat) CanceledAcquireCount() int64 {
Expand All @@ -266,6 +275,7 @@ func (p *Pool[T]) Stat() *Stat {
maxResources: p.maxSize,
acquireCount: p.acquireCount,
emptyAcquireCount: p.emptyAcquireCount,
emptyAcquireWaitTime: p.emptyAcquireWaitTime,
canceledAcquireCount: p.canceledAcquireCount.Load(),
acquireDuration: p.acquireDuration,
}
Expand Down Expand Up @@ -363,11 +373,13 @@ func (p *Pool[T]) acquire(ctx context.Context) (*Resource[T], error) {

// If a resource is available in the pool.
if res := p.tryAcquireIdleResource(); res != nil {
waitTime := time.Duration(nanotime() - startNano)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related to your PR as you are reusing something that existed, but I'm curious

Any reason to use startNano := nanotime, and not a start := time.Now() and here a time.Now().Since(start)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK, as you said I am reusing previous approach to get time. nanotime is provided via linking so I assume there is some platform specific trickery to make it cheaper to call maybe?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, directly calling nanotime is a micro optimization.

On my machine an uncontested Acquire and Release cycle takes 103ns with time.Now but only 56ns with nanotime directly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. So maybe a comment about it could be interesting, because the pattern is not obvious.

Or maybe I missed it

if waitedForLock {
p.emptyAcquireCount += 1
p.emptyAcquireWaitTime += waitTime
}
p.acquireCount += 1
p.acquireDuration += time.Duration(nanotime() - startNano)
p.acquireDuration += waitTime
p.mux.Unlock()
return res, nil
}
Expand All @@ -391,7 +403,9 @@ func (p *Pool[T]) acquire(ctx context.Context) (*Resource[T], error) {

p.emptyAcquireCount += 1
p.acquireCount += 1
p.acquireDuration += time.Duration(nanotime() - startNano)
waitTime := time.Duration(nanotime() - startNano)
p.acquireDuration += waitTime
p.emptyAcquireWaitTime += waitTime

return res, nil
}
Expand Down
9 changes: 6 additions & 3 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,8 @@ func TestPoolStatSuccessfulAcquireCounters(t *testing.T) {
stat := pool.Stat()
assert.Equal(t, int64(1), stat.AcquireCount())
assert.Equal(t, int64(1), stat.EmptyAcquireCount())
assert.True(t, stat.AcquireDuration() > 0, "expected stat.AcquireDuration() > 0 but %v", stat.AcquireDuration())
assert.Positive(t, stat.AcquireDuration(), "expected stat.AcquireDuration() > 0 but %v", stat.AcquireDuration())
assert.Equal(t, stat.EmptyAcquireWaitTime(), stat.AcquireDuration())
lastAcquireDuration := stat.AcquireDuration()

res, err = pool.Acquire(context.Background())
Expand All @@ -691,7 +692,8 @@ func TestPoolStatSuccessfulAcquireCounters(t *testing.T) {
stat = pool.Stat()
assert.Equal(t, int64(2), stat.AcquireCount())
assert.Equal(t, int64(1), stat.EmptyAcquireCount())
assert.True(t, stat.AcquireDuration() > lastAcquireDuration)
assert.Greater(t, stat.AcquireDuration(), lastAcquireDuration)
assert.Less(t, stat.EmptyAcquireWaitTime(), stat.AcquireDuration())
lastAcquireDuration = stat.AcquireDuration()

wg := &sync.WaitGroup{}
Expand All @@ -711,7 +713,8 @@ func TestPoolStatSuccessfulAcquireCounters(t *testing.T) {
stat = pool.Stat()
assert.Equal(t, int64(4), stat.AcquireCount())
assert.Equal(t, int64(2), stat.EmptyAcquireCount())
assert.True(t, stat.AcquireDuration() > lastAcquireDuration)
assert.Greater(t, stat.AcquireDuration(), lastAcquireDuration)
assert.Less(t, stat.EmptyAcquireWaitTime(), stat.AcquireDuration())
lastAcquireDuration = stat.AcquireDuration()
}

Expand Down
Loading