Skip to content

Instantly share code, notes, and snippets.

@SkinyMonkey
Last active August 9, 2021 05:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SkinyMonkey/d15514ec05be86c9e03dca678eec8e78 to your computer and use it in GitHub Desktop.
Save SkinyMonkey/d15514ec05be86c9e03dca678eec8e78 to your computer and use it in GitHub Desktop.
protoactor linked list benchmark
package main
import (
"fmt"
"runtime"
"sync"
"time"
"github.com/AsynkronIT/protoactor-go/actor"
)
type Test struct {
next *actor.PID
wg *sync.WaitGroup
}
type OutputMsg struct {
}
type ForwardMsg struct {
Content int
StartTime time.Time
}
func (t *Test) Receive(ctx actor.Context) {
switch msg := ctx.Message().(type) {
case *OutputMsg:
t.next = ctx.Sender()
case *ForwardMsg:
if t.next != nil {
msg.Content += 1
ctx.Send(t.next, msg)
} else {
fmt.Println("forwarding done : ", time.Since(msg.StartTime))
}
t.wg.Done()
}
}
func newTest(wg *sync.WaitGroup) actor.Producer {
return func() actor.Actor {
return &Test{
wg: wg,
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
runtime.GC()
rootContext := actor.EmptyRootContext
ts := []*actor.PID{}
before := time.Now()
wg := &sync.WaitGroup{}
props := actor.PropsFromProducer(newTest(wg))
var p *actor.PID
for i := 0; i < 1000000; i++ {
wg.Add(1)
t := rootContext.
Spawn(props)
ts = append(ts, t)
// linked list
if p != nil && i > 0 {
o := &OutputMsg{}
p.Request(o, t)
}
p = t
}
fmt.Println("spawn done : ", time.Since(before))
ts[0].Request(&ForwardMsg{Content: 0, StartTime: time.Now()}, nil)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment