docs: change code blocks from "shell" to "console"
[vpp.git] / src / vpp-api / java / jvpp-core / io / fd / vpp / jvpp / core / examples / CallbackApiWritePerfTest.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.dto.*;
25 import io.fd.vpp.jvpp.core.callback.ClassifyAddDelTableReplyCallback;
26
27 import java.util.logging.Logger;
28
29 public class CallbackApiWritePerfTest {
30
31     private static final Logger LOG = Logger.getLogger(CallbackApiWritePerfTest.class.getName());
32     private static final ClassifyAddDelTable REQUEST = createAddDelTable();
33
34     private static ClassifyAddDelTable createAddDelTable () {
35         ClassifyAddDelTable addDelTable = new ClassifyAddDelTable();
36         addDelTable.isAdd = 1;
37         addDelTable.tableIndex = -1;
38         addDelTable.nbuckets = 2;
39         addDelTable.memorySize = 2 << 20;
40         addDelTable.nextTableIndex = ~0; // default
41         addDelTable.missNextIndex = ~0; // default
42         addDelTable.skipNVectors = 0;
43         addDelTable.matchNVectors = 1;
44         addDelTable.mask =
45                 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
46                         (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
47         return addDelTable;
48     };
49
50     /**
51      *
52      * @param args - for running for one sec requires no parameter
53      *             - for running for set amount of requests requires one parameters, desired REQUEST amount
54      * @throws Exception if arguments aren't String representations of numbers
55      */
56     public static void main(String[] args) throws Exception {
57         if (args.length != 0) {
58             testInvokeCounter(true, Integer.parseUnsignedInt(args[0]));
59         } else {
60             testInvokeCounter(false, 0);
61         }
62     }
63
64     /**
65      *
66      * @param setCount true = run with set amount of requests, false = run for 1 sec
67      * @param count number of requests with which test should be run
68      * @throws Exception
69      */
70     private static void testInvokeCounter(boolean setCount, int count) throws Exception {
71         LOG.info("Testing callback API Invocation Counter");
72         try (final JVppRegistry registry = new JVppRegistryImpl("CallbackApiWritePerfTest");
73              final JVpp jvpp = new JVppCoreImpl()) {
74             TestCallback callback = new TestCallback(count);
75             registry.register(jvpp, callback);
76             if (!setCount) {
77                 for(int i = 0; i < 5; i++) {
78                     callback.reset();
79                     LOG.info("Starting invocation for 1sec");
80                     long time = System.nanoTime();
81                     do {
82                         jvpp.send(REQUEST);
83                     } while (System.nanoTime() - time < 1000000000 || callback.stop());
84                     int replyCount = callback.getReplyCounter();
85                     LOG.info(String.format("Invocation count within 1 second: %d", replyCount));
86                 }
87             } else {
88                 for(int i = 0; i < 5; i++) {
89                     LOG.info("Starting invocations");
90                     callback.reset();
91                     long time = System.nanoTime();
92                     for (int x = 1; x <= count; x++) {
93                         jvpp.send(REQUEST);
94                     }
95                     long timeAfter = callback.getTime();
96                     LOG.info(String.format("Invocations took %d ns (%f invocations/s)", timeAfter - time,
97                             count * (1000000000.0 / (timeAfter - time))));
98                 }
99             }
100
101
102             Thread.sleep(1000);
103             LOG.info("Disconnecting...");
104         }
105         Thread.sleep(1000);
106     }
107
108     static class TestCallback implements ClassifyAddDelTableReplyCallback {
109
110         private int replyCounter = 0;
111         private int count;
112         private long time = 0;
113         private boolean stop = false;
114
115         public TestCallback(int count) throws Exception {
116             this.count = count;
117         }
118
119         public int getReplyCounter() {
120             return replyCounter;
121         }
122
123         public void reset() {
124             replyCounter = 0;
125             time = 0;
126             stop = false;
127         }
128
129         public boolean stop() {
130             this.stop = true;
131             return false;
132         }
133
134         /* actual method called from VPP
135            not thread safe but since there's only one VPP thread listening for requests and calling
136            this method it's OK
137          */
138         @Override
139         public void onClassifyAddDelTableReply(final ClassifyAddDelTableReply msg) {
140             if (stop) {
141                 return;
142             }
143             replyCounter++;
144             if (replyCounter == count ) {
145                 time = System.nanoTime();
146             }
147         }
148
149         @Override
150         public void onError(VppCallbackException ex) {
151             System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(),
152                     ex.getCtxId(), ex.getErrorCode());
153         }
154
155         public long getTime() throws Exception {
156             while(time == 0) {
157                 Thread.sleep(1000);
158             }
159             return time;
160         }
161     }
162 }