<commit_before>/*
 * Copyright (c) 2008-2015, 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.icache;

import com.hazelcast.cache.ICache;
import com.hazelcast.config.CacheConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IList;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.simulator.test.TestContext;
import com.hazelcast.simulator.test.annotations.Performance;
import com.hazelcast.simulator.test.annotations.Run;
import com.hazelcast.simulator.test.annotations.Setup;
import com.hazelcast.simulator.test.annotations.Verify;
import com.hazelcast.simulator.test.annotations.Warmup;
import com.hazelcast.simulator.utils.ThreadSpawner;
import com.hazelcast.simulator.worker.selector.OperationSelector;
import com.hazelcast.simulator.worker.selector.OperationSelectorBuilder;

import javax.cache.CacheManager;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;
import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static com.hazelcast.simulator.tests.icache.helpers.CacheUtils.createCacheManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
 * In this tests we a putting and getting to/from a cache using an expiry policy.
 * The expiryDuration can be configured.
 * We verify that the cache is empty and items have expired.
 */
public class ExpiryTest {

    private enum Operation {
        PUT,
        PUT_ASYNC,
        GET,
        GET_ASYNC
    }

    private static final ILogger LOGGER = Logger.getLogger(ExpiryTest.class);

    public int threadCount = 3;
    public int expiryDuration = 500;
    public int keyCount = 1000;

    public double putProb = 0.4;
    public double putAsyncProb = 0.3;
    public double getProb = 0.2;
    public double getAsyncProb = 0.1;

    public int performanceUpdateFrequency = 10000;

    private TestContext testContext;
    private HazelcastInstance hazelcastInstance;
    private OperationSelectorBuilder<Operation> operationSelectorBuilder = new OperationSelectorBuilder<Operation>();
    private AtomicLong operations = new AtomicLong();
    private CacheManager cacheManager;
    private String basename;

    private ExpiryPolicy expiryPolicy;
    private CacheConfig<Integer, Long> config = new CacheConfig<Integer, Long>();

    @Setup
    public void setup(TestContext textConTx) {
        testContext = textConTx;
        hazelcastInstance = testContext.getTargetInstance();
        basename = testContext.getTestId();

        cacheManager = createCacheManager(hazelcastInstance);
        expiryPolicy = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, expiryDuration));

        config.setName(basename);

        operationSelectorBuilder.addOperation(Operation.PUT, putProb).addOperation(Operation.PUT_ASYNC, putAsyncProb)
                .addOperation(Operation.GET, getProb).addOperation(Operation.GET_ASYNC, getAsyncProb);
    }

    @Warmup(global = true)
    public void warmup() {
        cacheManager.createCache(basename, config);
    }

    @Run
    public void run() {
        ThreadSpawner spawner = new ThreadSpawner(testContext.getTestId());
        for (int i = 0; i < threadCount; i++) {
            spawner.spawn(new Worker());
        }
        spawner.awaitCompletion();
    }

    private class Worker implements Runnable {
        private final OperationSelector<Operation> selector = operationSelectorBuilder.build();
        private final Random random = new Random();
        private final Counter counter = new Counter();
        @SuppressWarnings("unchecked")<commit_msg>Minor cleanup of ExpiryTest due to private basename property.<commit_after>/*
 * Copyright (c) 2008-2015, 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.icache;

import com.hazelcast.cache.ICache;
import com.hazelcast.config.CacheConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IList;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.simulator.test.TestContext;
import com.hazelcast.simulator.test.annotations.Performance;
import com.hazelcast.simulator.test.annotations.Run;
import com.hazelcast.simulator.test.annotations.Setup;
import com.hazelcast.simulator.test.annotations.Verify;
import com.hazelcast.simulator.test.annotations.Warmup;
import com.hazelcast.simulator.utils.ThreadSpawner;
import com.hazelcast.simulator.worker.selector.OperationSelector;
import com.hazelcast.simulator.worker.selector.OperationSelectorBuilder;

import javax.cache.CacheManager;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;
import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static com.hazelcast.simulator.tests.icache.helpers.CacheUtils.createCacheManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
 * In this tests we a putting and getting to/from a cache using an expiry policy.
 * The expiryDuration can be configured.
 * We verify that the cache is empty and items have expired.
 */
public class ExpiryTest {

    private enum Operation {
        PUT,
        PUT_ASYNC,
        GET,
        GET_ASYNC
    }

    private static final ILogger LOGGER = Logger.getLogger(ExpiryTest.class);

    public String basename = ExpiryTest.class.getSimpleName();
    public int threadCount = 3;
    public int expiryDuration = 500;
    public int keyCount = 1000;

    public double putProb = 0.4;
    public double putAsyncProb = 0.3;
    public double getProb = 0.2;
    public double getAsyncProb = 0.1;

    public int performanceUpdateFrequency = 10000;

    private TestContext testContext;
    private HazelcastInstance hazelcastInstance;
    private OperationSelectorBuilder<Operation> operationSelectorBuilder = new OperationSelectorBuilder<Operation>();
    private AtomicLong operations = new AtomicLong();
    private CacheManager cacheManager;

    private ExpiryPolicy expiryPolicy;
    private CacheConfig<Integer, Long> config = new CacheConfig<Integer, Long>();

    @Setup
    public void setup(TestContext textConTx) {
        testContext = textConTx;
        hazelcastInstance = testContext.getTargetInstance();

        cacheManager = createCacheManager(hazelcastInstance);
        expiryPolicy = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, expiryDuration));

        config.setName(basename);

        operationSelectorBuilder.addOperation(Operation.PUT, putProb).addOperation(Operation.PUT_ASYNC, putAsyncProb)
                .addOperation(Operation.GET, getProb).addOperation(Operation.GET_ASYNC, getAsyncProb);
    }

    @Warmup(global = true)
    public void warmup() {
        cacheManager.createCache(basename, config);
    }

    @Run
    public void run() {
        ThreadSpawner spawner = new ThreadSpawner(testContext.getTestId());
        for (int i = 0; i < threadCount; i++) {
            spawner.spawn(new Worker());
        }
        spawner.awaitCompletion();
    }

    private class Worker implements Runnable {
        private final OperationSelector<Operation> selector = operationSelectorBuilder.build();
        private final Random random = new Random();
        private final Counter counter = new Counter();
        @SuppressWarnings("unchecked")
{'fraction_non_alphanumeric': 0.08488636363636363, 'fraction_numerical': 0.007727272727272728, 'mean_word_length': 4.034897025171625, 'pattern_counts': {'":': 0, '<': 15, '<?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': '3752643', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2468, 'n_words': 666}