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

perf(client): prealloc pkg/varlog.(*transmitter).transmitQueue #721

Merged
merged 1 commit into from
Mar 5, 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
9 changes: 9 additions & 0 deletions bench_tq.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -e

go test -c ./pkg/varlog
./varlog.test -test.v -test.run - -test.bench BenchmarkTransmitQueue -test.count 20 -test.benchmem -test.timeout 10h | tee out
grep -v PreAlloc out | sed 's,/NoAlloc,,g' >out.NoAlloc
grep -v NoAlloc out | sed 's,/PreAlloc,,g' >out.PreAlloc
benchstat out.NoAlloc out.PreAlloc
60 changes: 60 additions & 0 deletions pkg/varlog/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package varlog

import (
"fmt"
"math/rand"
"testing"

"github.com/kakao/varlog/internal/storagenode/client"
"github.com/kakao/varlog/pkg/types"
"github.com/kakao/varlog/proto/varlogpb"
)

func BenchmarkTransmitQueue(b *testing.B) {
sizes := []int{1 << 7, 1 << 10, 1 << 13}
tcs := []struct {
generate func(int) *transmitQueue
name string
}{
{
name: "NoAlloc",
generate: func(int) *transmitQueue {
return &transmitQueue{
pq: &PriorityQueue{},
}
},
},
{
name: "PreAlloc",
generate: func(size int) *transmitQueue {
return &transmitQueue{
pq: newPriorityQueue(size / 2),
}
},
},
}

for _, tc := range tcs {
for _, size := range sizes {
name := fmt.Sprintf("%s/%d", tc.name, size)
b.Run(name, func(b *testing.B) {
b.ResetTimer()
for range b.N {
tq := tc.generate(size)
for range size {
tr := transmitResult{
result: client.SubscribeResult{
LogEntry: varlogpb.LogEntry{
LogEntryMeta: varlogpb.LogEntryMeta{
GLSN: types.GLSN(rand.Int31()),
},
},
},
}
tq.Push(tr)
}
}
})
}
}
}
12 changes: 11 additions & 1 deletion pkg/varlog/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
sleq := newSubscribedLogEntiresQueue(begin, end, closer, v.logger)

tlogger := v.logger.Named("transmitter")
// The maximum length of transmitQ is end - begin in the worst case.
// Therefore, we can approximate half of it.
// TODO: Use a better approximation.
approxTransmitQSize := int((end - begin) / 2)

Check warning on line 54 in pkg/varlog/subscribe.go

View check run for this annotation

Codecov / codecov/patch

pkg/varlog/subscribe.go#L54

Added line #L54 was not covered by tests
tsm := &transmitter{
topicID: topicID,
subscribers: make(map[types.LogStreamID]*subscriber),
Expand All @@ -57,7 +61,7 @@
sleq: sleq,
wanted: begin,
end: end,
transmitQ: &transmitQueue{pq: &PriorityQueue{}},
transmitQ: &transmitQueue{pq: newPriorityQueue(approxTransmitQSize)},

Check warning on line 64 in pkg/varlog/subscribe.go

View check run for this annotation

Codecov / codecov/patch

pkg/varlog/subscribe.go#L64

Added line #L64 was not covered by tests
transmitCV: transmitCV,
timeout: subscribeOpts.timeout,
runner: runner.New("transmitter", tlogger),
Expand Down Expand Up @@ -89,6 +93,12 @@

type PriorityQueue []PriorityQueueItem

func newPriorityQueue(size int) *PriorityQueue {
items := make([]PriorityQueueItem, 0, size)
pq := PriorityQueue(items)
return &pq

Check warning on line 99 in pkg/varlog/subscribe.go

View check run for this annotation

Codecov / codecov/patch

pkg/varlog/subscribe.go#L96-L99

Added lines #L96 - L99 were not covered by tests
}

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
Expand Down