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