github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/remotebenchmark/node1/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"runtime/pprof"
     7  
     8  	"github.com/AsynkronIT/protoactor-go/actor"
     9  	"github.com/AsynkronIT/protoactor-go/examples/remotebenchmark/messages"
    10  	"github.com/AsynkronIT/protoactor-go/remote"
    11  
    12  	"log"
    13  	"sync"
    14  
    15  	"runtime"
    16  	"time"
    17  
    18  	"github.com/AsynkronIT/protoactor-go/mailbox"
    19  )
    20  
    21  type localActor struct {
    22  	count        int
    23  	wgStop       *sync.WaitGroup
    24  	messageCount int
    25  }
    26  
    27  func (state *localActor) Receive(context actor.Context) {
    28  	switch context.Message().(type) {
    29  	case *messages.Pong:
    30  		state.count++
    31  		if state.count%50000 == 0 {
    32  			log.Println(state.count)
    33  		}
    34  		if state.count == state.messageCount {
    35  			state.wgStop.Done()
    36  		}
    37  	}
    38  }
    39  
    40  func newLocalActor(stop *sync.WaitGroup, messageCount int) actor.Producer {
    41  	return func() actor.Actor {
    42  		return &localActor{
    43  			wgStop:       stop,
    44  			messageCount: messageCount,
    45  		}
    46  	}
    47  }
    48  
    49  var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
    50  var blockProfile = flag.String("blockprof", "", "execute contention profiling and save results here")
    51  
    52  func main() {
    53  	flag.Parse()
    54  	if *cpuprofile != "" {
    55  		f, err := os.Create(*cpuprofile)
    56  		if err != nil {
    57  			log.Fatal(err)
    58  		}
    59  		pprof.StartCPUProfile(f)
    60  		defer pprof.StopCPUProfile()
    61  	}
    62  
    63  	// Check for lock contention profiling
    64  	if *blockProfile != "" {
    65  		prof, err := os.Create(*blockProfile)
    66  		if err != nil {
    67  			log.Fatal(err)
    68  		}
    69  		runtime.SetBlockProfileRate(1)
    70  		defer func() {
    71  			pprof.Lookup("block").WriteTo(prof, 0)
    72  		}()
    73  	}
    74  
    75  	runtime.GOMAXPROCS(runtime.NumCPU() * 1)
    76  	runtime.GC()
    77  
    78  	var wg sync.WaitGroup
    79  
    80  	messageCount := 1000000
    81  	// remote.DefaultSerializerID = 1
    82  	remote.Start("127.0.0.1:8081")
    83  
    84  	rootContext := actor.EmptyRootContext
    85  	props := actor.
    86  		PropsFromProducer(newLocalActor(&wg, messageCount)).
    87  		WithMailbox(mailbox.Bounded(1000000))
    88  
    89  	pid := rootContext.Spawn(props)
    90  
    91  	remotePid := actor.NewPID("127.0.0.1:8080", "remote")
    92  	_ = rootContext.RequestFuture(remotePid, &messages.StartRemote{
    93  		Sender: pid,
    94  	}, 5*time.Second).
    95  		Wait()
    96  
    97  	wg.Add(1)
    98  
    99  	start := time.Now()
   100  	log.Println("Starting to send")
   101  
   102  	message := &messages.Ping{}
   103  	for i := 0; i < messageCount; i++ {
   104  		rootContext.Send(remotePid, message)
   105  	}
   106  
   107  	wg.Wait()
   108  	elapsed := time.Since(start)
   109  	log.Printf("Elapsed %s", elapsed)
   110  
   111  	x := int(float32(messageCount*2) / (float32(elapsed) / float32(time.Second)))
   112  	log.Printf("Msg per sec %v", x)
   113  
   114  	// f, err := os.Create("memprofile")
   115  	// if err != nil {
   116  	// 	log.Fatal(err)
   117  	// }
   118  	// pprof.WriteHeapProfile(f)
   119  	// f.Close()
   120  }