alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (IntObjectHashMapBenchmark.java)

This example Java source code file (IntObjectHashMapBenchmark.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

agronaenvironment, benchmark, environment, http2, illegalstateexception, int2objecthashmap, intobjecthashmap, keydistribution, maptype, nettyenvironment, override, param, random, util, value

The IntObjectHashMapBenchmark.java Java example source code

/*
 * Copyright 2015 The Netty Project
 *
 * The Netty Project licenses this file to you 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.netty.microbenchmark.common;

import io.netty.microbench.util.AbstractMicrobenchmark;
import io.netty.util.collection.IntObjectHashMap;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.infra.Blackhole;
import uk.co.real_logic.agrona.collections.Int2ObjectHashMap;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class IntObjectHashMapBenchmark extends AbstractMicrobenchmark {
    private static final Long VALUE = Long.MAX_VALUE;

    public enum MapType {
        AGRONA,
        NETTY
    }

    public enum KeyDistribution {
        HTTP2,
        RANDOM
    }

    @Param({ "10", "100", "1000", "10000", "100000" })
    public int size;

    @Param
    public MapType mapType;

    @Param
    public KeyDistribution keyDistribution;

    private Environment environment;

    @Setup(Level.Trial)
    public void setup() {
        switch(mapType) {
            case AGRONA: {
                environment = new AgronaEnvironment();
                break;
            }
            case NETTY: {
                environment = new NettyEnvironment();
                break;
            }
            default: {
                throw new IllegalStateException("Invalid mapType: " + mapType);
            }
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    public void put(Blackhole bh) {
        environment.put(bh);
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    public void lookup(Blackhole bh) {
        environment.lookup(bh);
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    public void remove(Blackhole bh) {
        environment.remove(bh);
    }

    private abstract class Environment {
        final int[] keys;
        Environment() {
            keys = new int[size];
            switch(keyDistribution) {
                case HTTP2:
                    for (int index = 0, key = 3; index < size; ++index, key += 2) {
                        keys[index] = key;
                    }
                    break;
                case RANDOM: {
                    // Create a 'size' # of random integers.
                    Random r = new Random();
                    Set<Integer> keySet = new HashSet();
                    while (keySet.size() < size) {
                        keySet.add(r.nextInt());
                    }

                    int index = 0;
                    for (Integer key : keySet) {
                        keys[index++] = key;
                    }
                    break;
                }
                default: {
                    throw new IllegalStateException("Unknown keyDistribution: " + keyDistribution);
                }
            }
        }
        abstract void put(Blackhole bh);
        abstract void lookup(Blackhole bh);
        abstract void remove(Blackhole bh);
    }

    private class AgronaEnvironment extends Environment {
        private final Int2ObjectHashMap<Long> map = new Int2ObjectHashMap();

        AgronaEnvironment() {
            for (int key : keys) {
                map.put(key, VALUE);
            }
        }

        @Override
        void put(Blackhole bh) {
            Int2ObjectHashMap<Long> map = new Int2ObjectHashMap();
            for (int key : keys) {
                bh.consume(map.put(key, VALUE));
            }
        }

        @Override
        void lookup(Blackhole bh) {
            for (int key : keys) {
                bh.consume(map.get(key));
            }
        }

        @Override
        void remove(Blackhole bh) {
            Int2ObjectHashMap<Long> copy = new Int2ObjectHashMap();
            copy.putAll(map);
            for (int key : keys) {
                bh.consume(copy.remove(key));
            }
        }
    }

    private class NettyEnvironment extends Environment {
        private final IntObjectHashMap<Long> map = new IntObjectHashMap();

        NettyEnvironment() {
            for (int key : keys) {
                map.put(key, VALUE);
            }
        }

        @Override
        void put(Blackhole bh) {
            IntObjectHashMap<Long> map = new IntObjectHashMap();
            for (int key : keys) {
                bh.consume(map.put(key, VALUE));
            }
        }

        @Override
        void lookup(Blackhole bh) {
            for (int key : keys) {
                bh.consume(map.get(key));
            }
        }

        @Override
        void remove(Blackhole bh) {
            IntObjectHashMap<Long> copy = new IntObjectHashMap();
            copy.putAll(map);
            for (int key : keys) {
                bh.consume(copy.remove(key));
            }
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java IntObjectHashMapBenchmark.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.