com.sumit.test.client.KeyValueClientThread.java Source code

Java tutorial

Introduction

Here is the source code for com.sumit.test.client.KeyValueClientThread.java

Source

/*
 *  Copyright 2014 Jasper Infotech (P) Limited . All Rights Reserved.
 *  JASPER INFOTECH PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *  
 *  @version     1.0, 22-Nov-2014
 *  @author sumit
 */
package com.sumit.test.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sumit.test.factory.IValueFactory;
import com.sumit.test.query.Query;
import com.sumit.test.query.QueryRepo;
import com.sumit.test.service.IKVService;

import net.spy.memcached.compat.log.Logger;
import net.spy.memcached.compat.log.LoggerFactory;
import net.thumbtack.test.config.GlobalConfig;
import net.thumbtack.test.config.OperationType;
import net.thumbtack.test.model.VendorDoc;

public class KeyValueClientThread<T> extends Thread {

    protected static final Logger log = LoggerFactory.getLogger(KeyValueClientThread.class);

    private final long operationCount;
    private final long insertStart;
    private final long insertEnd;

    private Map<OperationType, List<Long>> opRespTimesMap = new HashMap<>();

    public String[] queries = VendorDoc.queries;
    private final int queryIndex;
    private final int pageSize;
    private QueryRepo<? extends Query<T>, T> queryRepo;

    private final IKVService<T> client;
    private final Func func;
    private final IValueFactory<T> valueFactory;
    private Random randomKey = new Random();

    interface Func {
        void run() throws Exception;
    }

    public KeyValueClientThread(IKVService<T> _client, GlobalConfig config, long _insertStart, long _insertEnd,
            IValueFactory<T> _valueFactory, QueryRepo<? extends Query<T>, T> _queryRepo) {
        this.client = _client;
        this.valueFactory = _valueFactory;
        this.operationCount = config.getOperationCount();
        this.insertStart = _insertStart;
        this.insertEnd = _insertEnd;

        this.queryRepo = _queryRepo;
        this.queryIndex = config.getQueryIndex();
        this.pageSize = config.getPageSize();

        OperationType operationType = config.getOperationType();
        opRespTimesMap.put(operationType, new ArrayList<>());
        switch (operationType) {
        case INSERT: {
            func = new Func() {
                public void run() {
                    List<Long> respTimes = opRespTimesMap.get(OperationType.INSERT);
                    for (long i = insertStart; i < insertEnd; i++) {
                        long time = System.currentTimeMillis();
                        client.insert(String.valueOf(i), valueFactory.create(i));
                        respTimes.add(System.currentTimeMillis() - time);
                    }
                }
            };
            break;
        }
        case READ: {
            func = new Func() {
                public void run() {
                    List<Long> respTimes = opRespTimesMap.get(OperationType.READ);
                    for (int i = 0; i < operationCount; i++) {
                        long time = System.currentTimeMillis();
                        T val = client.get(genRandomKey());
                        respTimes.add(System.currentTimeMillis() - time);
                    }
                }
            };
            break;
        }
        case UPDATE: {
            func = new Func() {
                public void run() {
                    List<Long> respTimes = opRespTimesMap.get(OperationType.UPDATE);
                    for (long i = 0; i < operationCount; i++) {
                        long time = System.currentTimeMillis();
                        client.update(genRandomKey(), valueFactory.create(i));
                        respTimes.add(System.currentTimeMillis() - time);
                    }
                }
            };
            break;
        }
        case N1QL: {
            func = new Func() {
                public void run() throws JsonProcessingException {
                    List<Long> respTimes = opRespTimesMap.get(OperationType.N1QL);
                    Random randomKey = new Random();
                    List<? extends Query<T>> queries = queryRepo.getQueries();
                    for (long i = -20; i < operationCount; i++) {
                        int currQueryIndex = queryIndex != -1 ? queryIndex
                                : (int) (randomKey.nextDouble() * queries.size());
                        Query<T> query = queries.get(currQueryIndex);
                        T params = queryRepo.getRandomParams();
                        long time = System.currentTimeMillis();
                        client.executeQuery(query, params, pageSize);
                        if (i > 0) {
                            System.out.println("Query: " + i);
                            respTimes.add(System.currentTimeMillis() - time);
                        }
                    }
                }
            };
            break;
        }
        default: {
            throw new UnsupportedOperationException(operationType.toString());
        }
        }
    }

    private String genRandomKey() {
        return String.valueOf((int) (randomKey.nextDouble() * (insertEnd - insertStart) + insertStart));
    }

    @Override
    public void run() {
        try {
            func.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Map<OperationType, List<Long>> getRespTimes() {
        return this.opRespTimesMap;
    }
}