Generator improvements and cleanup
[govpp.git] / examples / perf-bench / perf-bench.go
1 // Copyright (c) 2017 Cisco and/or its affiliates.
2 //
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:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 // Binary simple-client is an example VPP management application that exercises the
16 // govpp API on real-world use-cases.
17 package main
18
19 import (
20         "flag"
21         "fmt"
22         "log"
23         "time"
24
25         "github.com/pkg/profile"
26         "github.com/sirupsen/logrus"
27
28         "git.fd.io/govpp.git"
29         "git.fd.io/govpp.git/api"
30         "git.fd.io/govpp.git/core"
31         "git.fd.io/govpp.git/examples/bin_api/vpe"
32 )
33
34 const (
35         defaultSyncRequestCount  = 1000
36         defaultAsyncRequestCount = 10000
37 )
38
39 func main() {
40         // parse optional flags
41         var sync, prof bool
42         var cnt int
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")
46         flag.Parse()
47
48         if cnt == 0 {
49                 // no specific count defined - use defaults
50                 if sync {
51                         cnt = defaultSyncRequestCount
52                 } else {
53                         cnt = defaultAsyncRequestCount
54                 }
55         }
56
57         if prof {
58                 defer profile.Start().Stop()
59         }
60
61         // connect to VPP
62         conn, err := govpp.Connect("")
63         if err != nil {
64                 log.Fatalln("Error:", err)
65         }
66         defer conn.Disconnect()
67
68         // create an API channel
69         ch, err := conn.NewAPIChannelBuffered(cnt, cnt)
70         if err != nil {
71                 log.Fatalln("Error:", err)
72         }
73         defer ch.Close()
74
75         // log only errors
76         core.SetLogger(&logrus.Logger{Level: logrus.ErrorLevel})
77
78         // run the test & measure the time
79         start := time.Now()
80
81         if sync {
82                 // run synchronous test
83                 syncTest(ch, cnt)
84         } else {
85                 // run asynchronous test
86                 asyncTest(ch, cnt)
87         }
88
89         elapsed := time.Since(start)
90         fmt.Println("Test took:", elapsed)
91         fmt.Printf("Requests per second: %.0f\n", float64(cnt)/elapsed.Seconds())
92 }
93
94 func syncTest(ch api.Channel, cnt int) {
95         fmt.Printf("Running synchronous perf test with %d requests...\n", cnt)
96
97         for i := 0; i < cnt; i++ {
98                 req := &vpe.ControlPing{}
99                 reply := &vpe.ControlPingReply{}
100
101                 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
102                         log.Fatalln("Error in reply:", err)
103                 }
104         }
105 }
106
107 func asyncTest(ch api.Channel, cnt int) {
108         fmt.Printf("Running asynchronous perf test with %d requests...\n", cnt)
109
110         ctxChan := make(chan api.RequestCtx, cnt)
111
112         go func() {
113                 for i := 0; i < cnt; i++ {
114                         ctxChan <- ch.SendRequest(&vpe.ControlPing{})
115                 }
116                 close(ctxChan)
117                 fmt.Printf("Sending asynchronous requests finished\n")
118         }()
119
120         for ctx := range ctxChan {
121                 reply := &vpe.ControlPingReply{}
122                 if err := ctx.ReceiveReply(reply); err != nil {
123                         log.Fatalln("Error in reply:", err)
124                 }
125         }
126 }