-
Notifications
You must be signed in to change notification settings - Fork 0
Add ring benchmarks #13
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
Open
freshdresch
wants to merge
2
commits into
develop
Choose a base branch
from
add-ring-benchmarks
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,302 @@ | ||
| package ring_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
| ) | ||
|
|
||
| type ChanWrapper[T any] struct { | ||
| ch chan T | ||
| } | ||
|
|
||
| func NewChanWrapper[T any](capacity int) *ChanWrapper[T] { | ||
| return &ChanWrapper[T]{ch: make(chan T, capacity)} | ||
| } | ||
|
|
||
| func (c *ChanWrapper[T]) EnqueueBurst(batch []T) { | ||
| for _, v := range batch { | ||
| c.ch <- v | ||
| } | ||
| } | ||
|
|
||
| func (c *ChanWrapper[T]) DequeueBurst(n int) []T { | ||
| res := make([]T, 0, n) | ||
| for i := 0; i < n; i++ { | ||
| select { | ||
| case v := <-c.ch: | ||
| res = append(res, v) | ||
| default: | ||
| return res | ||
| } | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| func BenchmarkChan_SPSC_Seq(b *testing.B) { | ||
| for _, size := range payloadSizes { | ||
| b.Run(fmt.Sprintf("%dB", size), func(b *testing.B) { | ||
| ch := make(chan []byte, ringSize) | ||
| payload := make([]byte, size) | ||
|
|
||
| b.SetBytes(int64(size)) | ||
| b.ResetTimer() | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| ch <- payload | ||
| <-ch | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkChan_MPSC_Parallel(b *testing.B) { | ||
| const producers = 4 | ||
| for _, size := range payloadSizes { | ||
| b.Run(fmt.Sprintf("%dB", size), func(b *testing.B) { | ||
| ch := make(chan []byte, 1024) | ||
| payload := make([]byte, size) | ||
|
|
||
| b.SetBytes(int64(size)) | ||
| b.ResetTimer() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(producers) | ||
|
|
||
| for i := 0; i < producers; i++ { | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| ch <- payload | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| for i := 0; i < b.N*producers; i++ { | ||
| <-ch | ||
| } | ||
| wg.Wait() | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkChan_SPMC_Parallel(b *testing.B) { | ||
| const consumers = 4 | ||
| for _, size := range payloadSizes { | ||
| b.Run(fmt.Sprintf("%dB", size), func(b *testing.B) { | ||
| ch := make(chan []byte, 1024) | ||
| payload := make([]byte, size) | ||
|
|
||
| b.SetBytes(int64(size)) | ||
| b.ResetTimer() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(consumers) | ||
|
|
||
| for i := 0; i < consumers; i++ { | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| <-ch | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| for i := 0; i < b.N*consumers; i++ { | ||
| ch <- payload | ||
| } | ||
| wg.Wait() | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkChan_MPMC_Parallel(b *testing.B) { | ||
| const ( | ||
| producers = 4 | ||
| consumers = 4 | ||
| ) | ||
| for _, size := range payloadSizes { | ||
| b.Run(fmt.Sprintf("%dB", size), func(b *testing.B) { | ||
| ch := make(chan []byte, 1024) | ||
| payload := make([]byte, size) | ||
|
|
||
| b.SetBytes(int64(size)) | ||
| b.ResetTimer() | ||
|
|
||
| var prodWG, consWG sync.WaitGroup | ||
| prodWG.Add(producers) | ||
| consWG.Add(consumers) | ||
|
|
||
| for i := 0; i < producers; i++ { | ||
| go func() { | ||
| defer prodWG.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| ch <- payload | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| for i := 0; i < consumers; i++ { | ||
| go func() { | ||
| defer consWG.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| <-ch | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| prodWG.Wait() | ||
| consWG.Wait() | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkChan_SPSC_Seq_Batch(b *testing.B) { | ||
| for _, batchSize := range batchSizes { | ||
| for _, payloadSize := range payloadSizes { | ||
| b.Run( | ||
| fmt.Sprintf("batch%d_%dB", batchSize, payloadSize), | ||
| func(b *testing.B) { | ||
| ch := make(chan [][]byte, ringSize) | ||
| payload := make([][]byte, batchSize) | ||
| for i := range payload { | ||
| payload[i] = make([]byte, payloadSize) | ||
| } | ||
|
|
||
| b.SetBytes(int64(payloadSize)) | ||
| b.ResetTimer() | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| ch <- payload | ||
| <-ch | ||
| } | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkChan_MPSC_Parallel_Batch(b *testing.B) { | ||
| const producers = 4 | ||
| for _, batchSize := range batchSizes { | ||
| for _, payloadSize := range payloadSizes { | ||
| b.Run( | ||
| fmt.Sprintf("batch%d_%dB", batchSize, payloadSize), | ||
| func(b *testing.B) { | ||
| ch := make(chan [][]byte, ringSize) | ||
| payload := make([][]byte, batchSize) | ||
| for i := range payload { | ||
| payload[i] = make([]byte, payloadSize) | ||
| } | ||
|
|
||
| b.SetBytes(int64(payloadSize)) | ||
| b.ResetTimer() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(producers) | ||
|
|
||
| for i := 0; i < producers; i++ { | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| ch <- payload | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| for i := 0; i < b.N*producers; i++ { | ||
| <-ch | ||
| } | ||
| wg.Wait() | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkChan_SPMC_Parallel_Batch(b *testing.B) { | ||
| const consumers = 4 | ||
| for _, batchSize := range batchSizes { | ||
| for _, payloadSize := range payloadSizes { | ||
| b.Run( | ||
| fmt.Sprintf("batch%d_%dB", batchSize, payloadSize), | ||
| func(b *testing.B) { | ||
| ch := make(chan [][]byte, ringSize) | ||
| payload := make([][]byte, batchSize) | ||
| for i := range payload { | ||
| payload[i] = make([]byte, payloadSize) | ||
| } | ||
|
|
||
| b.SetBytes(int64(payloadSize)) | ||
| b.ResetTimer() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(consumers) | ||
|
|
||
| for i := 0; i < consumers; i++ { | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| <-ch | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| for i := 0; i < b.N*consumers; i++ { | ||
| ch <- payload | ||
| } | ||
| wg.Wait() | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkChan_MPMC_Parallel_Batch(b *testing.B) { | ||
| const ( | ||
| producers = 4 | ||
| consumers = 4 | ||
| ) | ||
| for _, batchSize := range batchSizes { | ||
| for _, payloadSize := range payloadSizes { | ||
| b.Run( | ||
| fmt.Sprintf("batch%d_%dB", batchSize, payloadSize), | ||
| func(b *testing.B) { | ||
| ch := make(chan [][]byte, ringSize) | ||
| payload := make([][]byte, batchSize) | ||
| for i := range payload { | ||
| payload[i] = make([]byte, payloadSize) | ||
| } | ||
|
|
||
| b.SetBytes(int64(payloadSize)) | ||
| b.ResetTimer() | ||
|
|
||
| var prodWG, consWG sync.WaitGroup | ||
| prodWG.Add(producers) | ||
| consWG.Add(consumers) | ||
|
|
||
| for i := 0; i < producers; i++ { | ||
| go func() { | ||
| defer prodWG.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| ch <- payload | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| for i := 0; i < consumers; i++ { | ||
| go func() { | ||
| defer consWG.Done() | ||
| for j := 0; j < b.N; j++ { | ||
| <-ch | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| prodWG.Wait() | ||
| consWG.Wait() | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package ring_test | ||
|
|
||
| const ringSize = 1024 | ||
|
|
||
| var ( | ||
| batchSizes = []int{1, 8, 16, 32, 64} | ||
| payloadSizes = []int{8, 32, 64, 256, 512} // bytes | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused ChanWrapper type.
The
ChanWrappertype and its methods are defined but never used in any of the benchmarks below. All benchmarks use raw channels directly.Apply this diff to remove the unused code:
Alternatively, if you intended to use
ChanWrapperfor testing, you would need to update the benchmarks to actually use it.📝 Committable suggestion
🤖 Prompt for AI Agents