mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-06 14:33:10 -06:00
Add more benchmarks
This commit is contained in:
parent
606a0c552d
commit
5b9b1d31a8
|
|
@ -5,24 +5,42 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/matrix-org/util"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BenchmarkPrevEventIDs(b *testing.B) {
|
func BenchmarkPrevEventIDs(b *testing.B) {
|
||||||
for _, x := range []int64{1, 2, 4, 10, 50, 100, 300, 500, 800, 1000, 2000, 3000, 5000, 10000} {
|
for _, x := range []int64{1, 10, 100, 500, 1000, 2000} {
|
||||||
benchPrevEventIDs(b, int(x))
|
benchPrevEventIDs(b, int(x))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func benchPrevEventIDs(b *testing.B, count int) {
|
func benchPrevEventIDs(b *testing.B, count int) {
|
||||||
b.Run(fmt.Sprintf("Benchmark%d", count), func(b *testing.B) {
|
bwExtrems := generateBackwardsExtremities(b, count)
|
||||||
bwExtrems := generateBackwardsExtremities(b, count)
|
backfiller := testPerformBackfillRequest{
|
||||||
backfiller := PerformBackfillRequest{
|
BackwardsExtremities: bwExtrems,
|
||||||
BackwardsExtremities: bwExtrems,
|
}
|
||||||
}
|
|
||||||
|
b.Run(fmt.Sprintf("Original%d", count), func(b *testing.B) {
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
prevIDs := backfiller.PrevEventIDs()
|
prevIDs := backfiller.originalPrevEventIDs()
|
||||||
|
_ = prevIDs
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run(fmt.Sprintf("FirstAttempt%d", count), func(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
prevIDs := backfiller.firstAttemptPrevEventIDs()
|
||||||
|
_ = prevIDs
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run(fmt.Sprintf("UsingMap%d", count), func(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
prevIDs := backfiller.prevEventIDsUsingMap()
|
||||||
_ = prevIDs
|
_ = prevIDs
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -48,9 +66,71 @@ func generateBackwardsExtremities(t testLike, count int) map[string][]string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PerformBackfillRequest is a request to PerformBackfill.
|
||||||
|
type testPerformBackfillRequest struct {
|
||||||
|
BackwardsExtremities map[string][]string `json:"backwards_extremities"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *testPerformBackfillRequest) originalPrevEventIDs() []string {
|
||||||
|
var prevEventIDs []string
|
||||||
|
for _, pes := range r.BackwardsExtremities {
|
||||||
|
prevEventIDs = append(prevEventIDs, pes...)
|
||||||
|
}
|
||||||
|
prevEventIDs = util.UniqueStrings(prevEventIDs)
|
||||||
|
return prevEventIDs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *testPerformBackfillRequest) firstAttemptPrevEventIDs() []string {
|
||||||
|
// Collect 1k eventIDs, if possible, they may be cleared out below
|
||||||
|
maxPrevEventIDs := len(r.BackwardsExtremities) * 3
|
||||||
|
if maxPrevEventIDs > 2000 {
|
||||||
|
maxPrevEventIDs = 2000
|
||||||
|
}
|
||||||
|
prevEventIDs := make([]string, 0, maxPrevEventIDs)
|
||||||
|
for _, pes := range r.BackwardsExtremities {
|
||||||
|
prevEventIDs = append(prevEventIDs, pes...)
|
||||||
|
if len(prevEventIDs) > 1000 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prevEventIDs = util.UniqueStrings(prevEventIDs)
|
||||||
|
// If we still have > 100 eventIDs, only return the first 100
|
||||||
|
if len(prevEventIDs) > 100 {
|
||||||
|
return prevEventIDs[:100]
|
||||||
|
}
|
||||||
|
return prevEventIDs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *testPerformBackfillRequest) prevEventIDsUsingMap() []string {
|
||||||
|
var uniqueIDs map[string]struct{}
|
||||||
|
if len(r.BackwardsExtremities) > 100 {
|
||||||
|
uniqueIDs = make(map[string]struct{}, 100)
|
||||||
|
} else {
|
||||||
|
uniqueIDs = make(map[string]struct{}, len(r.BackwardsExtremities))
|
||||||
|
}
|
||||||
|
|
||||||
|
outerLoop:
|
||||||
|
for _, pes := range r.BackwardsExtremities {
|
||||||
|
for _, evID := range pes {
|
||||||
|
uniqueIDs[evID] = struct{}{}
|
||||||
|
if len(uniqueIDs) >= 100 {
|
||||||
|
break outerLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]string, len(uniqueIDs))
|
||||||
|
i := 0
|
||||||
|
for evID := range uniqueIDs {
|
||||||
|
result[i] = evID
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
const alphanumerics = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
const alphanumerics = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
|
|
||||||
// RandomString generates a pseudo-random string of length n.
|
// randomEventId generates a pseudo-random string of length n.
|
||||||
func randomEventId(src int64) string {
|
func randomEventId(src int64) string {
|
||||||
randSrc := rand.NewSource(src)
|
randSrc := rand.NewSource(src)
|
||||||
b := make([]byte, randomIDCharsCount)
|
b := make([]byte, randomIDCharsCount)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue