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

import java.util.HashMap;
import java.util.Map;

import jakarta.nosql.Settings;
import jakarta.nosql.keyvalue.BucketManagerFactory;
import jakarta.nosql.keyvalue.KeyValueConfiguration;

import org.eclipse.jnosql.diana.infinispan.keyvalue.InfinispanKeyValueConfiguration;

public class TestPerformance {
    private static final int N = 10000;
    private static void test(Map<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 {
        KeyValueConfiguration cfg = new InfinispanKeyValueConfiguration();
        Map<String, Object> props = new HashMap<>();
        props.put("infinispan.host", "localhost"); // necessary to get remote cache instead of local cache
        BucketManagerFactory bmf = cfg.get(Settings.of(props));
        Map<String,Integer> cache = bmf.getMap("performance_cache", String.class, Integer.class);
        for(int i = 0; i < REP; i++) {
            test(cache);
        }
        bmf.close();
    }
}
