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.JVppRegistry;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.core.JVppCoreImpl;
22 import io.fd.vpp.jvpp.core.callback.ShowVersionReplyCallback;
23 import io.fd.vpp.jvpp.core.dto.*;
24 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.logging.Logger;
29 public class FutureApiReadPerfTest {
31 private static final Logger LOG = Logger.getLogger(FutureApiReadPerfTest.class.getName());
32 private static final ShowVersion REQUEST = new ShowVersion();
33 private static volatile int currentCount = 0;
34 private static int desiredCount = 0;
35 private static long timeAfter = 0;
36 private static volatile boolean stop = false;
38 * Run after reply message is received
39 * in case of running for 1 sec check if time passed (stop variable) and if it does skip processing
40 * in case of running for set amount of REQUEST, record time in which was last reply received
41 * not thread save but since reading part process only one message at a time it's ok
43 private static Runnable replyFc = () -> {
48 if(currentCount == desiredCount) {
49 timeAfter = System.nanoTime();
54 * Used to reset counters and flags between runs
56 private static void reset() {
62 public static boolean stop() {
69 * @return time of last reply received
70 * @throws Exception during thread sleep
72 private static long getTime() throws Exception {
73 while(timeAfter == 0) {
74 LOG.info(String.format("Received %d replies", currentCount));
82 * @param args - for running for one sec requires no parameter
83 * - for running for set amount of requests requires one parameters, desired REQUEST amount
84 * @throws Exception if arguments aren't String representations of numbers
86 public static void main(String[] args) throws Exception {
87 if (args.length == 1) {
88 desiredCount = Integer.parseUnsignedInt(args[0]);
89 testInvokeCounter(true);
91 testInvokeCounter(false);
97 * @param setCount true = run with set amount of requests, false = run for 1 sec
100 private static void testInvokeCounter(boolean setCount) throws Exception {
101 LOG.info("Testing callback API Invocation Counter");
102 try (final JVppRegistry registry = new JVppRegistryImpl("FutureApiReadPerfTest");
103 final FutureJVppCoreFacade jvpp = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
105 for(int i = 0; i < 5; i++) {
107 LOG.info("Starting invocation for 1sec");
108 long time = System.nanoTime();
110 CompletableFuture<ShowVersionReply> replyFuture = jvpp.showVersion(REQUEST).toCompletableFuture();
111 replyFuture.thenRun(replyFc);
112 } while (System.nanoTime() - time < 1000000000 || stop());
113 LOG.info(String.format("Invocation count within 1 second: %d", currentCount));
116 for (int i = 0; i < 5; i++) {
117 LOG.info("Starting invocations");
119 long time = System.nanoTime();
120 for (int x = 0; x < desiredCount; x++) {
121 CompletableFuture<ShowVersionReply> replyFuture = jvpp.showVersion(REQUEST).toCompletableFuture();
122 replyFuture.thenRun(replyFc);
124 LOG.info("Invocations send");
125 long timeAfter = getTime();
126 LOG.info(String.format("Invocations took %d ns (%f invocations/s)", timeAfter - time,
127 desiredCount * (1000000000.0/(timeAfter - time))));
133 LOG.info("Disconnecting...");