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"
29 "git.fd.io/govpp.git/adapter/socketclient"
30 "git.fd.io/govpp.git/adapter/vppapiclient"
31 "git.fd.io/govpp.git/api"
32 "git.fd.io/govpp.git/core"
33 "git.fd.io/govpp.git/examples/bin_api/vpe"
37 defaultSyncRequestCount = 1000
38 defaultAsyncRequestCount = 10000
42 // parse optional flags
43 var sync, prof, sock bool
45 flag.BoolVar(&sync, "sync", false, "run synchronous perf test")
46 flag.BoolVar(&sock, "sock", false, "use socket client for VPP API")
47 flag.IntVar(&cnt, "count", 0, "count of requests to be sent to VPP")
48 flag.BoolVar(&prof, "prof", false, "generate profile data")
52 // no specific count defined - use defaults
54 cnt = defaultSyncRequestCount
56 cnt = defaultAsyncRequestCount
61 defer profile.Start().Stop()
66 a = socketclient.NewVppClient("/run/vpp-api.sock")
68 a = vppapiclient.NewVppClient("")
72 conn, err := core.Connect(a)
74 log.Fatalln("Error:", err)
76 defer conn.Disconnect()
78 // create an API channel
79 ch, err := conn.NewAPIChannelBuffered(cnt, cnt)
81 log.Fatalln("Error:", err)
85 ch.SetReplyTimeout(time.Second * 2)
88 core.SetLogger(&logrus.Logger{Level: logrus.ErrorLevel})
90 // run the test & measure the time
94 // run synchronous test
97 // run asynchronous test
101 elapsed := time.Since(start)
102 fmt.Println("Test took:", elapsed)
103 fmt.Printf("Requests per second: %.0f\n", float64(cnt)/elapsed.Seconds())
105 time.Sleep(time.Second)
108 func syncTest(ch api.Channel, cnt int) {
109 fmt.Printf("Running synchronous perf test with %d requests...\n", cnt)
111 for i := 0; i < cnt; i++ {
112 req := &vpe.ControlPing{}
113 reply := &vpe.ControlPingReply{}
115 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
116 log.Fatalln("Error in reply:", err)
121 func asyncTest(ch api.Channel, cnt int) {
122 fmt.Printf("Running asynchronous perf test with %d requests...\n", cnt)
124 ctxChan := make(chan api.RequestCtx, cnt)
127 for i := 0; i < cnt; i++ {
128 ctxChan <- ch.SendRequest(&vpe.ControlPing{})
131 fmt.Printf("Sending asynchronous requests finished\n")
134 for ctx := range ctxChan {
135 reply := &vpe.ControlPingReply{}
136 if err := ctx.ReceiveReply(reply); err != nil {
137 log.Fatalln("Error in reply:", err)