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

import org.infinispan.client.hotrod.DefaultTemplate;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.impl.ConfigurationProperties;

public class TestPerformance {
    private static final int N = 10000;
    private static void test(RemoteCache<String,Integer> cache) throws InterruptedException {
        // 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 {
        ConfigurationBuilder b = new ConfigurationBuilder();
        b.addServer().host("localhost").port(ConfigurationProperties.DEFAULT_HOTROD_PORT);
        RemoteCacheManager rcm = new RemoteCacheManager(b.build());
        RemoteCache<String,Integer> cache = rcm.administration().getOrCreateCache("performance_cache", DefaultTemplate.DIST_SYNC);
        for(int i = 0; i < REP; i++) {
            test(cache);
        }
        rcm.stop();
        rcm.close();
    }
}
