8a737d216b220efee5e7633b30a3af4a679fd973
[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 io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
20 import java.util.Random;
21 import javax.annotation.concurrent.NotThreadSafe;
22
23 @NotThreadSafe
24 class ClassifyTableProviderImpl implements ClassifyTableProvider {
25     /**
26      * Static seed to make rnd.nextBytes() output the same for all test run.
27      */
28     private static final long SEED = -2084670072119134328L;
29     private final int tableSetSize;
30     private final ClassifyAddDelTable[] tables;
31     private final Random rnd = new Random(SEED);
32
33     /**
34      * Pointer to Classify table to be returned by invocation of {@link #next()} method.
35      */
36     private int currentTable = 0;
37
38     ClassifyTableProviderImpl(final int tableSetSize) {
39         this.tableSetSize = tableSetSize;
40         tables = new ClassifyAddDelTable[tableSetSize];
41         initTables(tableSetSize);
42     }
43
44     @Override
45     public ClassifyAddDelTable next() {
46         final ClassifyAddDelTable result = tables[currentTable];
47         currentTable = (currentTable + 1) % tableSetSize;
48         return result;
49     }
50
51     private void initTables(final int tableSetSize) {
52         for (int i = 0; i < tableSetSize; ++i) {
53             tables[i] = createTable();
54         }
55     }
56
57     private ClassifyAddDelTable createTable() {
58         final ClassifyAddDelTable addDelTable = new ClassifyAddDelTable();
59         addDelTable.isAdd = 1;
60         addDelTable.tableIndex = -1;
61         addDelTable.nbuckets = 2;
62         addDelTable.memorySize = 2 << 20;
63         addDelTable.nextTableIndex = ~0;
64         addDelTable.missNextIndex = ~0;
65         addDelTable.skipNVectors = 0;
66         addDelTable.matchNVectors = 1;
67         addDelTable.mask = new byte[16];
68         addDelTable.maskLen = 16;
69         rnd.nextBytes(addDelTable.mask);
70         return addDelTable;
71     }
72 }