2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.vpp.jvpp.core.examples;
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;
27 import java.util.logging.Logger;
29 public class CallbackApiWritePerfTest {
31 private static final Logger LOG = Logger.getLogger(CallbackApiWritePerfTest.class.getName());
32 private static final ClassifyAddDelTable REQUEST = createAddDelTable();
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;
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};
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
56 public static void main(String[] args) throws Exception {
57 if (args.length != 0) {
58 testInvokeCounter(true, Integer.parseUnsignedInt(args[0]));
60 testInvokeCounter(false, 0);
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
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);
77 for(int i = 0; i < 5; i++) {
79 LOG.info("Starting invocation for 1sec");
80 long time = System.nanoTime();
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));
88 for(int i = 0; i < 5; i++) {
89 LOG.info("Starting invocations");
91 long time = System.nanoTime();
92 for (int x = 1; x <= count; x++) {
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))));
103 LOG.info("Disconnecting...");
108 static class TestCallback implements ClassifyAddDelTableReplyCallback {
110 private int replyCounter = 0;
112 private long time = 0;
113 private boolean stop = false;
115 public TestCallback(int count) throws Exception {
119 public int getReplyCounter() {
123 public void reset() {
129 public boolean stop() {
134 /* actual method called from VPP
135 not thread safe but since there's only one VPP thread listening for requests and calling
139 public void onClassifyAddDelTableReply(final ClassifyAddDelTableReply msg) {
144 if (replyCounter == count ) {
145 time = System.nanoTime();
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());
155 public long getTime() throws Exception {