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" |
---|
7 | "github.com/asynkron/protoactor-go/actor" |
---|
8 | ) |
---|
9 | |
---|
10 | var Times int = 2_000_000; |
---|
11 | var start time.Time; |
---|
12 | var shake = make( chan string ) |
---|
13 | var system * actor.ActorSystem |
---|
14 | |
---|
15 | type Msg struct { cnt int } |
---|
16 | type Send struct{} |
---|
17 | |
---|
18 | func ( state * Send ) Receive( context actor.Context ) { |
---|
19 | switch msg := context.Message().(type) { |
---|
20 | case * Msg: |
---|
21 | if msg.cnt >= Times { |
---|
22 | fmt.Printf( "%v\n", int64(time.Since(start) / time.Duration(Times) / time.Nanosecond) ); |
---|
23 | shake <- "hand" |
---|
24 | return; |
---|
25 | } // if |
---|
26 | //fmt.Printf( "Send %v\n", msg.cnt ); |
---|
27 | props := actor.PropsFromProducer( func() actor.Actor { return &Send{} } ) |
---|
28 | pid := system.Root.Spawn( props ) |
---|
29 | system.Root.Send( pid, &Msg{ msg.cnt + 1 } ); |
---|
30 | system.Root.Stop( context.Self() ); |
---|
31 | default: |
---|
32 | // ignore actor.Started message |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | func usage() { |
---|
37 | fmt.Printf( "Usage: %v [ times (> 0) ]\n", os.Args[0] ); |
---|
38 | os.Exit( 1 ); |
---|
39 | } |
---|
40 | |
---|
41 | func main() { |
---|
42 | switch len( os.Args ) { |
---|
43 | case 2: |
---|
44 | if os.Args[1] != "d" { Times, _ = strconv.Atoi( os.Args[1] ) } |
---|
45 | if Times < 1 { usage(); } |
---|
46 | case 1: // use defaults |
---|
47 | default: |
---|
48 | usage(); |
---|
49 | } // switch |
---|
50 | |
---|
51 | runtime.GOMAXPROCS(1); |
---|
52 | system = actor.NewActorSystem(); |
---|
53 | props := actor.PropsFromProducer( func() actor.Actor { return &Send{} } ); |
---|
54 | pid := system.Root.Spawn(props); |
---|
55 | start = time.Now(); |
---|
56 | system.Root.Send( pid, &Msg{ 0 } ); |
---|
57 | <- shake |
---|
58 | } |
---|
59 | |
---|
60 | // /usr/bin/time -f "%Uu %Ss %Er %Mkb" GoSendDynamic |
---|
61 | |
---|
62 | // Local Variables: // |
---|
63 | // tab-width: 4 // |
---|
64 | // compile-command: "go build" // |
---|
65 | // End: // |
---|