// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package cache.jcache;

import java.net.URI;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

public class TestPerformance {
    private static final int N = 10000;
    private static void test(Cache<String,Integer> cache) {
        // put speed
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            cache.put("K#" + (i + 1), i + 1);
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("%d puts per second\n", N * 1000 / (t2 - t1));
    }
    private static final int REP = 5;
    public static void main(String[] args) throws Exception {
        ((Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(Level.WARN); // hack to avoid debug & info output
        CachingProvider cp = Caching.getCachingProvider("org.redisson.jcache.JCachingProvider");
        CacheManager cm = cp.getCacheManager(new URI("file:/C:/Work/redis.yaml"), null, null);
        Cache<String,Integer> cache = cm.getCache("performance_cache");
        if(cache == null) {
            cache = cm.createCache("performance_cache", new MutableConfiguration<String,Integer>());
        }
        for(int i = 0; i < REP; i++) {
            test(cache);
        }
        cache.close();
        cm.close();
        cp.close();
    }
}
