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"
29 "git.fd.io/govpp.git/api"
30 "git.fd.io/govpp.git/core"
31 "git.fd.io/govpp.git/examples/bin_api/vpe"
35 defaultSyncRequestCount = 1000
36 defaultAsyncRequestCount = 10000
40 // parse optional flags
43 flag.BoolVar(&sync, "sync", false, "run synchronous perf test")
44 flag.IntVar(&cnt, "count", 0, "count of requests to be sent to VPP")
45 flag.BoolVar(&prof, "prof", false, "generate profile data")
49 // no specific count defined - use defaults
51 cnt = defaultSyncRequestCount
53 cnt = defaultAsyncRequestCount
58 defer profile.Start().Stop()
62 conn, err := govpp.Connect("")
64 log.Fatalln("Error:", err)
66 defer conn.Disconnect()
68 // create an API channel
69 ch, err := conn.NewAPIChannelBuffered(cnt, cnt)
71 log.Fatalln("Error:", err)
76 core.SetLogger(&logrus.Logger{Level: logrus.ErrorLevel})
78 // run the test & measure the time
82 // run synchronous test
85 // run asynchronous test
89 elapsed := time.Since(start)
90 fmt.Println("Test took:", elapsed)
91 fmt.Printf("Requests per second: %.0f\n", float64(cnt)/elapsed.Seconds())
94 func syncTest(ch api.Channel, cnt int) {
95 fmt.Printf("Running synchronous perf test with %d requests...\n", cnt)
97 for i := 0; i < cnt; i++ {
98 req := &vpe.ControlPing{}
99 reply := &vpe.ControlPingReply{}
101 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
102 log.Fatalln("Error in reply:", err)
107 func asyncTest(ch api.Channel, cnt int) {
108 fmt.Printf("Running asynchronous perf test with %d requests...\n", cnt)
110 ctxChan := make(chan api.RequestCtx, cnt)
113 for i := 0; i < cnt; i++ {
114 ctxChan <- ch.SendRequest(&vpe.ControlPing{})
117 fmt.Printf("Sending asynchronous requests finished\n")
120 for ctx := range ctxChan {
121 reply := &vpe.ControlPingReply{}
122 if err := ctx.ReceiveReply(reply); err != nil {
123 log.Fatalln("Error in reply:", err)