6ff440dccc0c2052f96051218415591c95948521
[vpp.git] / src / vpp-api / java / jvpp-core / io / fd / vpp / jvpp / core / examples / CallbackApiReadPerfTest.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.vpp.jvpp.core.examples;
18
19 import io.fd.vpp.jvpp.JVpp;
20 import io.fd.vpp.jvpp.JVppRegistry;
21 import io.fd.vpp.jvpp.JVppRegistryImpl;
22 import io.fd.vpp.jvpp.VppCallbackException;
23 import io.fd.vpp.jvpp.core.JVppCoreImpl;
24 import io.fd.vpp.jvpp.core.callback.ShowVersionReplyCallback;
25 import io.fd.vpp.jvpp.core.dto.*;
26
27 import java.util.logging.Logger;
28
29 public class CallbackApiReadPerfTest {
30
31     private static final Logger LOG = Logger.getLogger(CallbackApiReadPerfTest.class.getName());
32     private static final ShowVersion REQUEST = new ShowVersion();
33
34     /**
35      *
36      * @param args - for running for one sec requires no parameter
37      *             - for running for set amount of requests requires one parameters, desired REQUEST amount
38      * @throws Exception if arguments aren't String representations of numbers
39      */
40     public static void main(String[] args) throws Exception {
41         if (args.length != 0) {
42             testInvokeCounter(true, Integer.parseUnsignedInt(args[0]));
43         } else {
44             testInvokeCounter(false, 0);
45         }
46     }
47
48     /**
49      *
50      * @param setCount true = run with set amount of requests, false = run for 1 sec
51      * @param count number of request with which test should be run
52      * @throws Exception
53      */
54     private static void testInvokeCounter(boolean setCount, int count) throws Exception {
55         LOG.info("Testing callback API Invocation Counter");
56         try (final JVppRegistry registry = new JVppRegistryImpl("CallbackApiReadPerfTest");
57              final JVpp jvpp = new JVppCoreImpl()) {
58             TestCallback callback = new TestCallback(count);
59             registry.register(jvpp, callback);
60             if (!setCount) {
61                 for(int i = 0; i < 5; i++) {
62                     callback.reset();
63                     LOG.info("Starting invocation for 1sec");
64                     long time = System.nanoTime();
65                     do {
66                         jvpp.send(REQUEST);
67                     } while (System.nanoTime() - time < 1000000000 || callback.stop());
68                     int replyCount =  callback.getReplyCounter();
69                     LOG.info(String.format("Invocation count within 1 second: %d", replyCount));
70                 }
71             } else {
72                 for (int i = 0; i < 5; i++) {
73                     LOG.info("Starting invocations");
74                     callback.reset();
75                     long time = System.nanoTime();
76                     for (int x = 0; x < count; x++) {
77                         jvpp.send(REQUEST);
78                     }
79                     long timeAfter = callback.getTime();
80                     LOG.info(String.format("Invocations took %d ns (%f invocations/s)", timeAfter - time,
81                             count * (1000000000.0/(timeAfter - time))));
82                 }
83             }
84
85
86             Thread.sleep(1000);
87             LOG.info("Disconnecting...");
88         }
89         Thread.sleep(1000);
90     }
91
92     static class TestCallback implements ShowVersionReplyCallback {
93
94         private int replyCounter = 0;
95         private int count;
96         private long time = 0;
97         private boolean stop = false;
98
99         public TestCallback(int count) throws Exception {
100             this.count = count;
101         }
102
103         public int getReplyCounter() {
104             return replyCounter;
105         }
106
107         public void reset() {
108             replyCounter = 0;
109             time = 0;
110             stop = false;
111         }
112
113         public boolean stop() {
114             this.stop = true;
115             return false;
116         }
117
118         /* actual method called from VPP
119            not thread safe but since there's only one VPP thread listening for requests and calling
120            this method it's OK
121          */
122         @Override
123         public void onShowVersionReply(final ShowVersionReply msg) {
124             if (stop) {
125                 return;
126             }
127             replyCounter++;
128             if (replyCounter == count ) {
129                 time = System.nanoTime();
130             }
131         }
132
133         @Override
134         public void onError(VppCallbackException ex) {
135             System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
136                 ex.getCtxId(), ex.getErrorCode());
137         }
138
139         public long getTime() throws Exception {
140             while(time == 0) {
141                 Thread.sleep(1000);
142             }
143             return time;
144         }
145     }
146 }