File size: 2,238 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package bridge

import (
	"context"
	"fmt"
	"sync"
	"testing"
	"time"
)

func BenchmarkTabExecutor_SequentialSameTab(b *testing.B) {
	te := NewTabExecutor(4)
	ctx := context.Background()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = te.Execute(ctx, "tab1", func(ctx context.Context) error {
			return nil
		})
	}
}

func BenchmarkTabExecutor_ParallelDifferentTabs(b *testing.B) {
	te := NewTabExecutor(8)
	ctx := context.Background()
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		i := 0
		for pb.Next() {
			tabID := fmt.Sprintf("tab%d", i%8)
			_ = te.Execute(ctx, tabID, func(ctx context.Context) error {
				return nil
			})
			i++
		}
	})
}

func BenchmarkTabExecutor_ParallelSameTab(b *testing.B) {
	te := NewTabExecutor(8)
	ctx := context.Background()
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			_ = te.Execute(ctx, "tab1", func(ctx context.Context) error {
				return nil
			})
		}
	})
}

func BenchmarkTabExecutor_WithWork(b *testing.B) {
	te := NewTabExecutor(4)
	ctx := context.Background()
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		i := 0
		for pb.Next() {
			tabID := fmt.Sprintf("tab%d", i%4)
			_ = te.Execute(ctx, tabID, func(ctx context.Context) error {
				// Simulate light work
				sum := 0
				for j := 0; j < 100; j++ {
					sum += j
				}
				_ = sum
				return nil
			})
			i++
		}
	})
}

func BenchmarkSequentialVsParallel(b *testing.B) {
	workDuration := time.Microsecond * 100

	b.Run("Sequential_4Tabs", func(b *testing.B) {
		te := NewTabExecutor(1)
		ctx := context.Background()
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			for j := 0; j < 4; j++ {
				_ = te.Execute(ctx, fmt.Sprintf("tab%d", j), func(ctx context.Context) error {
					time.Sleep(workDuration)
					return nil
				})
			}
		}
	})

	b.Run("Parallel_4Tabs", func(b *testing.B) {
		te := NewTabExecutor(4)
		ctx := context.Background()
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			var wg sync.WaitGroup
			for j := 0; j < 4; j++ {
				wg.Add(1)
				tabID := fmt.Sprintf("tab%d", j)
				go func() {
					defer wg.Done()
					_ = te.Execute(ctx, tabID, func(ctx context.Context) error {
						time.Sleep(workDuration)
						return nil
					})
				}()
			}
			wg.Wait()
		}
	})
}