@@ -19,13 +19,27 @@ package event
19
19
20
20
import (
21
21
"errors"
22
+ "fmt"
22
23
"github.com/optimizely/go-sdk/pkg/logging"
23
24
"github.com/optimizely/go-sdk/pkg/utils"
24
25
"github.com/stretchr/testify/assert"
26
+ "math"
25
27
"testing"
26
28
"time"
27
29
)
28
30
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
+
29
43
type MockDispatcher struct {
30
44
ShouldFail bool
31
45
Events Queue
@@ -467,119 +481,69 @@ func (l *NoOpLogger) SetLogLevel(level logging.LogLevel) {
467
481
goos: darwin
468
482
goarch: amd64
469
483
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
487
514
*/
488
- func BenchmarkWithQueueSize (b * testing.B ) {
515
+ func BenchmarkProcessor (b * testing.B ) {
489
516
// no op logger added to keep out extra discarded events
490
517
logging .SetLogger (& NoOpLogger {})
491
518
492
519
merges := []struct {
493
- name string
494
- qSize int
520
+ name string
521
+ fun func ( qSize int ) Queue
495
522
}{
496
- {"QueueSize100" , 100 },
497
- {"QueueSize500" , 500 },
498
- {"QueueSize1000" , 1000 },
499
- {"QueueSize2000" , 2000 },
500
- {"QueueSize3000" , 3000 },
501
- {"QueueSize4000" , 4000 },
523
+ {"InMemory" , NewInMemoryQueue },
502
524
}
503
525
504
526
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
+ })
510
535
}
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 )
570
536
}
571
537
}
572
538
573
- exeCtx .TerminateAndWait ()
574
-
575
- return dispatcher .Events .Size ()
576
539
}
577
540
578
- func benchmarkProcessorWithQueue (q Queue , b * testing.B ) int {
541
+ func benchmarkProcessor (q Queue , bSize int , b * testing.B ) {
579
542
exeCtx := utils .NewCancelableExecutionCtx ()
580
- dispatcher := NewMockDispatcher ( 100 , false )
543
+ dispatcher := & CountingDispatcher {}
581
544
processor := NewBatchEventProcessor (
582
545
WithQueue (q ),
546
+ WithBatchSize (bSize ),
583
547
WithEventDispatcher (dispatcher ))
584
548
processor .Start (exeCtx )
585
549
@@ -590,33 +554,12 @@ func benchmarkProcessorWithQueue(q Queue, b *testing.B) int {
590
554
for ! success {
591
555
success = processor .ProcessEvent (conversion )
592
556
}
593
- //time.Sleep(benchmarkSleep)
594
557
}
595
558
596
559
exeCtx .TerminateAndWait ()
597
560
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 ()
617
564
}
618
-
619
- exeCtx .TerminateAndWait ()
620
-
621
- return dispatcher .Events .Size ()
622
565
}
0 commit comments