-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(metarepos): add a pool for []*mrpb.Report
This change adds the pool for []*mrpb.Report, which is reportQueuePool. ``` BenchmarkReportQueuePool/WithoutPool-16 763526 1573 ns/op 8192 B/op 1 allocs/op BenchmarkReportQueuePool/WithoutPool-16 745612 1574 ns/op 8192 B/op 1 allocs/op BenchmarkReportQueuePool/WithoutPool-16 738848 1569 ns/op 8192 B/op 1 allocs/op BenchmarkReportQueuePool/WithoutPool-16 765339 1563 ns/op 8192 B/op 1 allocs/op BenchmarkReportQueuePool/WithoutPool-16 739354 1567 ns/op 8192 B/op 1 allocs/op BenchmarkReportQueuePool/WithPool-16 4445330 270.2 ns/op 24 B/op 1 allocs/op BenchmarkReportQueuePool/WithPool-16 4342182 271.9 ns/op 24 B/op 1 allocs/op BenchmarkReportQueuePool/WithPool-16 4517366 267.3 ns/op 24 B/op 1 allocs/op BenchmarkReportQueuePool/WithPool-16 4440441 263.5 ns/op 24 B/op 1 allocs/op BenchmarkReportQueuePool/WithPool-16 4666981 263.1 ns/op 24 B/op 1 allocs/op ```
- Loading branch information
Showing
3 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,84 @@ | ||
package mrpb | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kakao/varlog/pkg/types" | ||
) | ||
|
||
func TestReports(t *testing.T) { | ||
const nid = types.NodeID(1) | ||
rng := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
for i := 0; i < 1e4; i++ { | ||
reports := NewReports(nid, time.Now()) | ||
require.Empty(t, reports.Reports) | ||
|
||
queue := NewReportQueue() | ||
require.Empty(t, queue) | ||
require.Equal(t, reportQueueSize, cap(queue)) | ||
numReports := rng.Intn(reportQueueSize*2) + 1 | ||
for j := 0; j < numReports; j++ { | ||
queue = append(queue, &Report{}) | ||
} | ||
require.Len(t, queue, numReports) | ||
reports.Reports = queue | ||
|
||
reports.Release() | ||
} | ||
} | ||
|
||
func TestReportQueuePool(t *testing.T) { | ||
rng := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
for i := 0; i < 1e4; i++ { | ||
queue := NewReportQueue() | ||
require.Empty(t, queue) | ||
require.Equal(t, reportQueueSize, cap(queue)) | ||
|
||
numReports := rng.Intn(reportQueueSize*2) + 1 | ||
for j := 0; j < numReports; j++ { | ||
queue = append(queue, &Report{}) | ||
} | ||
require.Len(t, queue, numReports) | ||
|
||
queue.Release() | ||
} | ||
} | ||
|
||
func BenchmarkReportQueuePool(b *testing.B) { | ||
const numReports = 128 | ||
predefinedReports := make([]*Report, numReports) | ||
for i := 0; i < numReports; i++ { | ||
predefinedReports[i] = &Report{} | ||
} | ||
reports := &Reports{} | ||
|
||
reports.Reports = make([]*Report, 0, reportQueueSize) | ||
b.ResetTimer() | ||
b.Run("WithoutPool", func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
for j := 0; j < numReports; j++ { | ||
reports.Reports = append(reports.Reports, predefinedReports[j]) | ||
} | ||
reports.Reports = make([]*Report, 0, reportQueueSize) | ||
} | ||
}) | ||
|
||
reports.Reports = NewReportQueue() | ||
b.ResetTimer() | ||
b.Run("WithPool", func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
for j := 0; j < numReports; j++ { | ||
reports.Reports = append(reports.Reports, predefinedReports[j]) | ||
} | ||
rq := (ReportQueue)(reports.Reports) | ||
rq.Release() | ||
reports.Reports = NewReportQueue() | ||
} | ||
}) | ||
} |