1 // Copyright (c) 2017 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // Binary simple-client is an example VPP management application that exercises the
16 // govpp API on real-world use-cases.
25 "github.com/pkg/profile"
26 "github.com/sirupsen/logrus"
28 "git.fd.io/govpp.git/adapter/socketclient"
29 "git.fd.io/govpp.git/adapter/statsclient"
30 "git.fd.io/govpp.git/api"
31 "git.fd.io/govpp.git/core"
32 "git.fd.io/govpp.git/examples/binapi/vpe"
36 defaultSyncRequestCount = 1000
37 defaultAsyncRequestCount = 10000
41 // parse optional flags
45 flag.BoolVar(&sync, "sync", false, "run synchronous perf test")
46 flag.StringVar(&sock, "socket", socketclient.DefaultSocketName, "Path to VPP API socket")
47 flag.String("socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
48 flag.IntVar(&cnt, "count", 0, "count of requests to be sent to VPP")
49 flag.BoolVar(&prof, "prof", false, "generate profile data")
53 // no specific count defined - use defaults
55 cnt = defaultSyncRequestCount
57 cnt = defaultAsyncRequestCount
62 defer profile.Start().Stop()
65 a := socketclient.NewVppClient(sock)
68 conn, err := core.Connect(a)
70 log.Fatalln("Error:", err)
72 defer conn.Disconnect()
74 // create an API channel
75 ch, err := conn.NewAPIChannelBuffered(cnt, cnt)
77 log.Fatalln("Error:", err)
81 ch.SetReplyTimeout(time.Second * 2)
84 core.SetLogger(&logrus.Logger{Level: logrus.ErrorLevel})
86 // run the test & measure the time
90 // run synchronous test
93 // run asynchronous test
97 elapsed := time.Since(start)
98 fmt.Println("Test took:", elapsed)
99 fmt.Printf("Requests per second: %.0f\n", float64(cnt)/elapsed.Seconds())
101 time.Sleep(time.Second)
104 func syncTest(ch api.Channel, cnt int) {
105 fmt.Printf("Running synchronous perf test with %d requests...\n", cnt)
107 for i := 0; i < cnt; i++ {
108 req := &vpe.ControlPing{}
109 reply := &vpe.ControlPingReply{}
111 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
112 log.Fatalln("Error in reply:", err)
117 func asyncTest(ch api.Channel, cnt int) {
118 fmt.Printf("Running asynchronous perf test with %d requests...\n", cnt)
120 ctxChan := make(chan api.RequestCtx, cnt)
123 for i := 0; i < cnt; i++ {
124 ctxChan <- ch.SendRequest(&vpe.ControlPing{})
127 fmt.Printf("Sending asynchronous requests finished\n")
130 for ctx := range ctxChan {
131 reply := &vpe.ControlPingReply{}
132 if err := ctx.ReceiveReply(reply); err != nil {
133 log.Fatalln("Error in reply:", err)