/
main.go
116 lines (92 loc) · 2.33 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
package main
import (
"flag"
"os"
"runtime/pprof"
"github.com/hei6775/light-protoactor-go/actor"
"github.com/hei6775/light-protoactor-go/examples/remotebenchmark/messages"
"github.com/hei6775/light-protoactor-go/remote"
"log"
"sync"
"runtime"
"time"
"github.com/hei6775/light-protoactor-go/mailbox"
)
type localActor struct {
count int
wgStop *sync.WaitGroup
messageCount int
}
func (state *localActor) Receive(context actor.Context) {
switch context.Message().(type) {
case *messages.Pong:
state.count++
if state.count%50000 == 0 {
log.Println(state.count)
}
if state.count == state.messageCount {
state.wgStop.Done()
}
}
}
func newLocalActor(stop *sync.WaitGroup, messageCount int) actor.Producer {
return func() actor.Actor {
return &localActor{
wgStop: stop,
messageCount: messageCount,
}
}
}
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() * 1)
runtime.GC()
var wg sync.WaitGroup
messageCount := 1000000
remote.Start("127.0.0.1:8081")
props := actor.
FromProducer(newLocalActor(&wg, messageCount)).
WithMailbox(mailbox.Bounded(1000000))
pid := actor.Spawn(props)
remote := actor.NewPID("127.0.0.1:8080", "remote")
actor.RequestFuture(remote, &messages.StartRemote{Sender: pid}, 5*time.Second).Wait()
wg.Add(1)
start := time.Now()
log.Println("Starting to send")
message := &messages.Ping{}
for i := 0; i < messageCount; i++ {
actor.Tell(remote, message)
}
wg.Wait()
elapsed := time.Since(start)
log.Printf("Elapsed %s", elapsed)
x := int(float32(messageCount*2) / (float32(elapsed) / float32(time.Second)))
log.Printf("Msg per sec %v", x)
// f, err := os.Create("memprofile")
// if err != nil {
// log.Fatal(err)
// }
// pprof.WriteHeapProfile(f)
// f.Close()
}