Update generated binapi to v22.02 & makefile changes
[govpp.git] / examples / perf-bench / perf-bench.go
index f48c154..f7a9857 100644 (file)
 package main
 
 import (
+       "context"
        "flag"
        "fmt"
        "log"
+       "os"
        "time"
 
        "github.com/pkg/profile"
@@ -28,8 +30,8 @@ import (
        "git.fd.io/govpp.git/adapter/socketclient"
        "git.fd.io/govpp.git/adapter/statsclient"
        "git.fd.io/govpp.git/api"
+       "git.fd.io/govpp.git/binapi/memclnt"
        "git.fd.io/govpp.git/core"
-       "git.fd.io/govpp.git/examples/binapi/vpe"
 )
 
 const (
@@ -39,14 +41,14 @@ const (
 
 func main() {
        // parse optional flags
-       var sync, prof bool
+       var sync bool
        var cnt int
-       var sock string
+       var sock, prof string
        flag.BoolVar(&sync, "sync", false, "run synchronous perf test")
-       flag.StringVar(&sock, "socket", socketclient.DefaultSocketName, "Path to VPP API socket")
-       flag.String("socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
+       flag.StringVar(&sock, "api-socket", socketclient.DefaultSocketName, "Path to VPP API socket")
+       flag.String("stats-socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
        flag.IntVar(&cnt, "count", 0, "count of requests to be sent to VPP")
-       flag.BoolVar(&prof, "prof", false, "generate profile data")
+       flag.StringVar(&prof, "prof", "", "enable profiling mode [mem, cpu]")
        flag.Parse()
 
        if cnt == 0 {
@@ -58,8 +60,16 @@ func main() {
                }
        }
 
-       if prof {
-               defer profile.Start().Stop()
+       switch prof {
+       case "mem":
+               defer profile.Start(profile.MemProfile, profile.MemProfileRate(1)).Stop()
+       case "cpu":
+               defer profile.Start(profile.CPUProfile).Stop()
+       case "":
+       default:
+               fmt.Printf("invalid profiling mode: %q\n", prof)
+               flag.Usage()
+               os.Exit(1)
        }
 
        a := socketclient.NewVppClient(sock)
@@ -89,9 +99,11 @@ func main() {
        if sync {
                // run synchronous test
                syncTest(ch, cnt)
+               //syncTest2(conn, cnt)
        } else {
                // run asynchronous test
                asyncTest(ch, cnt)
+               //asyncTest2(conn, cnt)
        }
 
        elapsed := time.Since(start)
@@ -105,8 +117,8 @@ func syncTest(ch api.Channel, cnt int) {
        fmt.Printf("Running synchronous perf test with %d requests...\n", cnt)
 
        for i := 0; i < cnt; i++ {
-               req := &vpe.ControlPing{}
-               reply := &vpe.ControlPingReply{}
+               req := &memclnt.ControlPing{}
+               reply := &memclnt.ControlPingReply{}
 
                if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
                        log.Fatalln("Error in reply:", err)
@@ -114,6 +126,27 @@ func syncTest(ch api.Channel, cnt int) {
        }
 }
 
+func syncTest2(conn api.Connection, cnt int) {
+       fmt.Printf("Running synchronous perf test with %d requests...\n", cnt)
+
+       stream, err := conn.NewStream(context.Background())
+       if err != nil {
+               log.Fatalln("Error NewStream:", err)
+       }
+       for i := 0; i < cnt; i++ {
+               if err := stream.SendMsg(&memclnt.ControlPing{}); err != nil {
+                       log.Fatalln("Error SendMsg:", err)
+               }
+               if msg, err := stream.RecvMsg(); err != nil {
+                       log.Fatalln("Error RecvMsg:", err)
+               } else if _, ok := msg.(*memclnt.ControlPingReply); ok {
+                       // ok
+               } else {
+                       log.Fatalf("unexpected reply: %v", msg.GetMessageName())
+               }
+       }
+}
+
 func asyncTest(ch api.Channel, cnt int) {
        fmt.Printf("Running asynchronous perf test with %d requests...\n", cnt)
 
@@ -121,16 +154,48 @@ func asyncTest(ch api.Channel, cnt int) {
 
        go func() {
                for i := 0; i < cnt; i++ {
-                       ctxChan <- ch.SendRequest(&vpe.ControlPing{})
+                       ctxChan <- ch.SendRequest(&memclnt.ControlPing{})
                }
                close(ctxChan)
                fmt.Printf("Sending asynchronous requests finished\n")
        }()
 
        for ctx := range ctxChan {
-               reply := &vpe.ControlPingReply{}
+               reply := &memclnt.ControlPingReply{}
                if err := ctx.ReceiveReply(reply); err != nil {
                        log.Fatalln("Error in reply:", err)
                }
        }
 }
+
+func asyncTest2(conn api.Connection, cnt int) {
+       fmt.Printf("Running asynchronous perf test with %d requests...\n", cnt)
+
+       ctxChan := make(chan api.Stream, cnt)
+
+       go func() {
+               for i := 0; i < cnt; i++ {
+                       stream, err := conn.NewStream(context.Background())
+                       if err != nil {
+                               log.Fatalln("Error NewStream:", err)
+                       }
+                       if err := stream.SendMsg(&memclnt.ControlPing{}); err != nil {
+                               log.Fatalln("Error SendMsg:", err)
+                       }
+                       ctxChan <- stream
+               }
+               close(ctxChan)
+               fmt.Printf("Sending asynchronous requests finished\n")
+       }()
+
+       for ctx := range ctxChan {
+               if msg, err := ctx.RecvMsg(); err != nil {
+                       log.Fatalln("Error RecvMsg:", err)
+               } else if _, ok := msg.(*memclnt.ControlPingReply); ok {
+                       // ok
+               } else {
+                       log.Fatalf("unexpected reply: %v", msg.GetMessageName())
+               }
+               ctx.Close()
+       }
+}