forked from asynkron/protoactor-go
/
main.go
177 lines (147 loc) · 3.49 KB
/
main.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"flag"
"os"
"runtime/pprof"
"github.com/AsynkronIT/protoactor-go/actor"
"log"
"sync"
"runtime"
"time"
"github.com/AsynkronIT/protoactor-go/mailbox"
)
type Msg struct {
Sender *actor.PID
}
type Start struct {
Sender *actor.PID
}
//type Started struct{}
type pingActor struct {
count int
wgStop *sync.WaitGroup
messageCount int
batch int
batchSize int
}
func (state *pingActor) sendBatch(context actor.Context, sender *actor.PID) bool {
if state.messageCount == 0 {
return false
}
var m interface{} = &Msg{
Sender: context.Self(),
}
for i := 0; i < state.batchSize; i++ {
sender.Tell(m)
}
state.messageCount -= state.batchSize
state.batch = state.batchSize
return true
}
func (state *pingActor) Receive(context actor.Context) {
switch msg := context.Message().(type) {
case *Start:
state.sendBatch(context, msg.Sender)
case *Msg:
state.batch--
if state.batch > 0 {
return
}
if !state.sendBatch(context, msg.Sender) {
state.wgStop.Done()
}
}
}
func newPingActor(stop *sync.WaitGroup, messageCount int, batchSize int) actor.Producer {
return func() actor.Actor {
return &pingActor{
wgStop: stop,
messageCount: messageCount,
batchSize: batchSize,
}
}
}
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var blockProfile = flag.String("blockprof", "", "execute contention profiling and save results here")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Check for lock contention profiling
if *blockProfile != "" {
prof, err := os.Create(*blockProfile)
if err != nil {
log.Fatal(err)
}
runtime.SetBlockProfileRate(1)
defer func() {
pprof.Lookup("block").WriteTo(prof, 0)
}()
}
runtime.GOMAXPROCS(runtime.NumCPU())
runtime.GC()
var wg sync.WaitGroup
messageCount := 1000000
batchSize := 100
tps := []int{300, 400, 500, 600, 700, 800, 900}
log.Println("Dispatcher Throughput Elapsed Time Messages per sec")
for _, tp := range tps {
d := mailbox.NewDefaultDispatcher(tp)
clientProps := actor.
FromProducer(newPingActor(&wg, messageCount, batchSize)).
WithMailbox(mailbox.Bounded(batchSize + 10)).
WithDispatcher(d)
echoProps := actor.
FromFunc(
func(context actor.Context) {
switch msg := context.Message().(type) {
case *Msg:
msg.Sender.Tell(msg)
}
}).
WithMailbox(mailbox.Bounded(batchSize + 10)).
WithDispatcher(d)
clients := make([]*actor.PID, 0)
echos := make([]*actor.PID, 0)
clientCount := runtime.NumCPU() * 2
for i := 0; i < clientCount; i++ {
client := actor.Spawn(clientProps)
echo := actor.Spawn(echoProps)
clients = append(clients, client)
echos = append(echos, echo)
wg.Add(1)
}
start := time.Now()
for i := 0; i < clientCount; i++ {
client := clients[i]
echo := echos[i]
client.Tell(&Start{
Sender: echo,
})
}
wg.Wait()
elapsed := time.Since(start)
x := int(float32(messageCount*2*clientCount) / (float32(elapsed) / float32(time.Second)))
log.Printf(" %v %s %v", tp, elapsed, x)
for i := 0; i < clientCount; i++ {
client := clients[i]
client.StopFuture().Wait()
echo := echos[i]
echo.StopFuture().Wait()
}
runtime.GC()
time.Sleep(2 * time.Second)
}
// f, err := os.Create("memprofile")
// if err != nil {
// log.Fatal(err)
// }
// pprof.WriteHeapProfile(f)
// f.Close()
}