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

Conversation

redbaron
Copy link
Contributor

@redbaron redbaron commented Aug 7, 2024

This measures time for resource acquisition for cases when pool is empty and it had to wait or construct new resource.

This measures time for resource acquisition for cases
when pool is empty and it had to wait or construct new resource.
@redbaron
Copy link
Contributor Author

redbaron commented Aug 7, 2024

This is to implement what was discussed in jackc/pgx#1669

@@ -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

@jackc
Copy link
Owner

jackc commented Aug 7, 2024

It looks fine. But maybe add some tests just to sanity check it.

@redbaron redbaron force-pushed the empty-acquire-time-stats branch from 95fa753 to 865f1d3 Compare August 7, 2024 16:25
pool_test.go Outdated
@@ -692,6 +693,7 @@ func TestPoolStatSuccessfulAcquireCounters(t *testing.T) {
assert.Equal(t, int64(2), stat.AcquireCount())
assert.Equal(t, int64(1), stat.EmptyAcquireCount())
assert.True(t, stat.AcquireDuration() > lastAcquireDuration)
assert.True(t, stat.EmptyAcquireWaitTime() < stat.AcquireDuration())

Choose a reason for hiding this comment

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

assert.Greater should be preferred to compare things. The error reported if it fails will be clearer

You should also change the one on previous line.

Choose a reason for hiding this comment

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

I would like to recommend you using testifylint available via golangci-lint.

I can help if you create an issue for me, assign it me, and you are not in a hurry 🤣

pool_test.go Outdated
@@ -682,6 +682,7 @@ func TestPoolStatSuccessfulAcquireCounters(t *testing.T) {
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())

Choose a reason for hiding this comment

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

assert.Positive

pool_test.go Outdated
@@ -712,6 +714,7 @@ func TestPoolStatSuccessfulAcquireCounters(t *testing.T) {
assert.Equal(t, int64(4), stat.AcquireCount())
assert.Equal(t, int64(2), stat.EmptyAcquireCount())
assert.True(t, stat.AcquireDuration() > lastAcquireDuration)
assert.True(t, stat.EmptyAcquireWaitTime() < stat.AcquireDuration())

Choose a reason for hiding this comment

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

assert.Greater

@redbaron
Copy link
Contributor Author

@jackc , I have updated existing tests to add checks for new stats

@jackc jackc merged commit ee87cc3 into jackc:master Aug 14, 2024
2 checks passed
@redbaron redbaron deleted the empty-acquire-time-stats branch August 14, 2024 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants