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.callback.ShowVersionReplyCallback;
25 import io.fd.vpp.jvpp.core.dto.*;
27 import java.util.logging.Logger;
29 public class CallbackApiReadPerfTest {
31 private static final Logger LOG = Logger.getLogger(CallbackApiReadPerfTest.class.getName());
32 private static final ShowVersion REQUEST = new ShowVersion();
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
40 public static void main(String[] args) throws Exception {
41 if (args.length != 0) {
42 testInvokeCounter(true, Integer.parseUnsignedInt(args[0]));
44 testInvokeCounter(false, 0);
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
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);
61 for(int i = 0; i < 5; i++) {
63 LOG.info("Starting invocation for 1sec");
64 long time = System.nanoTime();
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));
72 for (int i = 0; i < 5; i++) {
73 LOG.info("Starting invocations");
75 long time = System.nanoTime();
76 for (int x = 0; x < count; x++) {
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))));
87 LOG.info("Disconnecting...");
92 static class TestCallback implements ShowVersionReplyCallback {
94 private int replyCounter = 0;
96 private long time = 0;
97 private boolean stop = false;
99 public TestCallback(int count) throws Exception {
103 public int getReplyCounter() {
107 public void reset() {
113 public boolean stop() {
118 /* actual method called from VPP
119 not thread safe but since there's only one VPP thread listening for requests and calling
123 public void onShowVersionReply(final ShowVersionReply msg) {
128 if (replyCounter == count ) {
129 time = System.nanoTime();
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());
139 public long getTime() throws Exception {