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

import org.apache.ignite.Ignition;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.client.ClientCache;
import org.apache.ignite.client.ClientCacheConfiguration;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.configuration.ClientConfiguration;

public class TestPerformance {
    private static void put(ClientCache<String,Integer> cache, String key, Integer value) {
        cache.query(new SqlFieldsQuery("MERGE INTO performance_cache(ckey,cvalue) VALUES(?,?)").setSchema("PUBLIC").setArgs(key, value)).getAll();
    }
    private static final int N = 10000;
    private static void test(ClientCache<String,Integer> cache) throws InterruptedException {
        // put speed
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            put(cache, "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 {
        ClientConfiguration cfg = new ClientConfiguration().setAddresses("localhost:10800");
        IgniteClient cli = Ignition.startClient(cfg);
        ClientCache<String,Integer> cache = cli.getOrCreateCache(new ClientCacheConfiguration().setName("dummy"));
        cache.query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS performance_cache (ckey VARCHAR(16) NOT NULL, cvalue INTEGER NOT NULL, PRIMARY KEY(ckey))").setSchema("PUBLIC")).getAll();
        for(int i = 0; i < REP; i++) {
            test(cache);
        }
        cli.close();
    }
}
