jvpp-benchmark: fix IP generation 13/12713/1
authorMarek Gradzki <[email protected]>
Thu, 24 May 2018 08:49:51 +0000 (10:49 +0200)
committerMarek Gradzki <[email protected]>
Thu, 24 May 2018 09:49:33 +0000 (09:49 +0000)
Change-Id: I5ab4465f25c13e0f3b89e2757bde9dd47c660732
Signed-off-by: Marek Gradzki <[email protected]>
it/jvpp-benchmark/pom.xml
it/jvpp-benchmark/src/main/java/io/fd/hc2vpp/it/jvpp/benchmark/AclProviderImpl.java
it/jvpp-benchmark/src/main/java/io/fd/hc2vpp/it/jvpp/benchmark/AclUpdateBenchmark.java
it/jvpp-benchmark/src/test/java/io/fd/hc2vpp/it/jvpp/benchmark/AclProviderImplTest.java [new file with mode: 0644]

index f0e801e..84cf730 100644 (file)
             <artifactId>jvpp-acl</artifactId>
             <version>${jvpp.version}</version>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
index 4a39bec..fea9055 100644 (file)
@@ -27,6 +27,7 @@ class AclProviderImpl implements AclProvider {
 
     private final int aclSetSize;
     private final AclAddReplace[] acls;
+    private final IpProvider ipProvider = new IpProvider();
 
     /**
      * Pointer to ACL to be returned by invocation of {@link #next()} method.
@@ -54,29 +55,28 @@ class AclProviderImpl implements AclProvider {
     }
 
     private void initAcls(final int aclSetSize, final int aclSize) {
-        long ip = 0x01000000; // 1.0.0.0
         for (int i = 0; i < aclSetSize; ++i) {
-            acls[i] = createAddReplaceRequest(aclSize, CREATE_NEW_ACL, (int) (ip++));
+            acls[i] = createAddReplaceRequest(aclSize, CREATE_NEW_ACL);
         }
     }
 
-    private static AclAddReplace createAddReplaceRequest(final int size, final int index, final int ip) {
+    private AclAddReplace createAddReplaceRequest(final int size, final int index) {
         AclAddReplace request = new AclAddReplace();
         request.aclIndex = index;
         request.count = size;
-        request.r = createRules(size, ip);
+        request.r = createRules(size);
         return request;
     }
 
-    private static AclRule[] createRules(final int size, final int ip) {
+    private AclRule[] createRules(final int size) {
         final AclRule[] rules = new AclRule[size];
         for (int i = 0; i < size; ++i) {
             rules[i] = new AclRule();
             rules[i].isIpv6 = 0;
             rules[i].isPermit = 1;
-            rules[i].srcIpAddr = getIp(ip);
+            rules[i].srcIpAddr = ipProvider.next();
             rules[i].srcIpPrefixLen = 32;
-            rules[i].dstIpAddr = getIp(ip);
+            rules[i].dstIpAddr = ipProvider.next();
             rules[i].dstIpPrefixLen = 32;
             rules[i].dstportOrIcmpcodeFirst = 0;
             rules[i].dstportOrIcmpcodeLast = MAX_PORT_NUMBER;
@@ -87,11 +87,19 @@ class AclProviderImpl implements AclProvider {
         return rules;
     }
 
-    private static byte[] getIp(final int i) {
-        int b1 = (i >> 24) & 0xff;
-        int b2 = (i >> 16) & 0xff;
-        int b3 = (i >> 8) & 0xff;
-        int b4 = i & 0xff;
-        return new byte[] {(byte) b1, (byte) b2, (byte) b3, (byte) b4};
+    private static final class IpProvider {
+        private long ip = 0x01000000; // 1.0.0.0
+
+        private static byte[] getIp(final int i) {
+            int b1 = (i >> 24) & 0xff;
+            int b2 = (i >> 16) & 0xff;
+            int b3 = (i >> 8) & 0xff;
+            int b4 = i & 0xff;
+            return new byte[] {(byte) b1, (byte) b2, (byte) b3, (byte) b4};
+        }
+
+        private byte[] next() {
+            return getIp((int) (ip++));
+        }
     }
 }
\ No newline at end of file
index 8a26e2d..ef314b8 100644 (file)
@@ -16,8 +16,6 @@
 
 package io.fd.hc2vpp.it.jvpp.benchmark;
 
-import static org.openjdk.jmh.annotations.Mode.Throughput;
-
 import com.google.common.io.CharStreams;
 import io.fd.vpp.jvpp.JVppRegistryImpl;
 import io.fd.vpp.jvpp.acl.JVppAclImpl;
diff --git a/it/jvpp-benchmark/src/test/java/io/fd/hc2vpp/it/jvpp/benchmark/AclProviderImplTest.java b/it/jvpp-benchmark/src/test/java/io/fd/hc2vpp/it/jvpp/benchmark/AclProviderImplTest.java
new file mode 100644 (file)
index 0000000..fd4b0d7
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.hc2vpp.it.jvpp.benchmark;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+
+import io.fd.vpp.jvpp.acl.dto.AclAddReplace;
+import java.util.Arrays;
+import org.junit.Test;
+
+public class AclProviderImplTest {
+    @Test
+    public void testAclsDiffer() throws Exception {
+        final AclProviderImpl aclProvider = new AclProviderImpl(2, 2);
+        final AclAddReplace acl0 = aclProvider.next();
+        final AclAddReplace acl1 = aclProvider.next();
+        final AclAddReplace acl2 = aclProvider.next();
+        final AclAddReplace acl3 = aclProvider.next();
+
+        // Test if ACLs are provided in round-robin fashion
+        assertEquals("ACLs 0 and 2 should be equal", acl0, acl2);
+        assertEquals("ACLs 1 and 3 should be equal", acl1, acl3);
+        assertNotEquals("ACLs 0 and 1 should be different", acl0, acl1);
+    }
+
+    @Test
+    public void testRulesDiffer() throws Exception {
+        final int aclSize = 3;
+        final AclProviderImpl aclProvider = new AclProviderImpl(1, aclSize);
+        final AclAddReplace acl = aclProvider.next();
+        assertEquals("Unexpected value of AclAddReplace.count", aclSize, acl.count);
+        assertEquals("Unexpected size of ACL", aclSize, acl.r.length);
+        assertNotEquals("ACL rules 0 and 1 should be different", acl.r[0], acl.r[1]);
+        assertNotEquals("ACL rules 1 and 2 should be different", acl.r[1], acl.r[2]);
+        assertNotEquals("ACL rules 0 and 2 should be different", acl.r[0], acl.r[2]);
+    }
+
+    @Test
+    public void testIPsWithinRuleDiffer() throws Exception {
+        final AclProviderImpl aclProvider = new AclProviderImpl(1, 1);
+        final AclAddReplace acl = aclProvider.next();
+        assertFalse(Arrays.equals(acl.r[0].srcIpAddr, acl.r[0].dstIpAddr));
+    }
+}
\ No newline at end of file