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

import java.util.concurrent.TimeUnit;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.ConnectionFactoryBuilder.Protocol;
import net.spy.memcached.MemcachedClient;

public class TestPerformance {
    private static final int N = 10000;
    private static void test(MemcachedClient cache) throws InterruptedException {
        // put speed
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            cache.set("K#" + (i + 1), 0, i + 1);
        }
        cache.waitForQueues(100, TimeUnit.MILLISECONDS); // needed to sync
        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 {
        MemcachedClient cache = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY).build(), AddrUtil.getAddresses("localhost:11211"));
        for(int i = 0; i < REP; i++) {
            test(cache);
        }
        cache.shutdown();
    }
}
