32700933e1e412bfb2f763ea01543f46c635b9ca
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2018 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.hc2vpp.it.jvpp.benchmark.classify;
18
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;
46
47 @BenchmarkMode(Mode.AverageTime)
48 @State(Scope.Thread)
49 @Fork(1)
50 @Threads(1)
51 @Timeout(time = 5)
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);
57
58     @Param( {"100"})
59     private int tableSetSize;
60
61     private JVppRegistryImpl registry;
62     private FutureJVppCoreFacade jvppCore;
63     private ClassifyTableProvider classifyTableProvider;
64
65     @Benchmark
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();
69     }
70
71     @Setup(Level.Iteration)
72     public void setup() throws Exception {
73         initProvider();
74         startVpp();
75         connect();
76     }
77
78     @TearDown(Level.Iteration)
79     public void tearDown() throws Exception {
80         disconnect();
81         stopVpp();
82     }
83
84     private void initProvider() {
85         classifyTableProvider = new ClassifyTableProviderImpl(tableSetSize);
86     }
87
88     private void startVpp() throws Exception {
89         LOG.info("Starting VPP ...");
90         final String[] cmd = {"/bin/sh", "-c", "sudo service vpp start"};
91         exec(cmd);
92         LOG.info("VPP started successfully");
93     }
94
95     private void stopVpp() throws Exception {
96         LOG.info("Stopping VPP ...");
97         final String[] cmd = {"/bin/sh", "-c", "sudo service vpp stop"};
98         exec(cmd);
99
100         // Wait to be sure VPP was stopped.
101         // Prevents VPP start failure: "vpp.service: Start request repeated too quickly".
102         Thread.sleep(1500);
103         LOG.info("VPP stopped successfully");
104     }
105
106     private static void exec(String[] command) throws IOException, InterruptedException {
107         Process process = Runtime.getRuntime().exec(command);
108         process.waitFor();
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);
113         }
114     }
115
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");
121     }
122
123     private void disconnect() throws Exception {
124         LOG.info("Disconnecting ...");
125         jvppCore.close();
126         registry.close();
127         LOG.info("Successfully disconnected ...");
128     }
129 }