All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.simulator.tests.map.MapPutAllTest Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.simulator.tests.map;

import com.hazelcast.core.IMap;
import com.hazelcast.simulator.test.AbstractTest;
import com.hazelcast.simulator.test.BaseThreadState;
import com.hazelcast.simulator.test.annotations.Setup;
import com.hazelcast.simulator.test.annotations.Teardown;
import com.hazelcast.simulator.test.annotations.TimeStep;
import com.hazelcast.simulator.test.annotations.Warmup;
import com.hazelcast.simulator.tests.helpers.GenericTypes;
import com.hazelcast.simulator.tests.helpers.KeyLocality;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import static com.hazelcast.simulator.tests.helpers.GenericTypes.STRING;
import static com.hazelcast.simulator.tests.helpers.HazelcastTestUtils.getOperationCountInformation;
import static com.hazelcast.simulator.tests.helpers.KeyLocality.SHARED;

/**
 * Test for {@link IMap#putAll(Map)} which uses a set of prepared maps with input values during the RUN phase.
 *
 * You can configure the {@link #keyType} and {@link #valueType} for the used maps.
 */
public class MapPutAllTest extends AbstractTest {

    // properties
    public KeyLocality keyLocality = SHARED;
    public GenericTypes keyType = STRING;
    public GenericTypes valueType = STRING;
    public int keyCount = 1000000;
    public int itemCount = 10000;

    // the length of the key (just used with keyType STRING)
    public int keySize = 10;
    // the length of the the value (just used with keyType STRING)
    public int valueSize = 100;
    // the number of prepared maps with input values (we don't want to keep inserting the same values over an over again)
    public int mapCount = 2;

    // if we want to used a SortedMap for input values
    public boolean useSortedMap = true;
    // if we want to use putAll() or put() in a loop (this is a nice setting to see what kind of speedup or slowdown to expect)
    public boolean usePutAll = true;

    private IMap map;
    private Map[] inputMaps;

    @Setup
    public void setUp() {
        map = targetInstance.getMap(name);
    }

    @Warmup
    @SuppressWarnings("unchecked")
    public void warmup() {
        Object[] keys = keyType.generateKeys(targetInstance, keyLocality, keyCount, keySize);

        inputMaps = new Map[mapCount];
        Random random = new Random();
        for (int mapIndex = 0; mapIndex < mapCount; mapIndex++) {
            // generate a SortedMap or HashMap depending on the configuration
            Map tmpMap = (useSortedMap ? new TreeMap() : new HashMap(itemCount));
            for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
                Object key = keys[random.nextInt(keyCount)];
                Object value = valueType.generateValue(random, valueSize);
                tmpMap.put(key, value);
            }
            inputMaps[mapIndex] = tmpMap;
        }
    }

    @TimeStep
    public void timeStep(ThreadState state) {
        Map insertMap = state.randomMap();

        // TODO: would be better to have two timeStep methods
        if (usePutAll) {
            map.putAll(insertMap);
        } else {
            for (Map.Entry entry : insertMap.entrySet()) {
                map.put(entry.getKey(), entry.getValue());
            }
        }
    }

    public class ThreadState extends BaseThreadState {

        private Map randomMap() {
            return inputMaps[randomInt(inputMaps.length)];
        }
    }

    @Teardown
    public void tearDown() {
        map.destroy();
        logger.info(getOperationCountInformation(targetInstance));

        if (valueType == GenericTypes.INTEGER) {
            valueSize = Integer.MAX_VALUE;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy