<commit_before>/*
 * 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.TestException;
import com.hazelcast.simulator.test.annotations.AfterRun;
import com.hazelcast.simulator.test.annotations.BeforeRun;
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.Verify;
import com.hazelcast.simulator.test.annotations.Warmup;

import java.util.HashMap;
import java.util.Map;

import static com.hazelcast.simulator.utils.UuidUtil.newSecureUuidString;
import static org.junit.Assert.assertEquals;

/**
 * This tests the cas method: replace. So for optimistic concurrency control.
 * <p>
 * We have a bunch of predefined keys, and we are going to concurrently increment the value and we protect ourselves against lost
 * updates using cas method replace.
 * <p>
 * Locally we keep track of all increments, and if the sum of these local increments matches the global increment, we are done.
 */
public class MapCasTest extends AbstractTest {

    // properties
    public int keyCount = 1000;

    private IMap<Integer, Long> map;
    private IMap<String, Map<Integer, Long>> resultsPerWorker;

    @Setup
    public void setUp() {
        map = targetInstance.getMap(name);
        resultsPerWorker = targetInstance.getMap(name + ":ResultMap");
    }

    @Warmup(global = true)
    public void warmup() {
        for (int i = 0; i < keyCount; i++) {
            map.put(i, 0L);
        }
    }

    @BeforeRun
    public void beforeRun(ThreadState state) {
        int size = map.size();
        if (size != keyCount) {
            throw new TestException(
                    "Warmup has not run since the map is not filled correctly, found size: %s, expected size: %s",
                    size, keyCount);
        }

        for (int i = 0; i < keyCount; i++) {
            state.result.put(i, 0L);
        }
    }

    @TimeStep
    protected void timeStep(ThreadState state) throws Exception {
        Integer key = state.randomInt(keyCount);
        long incrementValue = state.randomInt(100);

        for (; ; ) {
            Long current = map.get(key);
            Long update = current + incrementValue;
            if (map.replace(key, current, update)) {
                state.increment(key, incrementValue);
                break;
            }
        }
    }

    @AfterRun
    public void afterRun(ThreadState state) {
        resultsPerWorker.put(newSecureUuidString(), state.result);
    }
<commit_msg>Made timestep method of MapCasTest public<commit_after>/*
 * 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.TestException;
import com.hazelcast.simulator.test.annotations.AfterRun;
import com.hazelcast.simulator.test.annotations.BeforeRun;
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.Verify;
import com.hazelcast.simulator.test.annotations.Warmup;

import java.util.HashMap;
import java.util.Map;

import static com.hazelcast.simulator.utils.UuidUtil.newSecureUuidString;
import static org.junit.Assert.assertEquals;

/**
 * This tests the cas method: replace. So for optimistic concurrency control.
 * <p>
 * We have a bunch of predefined keys, and we are going to concurrently increment the value and we protect ourselves against lost
 * updates using cas method replace.
 * <p>
 * Locally we keep track of all increments, and if the sum of these local increments matches the global increment, we are done.
 */
public class MapCasTest extends AbstractTest {

    // properties
    public int keyCount = 1000;

    private IMap<Integer, Long> map;
    private IMap<String, Map<Integer, Long>> resultsPerWorker;

    @Setup
    public void setUp() {
        map = targetInstance.getMap(name);
        resultsPerWorker = targetInstance.getMap(name + ":ResultMap");
    }

    @Warmup(global = true)
    public void warmup() {
        for (int i = 0; i < keyCount; i++) {
            map.put(i, 0L);
        }
    }

    @BeforeRun
    public void beforeRun(ThreadState state) {
        int size = map.size();
        if (size != keyCount) {
            throw new TestException(
                    "Warmup has not run since the map is not filled correctly, found size: %s, expected size: %s",
                    size, keyCount);
        }

        for (int i = 0; i < keyCount; i++) {
            state.result.put(i, 0L);
        }
    }

    @TimeStep
    public void timeStep(ThreadState state) throws Exception {
        Integer key = state.randomInt(keyCount);
        long incrementValue = state.randomInt(100);

        for (; ; ) {
            Long current = map.get(key);
            Long update = current + incrementValue;
            if (map.replace(key, current, update)) {
                state.increment(key, incrementValue);
                break;
            }
        }
    }

    @AfterRun
    public void afterRun(ThreadState state) {
        resultsPerWorker.put(newSecureUuidString(), state.result);
    }

{'fraction_non_alphanumeric': 0.09358803494743077, 'fraction_numerical': 0.0068117873537686955, 'mean_word_length': 2.9729411764705884, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5714773', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1907, 'n_words': 609}