2 * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.opendaylight.yangtools.triemap;
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;
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;
31 private static Stopwatch runIteration() {
32 final TrieMap<?, ?>[] maps = new TrieMap<?, ?>[COUNT];
34 final Stopwatch watch = Stopwatch.createStarted();
35 for (int i = 0; i < COUNT; ++i) {
36 maps[i] = new TrieMap<>();
40 // Do not allow optimizations
41 LOG.trace("Maps: {}", (Object) maps);
45 private static long elapsedToNs(final Stopwatch watch) {
46 return watch.elapsed(TimeUnit.NANOSECONDS) / COUNT;
51 public void testInstantiation() {
53 for (int i = 0; i < WARMUP; ++i) {
54 final Stopwatch time = runIteration();
55 LOG.debug("Warmup {} took {} ({} ns)", i, time, elapsedToNs(time));
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);
65 LOG.info("Instantiation cost {} ns", acc / ITERATIONS / COUNT);