1 | // https://github.com/AsynkronIT/protoactor-go |
---|
2 | // https://pkg.go.dev/github.com/AsynkronIT/protoactor-go/actor |
---|
3 | package main |
---|
4 | |
---|
5 | import ( |
---|
6 | "os"; "strconv"; "fmt"; "time"; "runtime"; "sync/atomic" |
---|
7 | "github.com/asynkron/protoactor-go/actor" |
---|
8 | ) |
---|
9 | |
---|
10 | var Messages, Times uint64 = 100_000, 10 |
---|
11 | var Processors int = 4 |
---|
12 | |
---|
13 | var starttime time.Time; |
---|
14 | var shake = make( chan string ) |
---|
15 | var actorCnt uint64 = 0 |
---|
16 | |
---|
17 | type IntMsg struct { val int } |
---|
18 | type CharMsg struct { val rune } |
---|
19 | type StateMsg struct {} |
---|
20 | |
---|
21 | var statemsg StateMsg |
---|
22 | |
---|
23 | type Client struct { |
---|
24 | servers [] * actor.PID; |
---|
25 | intmsg [] IntMsg; |
---|
26 | charmsg [] CharMsg; |
---|
27 | results, times uint64; |
---|
28 | } |
---|
29 | type Server struct {} |
---|
30 | |
---|
31 | var system * actor.ActorSystem |
---|
32 | var client * actor.PID |
---|
33 | |
---|
34 | func ( state * Server ) Receive( context actor.Context ) { |
---|
35 | switch msg := context.Message().(type) { |
---|
36 | case * IntMsg: |
---|
37 | msg.val = 7 |
---|
38 | system.Root.Send( client, msg ) |
---|
39 | case * CharMsg: |
---|
40 | msg.val = 'x' |
---|
41 | system.Root.Send( client, msg ) |
---|
42 | case * StateMsg: |
---|
43 | if ( atomic.AddUint64( &actorCnt, 1 ) == Messages + 1 ) { |
---|
44 | shake <- "hand" |
---|
45 | } // if |
---|
46 | default: |
---|
47 | // ignore actor.Started message |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | func process( state * Client, context * actor.Context ) { |
---|
52 | state.results++ |
---|
53 | if ( state.results == 2 * Messages ) { |
---|
54 | state.times += 1 |
---|
55 | if ( state.times == Times ) { |
---|
56 | for i := uint64(0); i < Messages; i += 1 { |
---|
57 | system.Root.Send( state.servers[i], &statemsg ) |
---|
58 | } |
---|
59 | if ( atomic.AddUint64( &actorCnt, 1 ) == Messages + 1 ) { |
---|
60 | shake <- "hand" |
---|
61 | } // if |
---|
62 | return |
---|
63 | } |
---|
64 | state.results = 0 |
---|
65 | system.Root.Send( (*context).Self(), &statemsg ) |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | func ( state * Client ) Receive( context actor.Context ) { |
---|
71 | switch context.Message().(type) { |
---|
72 | case * IntMsg: |
---|
73 | process( state, &context ) |
---|
74 | case * CharMsg: |
---|
75 | process( state, &context ) |
---|
76 | case * StateMsg: |
---|
77 | for i := uint64(0); i < Messages; i += 1 { |
---|
78 | system.Root.Send( state.servers[i], &state.intmsg[i] ) |
---|
79 | system.Root.Send( state.servers[i], &state.charmsg[i] ) |
---|
80 | } |
---|
81 | default: |
---|
82 | // ignore actor.Started message |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | func usage() { |
---|
87 | fmt.Printf( "Usage: %v " + |
---|
88 | "[ messages (> 0) | 'd' (default %v) ] " + |
---|
89 | "[ processors (> 0) | 'd' (default %v) ] " + |
---|
90 | "[ Times (> 0) | 'd' (default %v) ]\n", |
---|
91 | os.Args[0], Messages, Processors, Times ); |
---|
92 | os.Exit( 1 ); |
---|
93 | } |
---|
94 | |
---|
95 | func main() { |
---|
96 | switch len( os.Args ) { |
---|
97 | case 4: |
---|
98 | if os.Args[3] != "d" { // default ? |
---|
99 | Times, _ = strconv.ParseUint( os.Args[3], 10, 64 ) |
---|
100 | if Times < 1 { usage(); } |
---|
101 | } // if |
---|
102 | fallthrough |
---|
103 | case 3: |
---|
104 | if os.Args[2] != "d" { // default ? |
---|
105 | Processors, _ = strconv.Atoi( os.Args[2] ) |
---|
106 | if Processors < 1 { usage(); } |
---|
107 | } // if |
---|
108 | fallthrough |
---|
109 | case 2: |
---|
110 | if os.Args[1] != "d" { // default ? |
---|
111 | Messages, _ = strconv.ParseUint( os.Args[1], 10, 64 ) |
---|
112 | if Messages < 1 { usage(); } |
---|
113 | } // if |
---|
114 | case 1: // use defaults |
---|
115 | default: |
---|
116 | usage(); |
---|
117 | } // switch |
---|
118 | |
---|
119 | runtime.GOMAXPROCS( Processors ); |
---|
120 | |
---|
121 | starttime = time.Now(); |
---|
122 | |
---|
123 | system = actor.NewActorSystem(); |
---|
124 | servers := make( [] * actor.PID, Messages ); |
---|
125 | intmsg := make( []IntMsg, Messages ); |
---|
126 | charmsg := make( []CharMsg, Messages ); |
---|
127 | |
---|
128 | props := actor.PropsFromProducer( func() actor.Actor { return &Client{ servers, intmsg, charmsg, 0, 0 } }) |
---|
129 | client = system.Root.Spawn(props) |
---|
130 | |
---|
131 | for r := uint64(0); r < Messages; r += 1 { // create messages and actors |
---|
132 | props := actor.PropsFromProducer( func() actor.Actor { return &Server{} }) |
---|
133 | servers[r] = system.Root.Spawn(props) |
---|
134 | } // for |
---|
135 | |
---|
136 | system.Root.Send( client, &statemsg ); |
---|
137 | |
---|
138 | <- shake // wait for actors to finish |
---|
139 | |
---|
140 | fmt.Printf( "%.2f\n", time.Since( starttime ).Seconds() ); |
---|
141 | } // main |
---|
142 | |
---|
143 | // /usr/bin/time -f "%Uu %Ss %Er %Mkb" GoMatrix |
---|
144 | |
---|
145 | // Local Variables: // |
---|
146 | // tab-width: 4 // |
---|
147 | // compile-command: "go build" // |
---|
148 | // End: // |
---|