Skip to content

Commit 1709e4c

Browse files
author
Mike Davis
authored
refact(bench): Consolidate benchmarking for EventProcessor. (#204)
1 parent 973644b commit 1709e4c

File tree

4 files changed

+67
-235
lines changed

4 files changed

+67
-235
lines changed

pkg/event/chan_queue.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

pkg/event/chan_queue_test.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

pkg/event/factory_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type TestConfig struct {
3232
pkg.ProjectConfig
3333
}
3434

35+
func (TestConfig) GetAttributeByKey(string) (entities.Attribute, error) {
36+
return entities.Attribute{ID: "100000", Key: "sample_attribute"}, nil
37+
}
38+
3539
func (TestConfig) GetEventByKey(string) (entities.Event, error) {
3640
return entities.Event{ExperimentIds: []string{"15402980349"}, ID: "15368860886", Key: "sample_conversion"}, nil
3741
}
@@ -77,7 +81,7 @@ func RandomString(len int) string {
7781
var userID = RandomString(10)
7882
var userContext = entities.UserContext{
7983
ID: userID,
80-
Attributes: make(map[string]interface{}),
84+
Attributes: map[string]interface{}{"test": "val"},
8185
}
8286

8387
func BuildTestImpressionEvent() UserEvent {

pkg/event/processor_test.go

Lines changed: 62 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@ package event
1919

2020
import (
2121
"errors"
22+
"fmt"
2223
"github.com/optimizely/go-sdk/pkg/logging"
2324
"github.com/optimizely/go-sdk/pkg/utils"
2425
"github.com/stretchr/testify/assert"
26+
"math"
2527
"testing"
2628
"time"
2729
)
2830

31+
type CountingDispatcher struct {
32+
eventCount int
33+
visitorCount int
34+
}
35+
36+
func (c *CountingDispatcher) DispatchEvent(event LogEvent) (bool, error) {
37+
c.eventCount++
38+
c.visitorCount += len(event.Event.Visitors)
39+
return true, nil
40+
}
41+
42+
2943
type MockDispatcher struct {
3044
ShouldFail bool
3145
Events Queue
@@ -467,119 +481,69 @@ func (l *NoOpLogger) SetLogLevel(level logging.LogLevel) {
467481
goos: darwin
468482
goarch: amd64
469483
pkg: github.com/optimizely/go-sdk/pkg/event
470-
BenchmarkWithQueueSize/QueueSize100-8 2000000 774 ns/op
471-
BenchmarkWithQueueSize/QueueSize500-8 2000000 716 ns/op
472-
BenchmarkWithQueueSize/QueueSize1000-8 2000000 690 ns/op
473-
BenchmarkWithQueueSize/QueueSize2000-8 2000000 732 ns/op
474-
BenchmarkWithQueueSize/QueueSize3000-8 2000000 735 ns/op
475-
BenchmarkWithQueueSize/QueueSize4000-8 2000000 740 ns/op
476-
477-
BenchmarkWithBatchSize/BatchSize10-8 2000000 665 ns/op
478-
BenchmarkWithBatchSize/BatchSize20-8 2000000 597 ns/op
479-
BenchmarkWithBatchSize/BatchSize30-8 3000000 556 ns/op
480-
BenchmarkWithBatchSize/BatchSize40-8 3000000 566 ns/op
481-
BenchmarkWithBatchSize/BatchSize50-8 3000000 546 ns/op
482-
BenchmarkWithBatchSize/BatchSize60-8 3000000 504 ns/op
483-
484-
BenchmarkWithQueue/InMemoryQueue-8 2000000 674 ns/op
485-
BenchmarkWithQueue/ChannelQueue-8 2000000 937 ns/op
486-
484+
BenchmarkProcessor/InMemory/BatchSize-10/QueueSize-10-8 2531830 456 ns/op
485+
BenchmarkProcessor/InMemory/BatchSize-20/QueueSize-10-8 2966862 398 ns/op
486+
BenchmarkProcessor/InMemory/BatchSize-30/QueueSize-10-8 3224689 372 ns/op
487+
BenchmarkProcessor/InMemory/BatchSize-40/QueueSize-10-8 3283634 384 ns/op
488+
BenchmarkProcessor/InMemory/BatchSize-50/QueueSize-10-8 3368804 352 ns/op
489+
BenchmarkProcessor/InMemory/BatchSize-60/QueueSize-10-8 3468763 336 ns/op
490+
BenchmarkProcessor/InMemory/BatchSize-10/QueueSize-100-8 2581394 464 ns/op
491+
BenchmarkProcessor/InMemory/BatchSize-20/QueueSize-100-8 2911731 408 ns/op
492+
BenchmarkProcessor/InMemory/BatchSize-30/QueueSize-100-8 3224674 375 ns/op
493+
BenchmarkProcessor/InMemory/BatchSize-40/QueueSize-100-8 3262027 366 ns/op
494+
BenchmarkProcessor/InMemory/BatchSize-50/QueueSize-100-8 3094736 354 ns/op
495+
BenchmarkProcessor/InMemory/BatchSize-60/QueueSize-100-8 3523911 338 ns/op
496+
BenchmarkProcessor/InMemory/BatchSize-10/QueueSize-1000-8 2580465 467 ns/op
497+
BenchmarkProcessor/InMemory/BatchSize-20/QueueSize-1000-8 2940940 415 ns/op
498+
BenchmarkProcessor/InMemory/BatchSize-30/QueueSize-1000-8 3229284 375 ns/op
499+
BenchmarkProcessor/InMemory/BatchSize-40/QueueSize-1000-8 3280029 367 ns/op
500+
BenchmarkProcessor/InMemory/BatchSize-50/QueueSize-1000-8 3258297 368 ns/op
501+
BenchmarkProcessor/InMemory/BatchSize-60/QueueSize-1000-8 3484419 336 ns/op
502+
BenchmarkProcessor/InMemory/BatchSize-10/QueueSize-10000-8 2598885 462 ns/op
503+
BenchmarkProcessor/InMemory/BatchSize-20/QueueSize-10000-8 2907445 414 ns/op
504+
BenchmarkProcessor/InMemory/BatchSize-30/QueueSize-10000-8 3215616 382 ns/op
505+
BenchmarkProcessor/InMemory/BatchSize-40/QueueSize-10000-8 3243544 367 ns/op
506+
BenchmarkProcessor/InMemory/BatchSize-50/QueueSize-10000-8 3382228 391 ns/op
507+
BenchmarkProcessor/InMemory/BatchSize-60/QueueSize-10000-8 3503428 354 ns/op
508+
BenchmarkProcessor/InMemory/BatchSize-10/QueueSize-100000-8 2268799 512 ns/op
509+
BenchmarkProcessor/InMemory/BatchSize-20/QueueSize-100000-8 2788728 429 ns/op
510+
BenchmarkProcessor/InMemory/BatchSize-30/QueueSize-100000-8 2799598 404 ns/op
511+
BenchmarkProcessor/InMemory/BatchSize-40/QueueSize-100000-8 3010062 368 ns/op
512+
BenchmarkProcessor/InMemory/BatchSize-50/QueueSize-100000-8 3353461 352 ns/op
513+
BenchmarkProcessor/InMemory/BatchSize-60/QueueSize-100000-8 3429447 342 ns/op
487514
*/
488-
func BenchmarkWithQueueSize(b *testing.B) {
515+
func BenchmarkProcessor(b *testing.B) {
489516
// no op logger added to keep out extra discarded events
490517
logging.SetLogger(&NoOpLogger{})
491518

492519
merges := []struct {
493-
name string
494-
qSize int
520+
name string
521+
fun func(qSize int) Queue
495522
}{
496-
{"QueueSize100", 100},
497-
{"QueueSize500", 500},
498-
{"QueueSize1000", 1000},
499-
{"QueueSize2000", 2000},
500-
{"QueueSize3000", 3000},
501-
{"QueueSize4000", 4000},
523+
{"InMemory", NewInMemoryQueue},
502524
}
503525

504526
for _, merge := range merges {
505-
var totalSent = 0
506-
var numberRun = 0
507-
b.Run(merge.name, func(b *testing.B) {
508-
if numberRun == 0 {
509-
numberRun = b.N
527+
for i := 1.; i <= 5; i++ {
528+
qs := int(math.Pow(10, i))
529+
for j := 1; j <= 6; j++ {
530+
bs := 10 * j
531+
b.Run(fmt.Sprintf("%s/BatchSize-%d/QueueSize-%d", merge.name, bs, qs), func(b *testing.B) {
532+
q := merge.fun(qs)
533+
benchmarkProcessor(q, bs, b)
534+
})
510535
}
511-
totalSent += benchmarkProcessorWithQueueSize(merge.qSize, b)
512-
})
513-
if totalSent < numberRun {
514-
println("Total sent and run ", totalSent, numberRun)
515-
b.Fail()
516-
}
517-
}
518-
}
519-
520-
func BenchmarkWithBatchSize(b *testing.B) {
521-
logging.SetLogger(&NoOpLogger{})
522-
523-
merges := []struct {
524-
name string
525-
batchSize int
526-
}{
527-
{"BatchSize10", 10},
528-
{"BatchSize20", 20},
529-
{"BatchSize30", 30},
530-
{"BatchSize40", 40},
531-
{"BatchSize50", 50},
532-
{"BatchSize60", 60},
533-
}
534-
535-
for _, merge := range merges {
536-
b.Run(merge.name, func(b *testing.B) {
537-
benchmarkProcessorWithBatchSize(merge.batchSize, b)
538-
})
539-
}
540-
541-
}
542-
543-
func BenchmarkWithQueue(b *testing.B) {
544-
logging.SetLogger(&NoOpLogger{})
545-
546-
b.Run("InMemoryQueue", func(b *testing.B) {
547-
benchmarkProcessorWithQueue(NewInMemoryQueue(defaultQueueSize), b)
548-
})
549-
550-
b.Run("ChannelQueue", func(b *testing.B) {
551-
benchmarkProcessorWithQueue(NewChanQueue(defaultQueueSize), b)
552-
})
553-
554-
}
555-
556-
func benchmarkProcessorWithQueueSize(qSize int, b *testing.B) int {
557-
exeCtx := utils.NewCancelableExecutionCtx()
558-
dispatcher := NewMockDispatcher(100, false)
559-
processor := NewBatchEventProcessor(
560-
WithQueueSize(qSize),
561-
WithEventDispatcher(dispatcher))
562-
processor.Start(exeCtx)
563-
564-
conversion := BuildTestConversionEvent()
565-
566-
for i := 0; i < b.N; i++ {
567-
var success = false
568-
for !success {
569-
success = processor.ProcessEvent(conversion)
570536
}
571537
}
572538

573-
exeCtx.TerminateAndWait()
574-
575-
return dispatcher.Events.Size()
576539
}
577540

578-
func benchmarkProcessorWithQueue(q Queue, b *testing.B) int {
541+
func benchmarkProcessor(q Queue, bSize int, b *testing.B) {
579542
exeCtx := utils.NewCancelableExecutionCtx()
580-
dispatcher := NewMockDispatcher(100, false)
543+
dispatcher := &CountingDispatcher{}
581544
processor := NewBatchEventProcessor(
582545
WithQueue(q),
546+
WithBatchSize(bSize),
583547
WithEventDispatcher(dispatcher))
584548
processor.Start(exeCtx)
585549

@@ -590,33 +554,12 @@ func benchmarkProcessorWithQueue(q Queue, b *testing.B) int {
590554
for !success {
591555
success = processor.ProcessEvent(conversion)
592556
}
593-
//time.Sleep(benchmarkSleep)
594557
}
595558

596559
exeCtx.TerminateAndWait()
597560

598-
return dispatcher.Events.Size()
599-
}
600-
601-
func benchmarkProcessorWithBatchSize(bs int, b *testing.B) int {
602-
exeCtx := utils.NewCancelableExecutionCtx()
603-
dispatcher := NewMockDispatcher(100, false)
604-
processor := NewBatchEventProcessor(
605-
WithBatchSize(bs),
606-
WithEventDispatcher(dispatcher))
607-
processor.Start(exeCtx)
608-
609-
conversion := BuildTestConversionEvent()
610-
611-
for i := 0; i < b.N; i++ {
612-
var success = false
613-
for !success {
614-
success = processor.ProcessEvent(conversion)
615-
}
616-
//time.Sleep(benchmarkSleep)
561+
if b.N != dispatcher.visitorCount {
562+
println("Total sent and run ", dispatcher.visitorCount, b.N)
563+
b.Fail()
617564
}
618-
619-
exeCtx.TerminateAndWait()
620-
621-
return dispatcher.Events.Size()
622565
}

0 commit comments

Comments
 (0)