<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.core.HazelcastInstance;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.simulator.test.TestContext;
import com.hazelcast.simulator.test.TestRunner;
import com.hazelcast.simulator.test.annotations.RunWithWorker;
import com.hazelcast.simulator.test.annotations.Setup;
import com.hazelcast.simulator.test.annotations.Verify;
import com.hazelcast.simulator.worker.tasks.AbstractMonotonicWorker;

import javax.cache.CacheManager;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;

import static com.hazelcast.simulator.tests.icache.helpers.CacheUtils.createCacheManager;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.FormatUtils.humanReadableByteCount;

public class ExpiryICacheTest {

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

    // properties
    public String basename = ExpiryICacheTest.class.getSimpleName();
    public double maxHeapUsagePercentage = 80;

    private final ExpiryPolicy expiryPolicy = new CreatedExpiryPolicy(Duration.ONE_MINUTE);

    private ICache<Long, Long> cache;

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

        CacheManager cacheManager = createCacheManager(hazelcastInstance);
        cache = (ICache<Long, Long>) cacheManager.<Long, Long>getCache(basename);
    }

    @Verify(global = false)
    public void globalVerify() {
        LOGGER.info(basename + " cache size = " + cache.size());
        logMemoryStatistics();
    }

    @RunWithWorker
    public Worker run() {
        return new Worker();
    }

    private class Worker extends AbstractMonotonicWorker {

        @Override
        protected void beforeRun() {
            LOGGER.info(basename + ".beforeRun()");
            logMemoryStatistics();
        }

        @Override
        public void timeStep() {
            double usedPercentage = heapUsedPercentage();
            if (usedPercentage >= maxHeapUsagePercentage) {
                LOGGER.info("heap used: " + usedPercentage + "% cache size: " + cache.size());
                sleepSeconds(10);
            } else {
                for (int i = 0; i < 1000; i++) {
                    if (getIteration() % 100000 == 0) {
                        LOGGER.info("At " + getIteration() + " heap used: " + usedPercentage + "% cache size: " + cache.size());
                    }

                    long key = getRandom().nextLong();
                    cache.put(key, 0L, expiryPolicy);
                }
            }
        }

        @Override
        protected void afterRun() {
            LOGGER.info(basename + ".afterRun()");
            logMemoryStatistics();
        }

        private double heapUsedPercentage() {
            long total = Runtime.getRuntime().totalMemory();
            long max = Runtime.getRuntime().maxMemory();
            return (100d * total) / max;
        }
    }

    private void logMemoryStatistics() {
        long free = Runtime.getRuntime().freeMemory();
        long total = Runtime.getRuntime().totalMemory();
        long baseLineUsed = total - free;
        long maxBytes = Runtime.getRuntime().maxMemory();
        double usedOfMax = 100.0 * ((double) baseLineUsed / (double) maxBytes);

        LOGGER.info(basename + " free = " + humanReadableByteCount(free, true) + " = " + free);
        LOGGER.info(basename + " used = " + humanReadableByteCount(baseLineUsed, true) + " = " + baseLineUsed);
        LOGGER.info(basename + " max = " + humanReadableByteCount(maxBytes, true) + " = " + maxBytes);
        LOGGER.info(basename + " usedOfMax = " + usedOfMax + '%');
    }

    public static void main(String[] args) throws Exception {
        new TestRunner<ExpiryICacheTest>(new ExpiryICacheTest()).run();
    }
}<commit_msg>operate on a fixed well defined keyDomain with a known upper bound,  which will not grow with the number of clients use<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.core.HazelcastInstance;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.simulator.test.TestContext;
import com.hazelcast.simulator.test.TestRunner;
import com.hazelcast.simulator.test.annotations.RunWithWorker;
import com.hazelcast.simulator.test.annotations.Setup;
import com.hazelcast.simulator.test.annotations.Verify;
import com.hazelcast.simulator.utils.AssertTask;
import com.hazelcast.simulator.worker.tasks.AbstractMonotonicWorker;

import javax.cache.CacheManager;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.expiry.ExpiryPolicy;

import static com.hazelcast.simulator.tests.icache.helpers.CacheUtils.createCacheManager;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.FormatUtils.humanReadableByteCount;
import static com.hazelcast.simulator.utils.TestUtils.assertTrueEventually;
import static org.junit.Assert.assertEquals;

public class ExpiryICacheTest {

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

    //default keyCount entries of int,  is upper bound to approx 8MB possible max, if all put in side expiryPolicy time
    public int keyCount=1000000;
    public String basename = ExpiryICacheTest.class.getSimpleName();

    private final ExpiryPolicy expiryPolicy = new CreatedExpiryPolicy(Duration.ONE_MINUTE);

    private ICache cache;

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

        CacheManager cacheManager = createCacheManager(hazelcastInstance);
        cache = (ICache) cacheManager.getCache(basename);
    }

    @RunWithWorker
    public Worker run() {
        return new Worker();
    }

    private class Worker extends AbstractMonotonicWorker {

        @Override
        protected void beforeRun() { }

        @Override
        public void timeStep() {
            long key = getRandom().nextInt(keyCount);
            if ( ! cache.containsKey(key) ) {
                cache.put(key, 0, expiryPolicy);
            }
        }

        @Override
        protected void afterRun() { }
    }

    @Verify(global = false)
    public void globalVerify() {

        assertTrueEventually(new AssertTask() {
            @Override
            public void run() throws Exception {
                LOGGER.info(basename + " cache size = " + cache.size());
                assertEquals(basename + ": ICache should be empty, TTL events not processed", 0, cache.size());
            }
        });
    }

    public static void main(String[] args) throws Exception {
        new TestRunner<ExpiryICacheTest>(new ExpiryICacheTest()).run();
    }
}
{'fraction_non_alphanumeric': 0.09760191846522782, 'fraction_numerical': 0.006954436450839328, 'mean_word_length': 3.1559541604384655, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, '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': '2656714', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2341, 'n_words': 597}