2 * Copyright (c) 2018 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.hc2vpp.it.jvpp.benchmark.classify;
19 import com.google.common.io.CharStreams;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.core.JVppCoreImpl;
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
23 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.nio.charset.StandardCharsets;
27 import java.util.Arrays;
28 import java.util.concurrent.TimeUnit;
29 import org.openjdk.jmh.annotations.Benchmark;
30 import org.openjdk.jmh.annotations.BenchmarkMode;
31 import org.openjdk.jmh.annotations.Fork;
32 import org.openjdk.jmh.annotations.Level;
33 import org.openjdk.jmh.annotations.Measurement;
34 import org.openjdk.jmh.annotations.Mode;
35 import org.openjdk.jmh.annotations.OutputTimeUnit;
36 import org.openjdk.jmh.annotations.Param;
37 import org.openjdk.jmh.annotations.Scope;
38 import org.openjdk.jmh.annotations.Setup;
39 import org.openjdk.jmh.annotations.State;
40 import org.openjdk.jmh.annotations.TearDown;
41 import org.openjdk.jmh.annotations.Threads;
42 import org.openjdk.jmh.annotations.Timeout;
43 import org.openjdk.jmh.annotations.Warmup;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 @BenchmarkMode(Mode.AverageTime)
52 @Warmup(iterations = 20, time = 2)
53 @Measurement(iterations = 100, time = 2)
54 @OutputTimeUnit(TimeUnit.MILLISECONDS)
55 public class ClassifyTableCreateBenchmark {
56 private static final Logger LOG = LoggerFactory.getLogger(ClassifyTableCreateBenchmark.class);
59 private int tableSetSize;
61 private JVppRegistryImpl registry;
62 private FutureJVppCoreFacade jvppCore;
63 private ClassifyTableProvider classifyTableProvider;
66 public ClassifyAddDelTableReply testMethod() throws Exception {
67 // Caller may want to process reply, so return it to prevent JVM from dead code elimination
68 return jvppCore.classifyAddDelTable(classifyTableProvider.next()).toCompletableFuture().get();
71 @Setup(Level.Iteration)
72 public void setup() throws Exception {
78 @TearDown(Level.Iteration)
79 public void tearDown() throws Exception {
84 private void initProvider() {
85 classifyTableProvider = new ClassifyTableProviderImpl(tableSetSize);
88 private void startVpp() throws Exception {
89 LOG.info("Starting VPP ...");
90 final String[] cmd = {"/bin/sh", "-c", "sudo service vpp start"};
92 LOG.info("VPP started successfully");
95 private void stopVpp() throws Exception {
96 LOG.info("Stopping VPP ...");
97 final String[] cmd = {"/bin/sh", "-c", "sudo service vpp stop"};
100 // Wait to be sure VPP was stopped.
101 // Prevents VPP start failure: "vpp.service: Start request repeated too quickly".
103 LOG.info("VPP stopped successfully");
106 private static void exec(String[] command) throws IOException, InterruptedException {
107 Process process = Runtime.getRuntime().exec(command);
109 if (process.exitValue() != 0) {
110 String error_msg = "Failed to execute " + Arrays.toString(command) + ": " +
111 CharStreams.toString(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
112 throw new IllegalStateException(error_msg);
116 private void connect() throws IOException {
117 LOG.info("Connecting to JVPP ...");
118 registry = new JVppRegistryImpl("ACLUpdateBenchmark");
119 jvppCore = new FutureJVppCoreFacade(registry, new JVppCoreImpl());
120 LOG.info("Successfully connected to JVPP");
123 private void disconnect() throws Exception {
124 LOG.info("Disconnecting ...");
127 LOG.info("Successfully disconnected ...");