9424d072246c907b6f0b5bea8a00c06a68ff669c
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestInstantiationSpeed.java
1 /*
2  * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
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 package org.opendaylight.yangtools.triemap;
17
18 import com.google.common.base.Stopwatch;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class TestInstantiationSpeed {
26     private static final Logger LOG = LoggerFactory.getLogger(TestInstantiationSpeed.class);
27     private static final int COUNT = 1000000;
28     private static final int ITERATIONS = 10;
29     private static final int WARMUP = 10;
30
31     private static Stopwatch runIteration() {
32         final TrieMap<?, ?>[] maps = new TrieMap<?, ?>[COUNT];
33
34         final Stopwatch watch = Stopwatch.createStarted();
35         for (int i = 0; i < COUNT; ++i) {
36             maps[i] = new TrieMap<>();
37         }
38         watch.stop();
39
40         // Do not allow optimizations
41         LOG.trace("Maps: {}", (Object) maps);
42         return watch;
43     }
44
45     private static long elapsedToNs(final Stopwatch watch) {
46         return watch.elapsed(TimeUnit.NANOSECONDS) / COUNT;
47     }
48
49     @Ignore
50     @Test
51     public void testInstantiation() {
52
53         for (int i = 0; i < WARMUP; ++i) {
54             final Stopwatch time = runIteration();
55             LOG.debug("Warmup {} took {} ({} ns)", i, time, elapsedToNs(time));
56         }
57
58         long acc = 0;
59         for (int i = 0; i < ITERATIONS; ++i) {
60             final Stopwatch time = runIteration();
61             LOG.debug("Iteration {} took {} ({} ns)", i, time, elapsedToNs(time));
62             acc += time.elapsed(TimeUnit.NANOSECONDS);
63         }
64
65         LOG.info("Instantiation cost {} ns", acc / ITERATIONS / COUNT);
66     }
67 }