1 | // https://github.com/asynkron/protoactor-go |
---|
2 | // https://pkg.go.dev/github.com/asynkron/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 Actors, Set, Rounds, Processors int = 40_000, 100, 100, 1 // default values |
---|
11 | var Batch int = 1 |
---|
12 | var starttime time.Time; |
---|
13 | var shake = make( chan string ) |
---|
14 | var actorCnt int64 = 0 |
---|
15 | |
---|
16 | type Msg struct {} |
---|
17 | var msg Msg |
---|
18 | type Executor struct { |
---|
19 | actors [] * actor.PID; |
---|
20 | id, rounds, group, recs, sends int; |
---|
21 | } |
---|
22 | var system * actor.ActorSystem |
---|
23 | |
---|
24 | func ( state * Executor ) Receive( context actor.Context ) { |
---|
25 | switch context.Message().(type) { |
---|
26 | case * Msg: |
---|
27 | if state.recs == state.rounds { |
---|
28 | if ( atomic.AddInt64( &actorCnt, 1 ) == int64(Actors) ) { |
---|
29 | fmt.Printf( "%.2f\n", time.Since( starttime ).Seconds() ); |
---|
30 | shake <- "hand" |
---|
31 | } // if |
---|
32 | return; |
---|
33 | } // if |
---|
34 | if state.recs % Batch == 0 { |
---|
35 | for i := 0; i < Batch; i += 1 { |
---|
36 | system.Root.Send( state.actors[state.group + state.sends % Set], &msg ); // cycle through group |
---|
37 | state.sends += 1; |
---|
38 | } |
---|
39 | } |
---|
40 | state.recs += 1; |
---|
41 | //fmt.Printf( "%v %v %v %v %v\n", state.id, state.group, state.recs, state.sends, state.group + state.sends % Set ); |
---|
42 | default: |
---|
43 | // ignore actor.Started message |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | func usage() { |
---|
48 | fmt.Printf( "Usage: %v " + |
---|
49 | "[ actors (> 0 && > set && actors % set == 0 ) | 'd' (default %v ) ] " + |
---|
50 | "[ set (> 0) | 'd' (default %v) ] " + |
---|
51 | "[ rounds (> 0) | 'd' (default %v) ] " + |
---|
52 | "[ processors (> 0) | 'd' (default %v) ] " + |
---|
53 | "[ batch (> 0) | 'd' (default %v) ]\n", |
---|
54 | os.Args[0], Actors, Set, Rounds, Processors ); |
---|
55 | os.Exit( 1 ); |
---|
56 | } |
---|
57 | |
---|
58 | func main() { |
---|
59 | switch len( os.Args ) { |
---|
60 | case 6: |
---|
61 | if os.Args[5] != "d" { // default ? |
---|
62 | Batch, _ = strconv.Atoi( os.Args[5] ) |
---|
63 | if Batch < 1 { usage(); } |
---|
64 | } // if |
---|
65 | fallthrough |
---|
66 | case 5: |
---|
67 | if os.Args[4] != "d" { // default ? |
---|
68 | Processors, _ = strconv.Atoi( os.Args[4] ) |
---|
69 | if Processors < 1 { usage(); } |
---|
70 | } // if |
---|
71 | fallthrough |
---|
72 | case 4: |
---|
73 | if os.Args[3] != "d" { // default ? |
---|
74 | Rounds, _ = strconv.Atoi( os.Args[3] ) |
---|
75 | if Rounds < 1 { usage(); } |
---|
76 | } // if |
---|
77 | fallthrough |
---|
78 | case 3: |
---|
79 | if os.Args[2] != "d" { // default ? |
---|
80 | Set, _ = strconv.Atoi( os.Args[2] ) |
---|
81 | if Set < 1 { usage(); } |
---|
82 | } // if |
---|
83 | fallthrough |
---|
84 | case 2: |
---|
85 | if os.Args[1] != "d" { // default ? |
---|
86 | Actors, _ = strconv.Atoi( os.Args[1] ) |
---|
87 | if Actors < 1 || Actors <= Set || Actors % Set != 0 { usage(); } |
---|
88 | } // if |
---|
89 | case 1: // use defaults |
---|
90 | default: |
---|
91 | usage(); |
---|
92 | } // switch |
---|
93 | |
---|
94 | runtime.GOMAXPROCS( Processors ); |
---|
95 | system = actor.NewActorSystem(); |
---|
96 | |
---|
97 | actors := make( [] *actor.PID, Actors, Actors ); // create actors |
---|
98 | for id := 0; id < Actors; id += 1 { |
---|
99 | props := actor.PropsFromProducer( func() actor.Actor { |
---|
100 | return &Executor{ actors, id, Set * Rounds, id / Set * Set, 0, 0 } |
---|
101 | } ); |
---|
102 | actors[id] = system.Root.Spawn( props ); |
---|
103 | } // for |
---|
104 | starttime = time.Now(); |
---|
105 | for id := 0; id < Actors; id += 1 { // start actors |
---|
106 | system.Root.Send( actors[id], &msg ); |
---|
107 | } // for |
---|
108 | <- shake |
---|
109 | } // main |
---|
110 | |
---|
111 | // /usr/bin/time -f "%Uu %Ss %Er %Mkb" GoExecutor |
---|
112 | |
---|
113 | // Local Variables: // |
---|
114 | // tab-width: 4 // |
---|
115 | // compile-command: "go build" // |
---|
116 | // End: // |
---|