net.thumbtack.test.client.JsonQueryClientThread.java Source code

Java tutorial

Introduction

Here is the source code for net.thumbtack.test.client.JsonQueryClientThread.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 net.thumbtack.test.client;

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

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;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonQueryClientThread extends Thread {

    private static ObjectMapper om = new ObjectMapper();

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

    private final CouchbaseClient client;
    public String[] viewNames = VendorDoc.viewNames;
    public String[] queries = VendorDoc.queries;
    private final long operationCount;
    private final long insertStart;
    private final long insertEnd;
    private final Func func;
    private final int pageSize;

    private List<Long> respTimes = new ArrayList<Long>();

    private final int queryIndex;

    private final int pageNumber;

    interface Func {
        void run() throws Exception;
    }

    public JsonQueryClientThread(CouchbaseClient _client, GlobalConfig config, long _insertStart, long _insertEnd) {
        this.client = _client;
        this.operationCount = config.getOperationCount();
        this.insertStart = _insertStart;
        this.insertEnd = _insertEnd;
        this.pageSize = config.getPageSize();
        this.queryIndex = config.getQueryIndex();
        this.pageNumber = config.getPageNumber();
        OperationType operationType = config.getOperationType();
        switch (operationType) {
        case INSERT: {
            func = new Func() {
                public void run() throws JsonProcessingException {
                    for (long i = insertStart; i < insertEnd; i++) {
                        VendorDoc vendorDoc = new VendorDoc(i);
                        long time = System.currentTimeMillis();
                        client.insert(String.valueOf(i), om.writeValueAsString(vendorDoc));
                        respTimes.add(time - System.currentTimeMillis());
                    }
                }
            };
            break;
        }
        case READ: {
            func = new Func() {
                public void run() {
                    Random randomKey = new Random();
                    for (int i = 0; i < operationCount; i++) {
                        long time = System.currentTimeMillis();
                        client.read(String
                                .valueOf((int) (randomKey.nextDouble() * (insertEnd - insertStart) + insertStart)));
                        respTimes.add(time - System.currentTimeMillis());
                    }
                }
            };
            break;
        }
        case UPDATE: {
            func = new Func() {
                public void run() throws JsonProcessingException {
                    Random randomKey = new Random();
                    for (long i = 0; i < operationCount; i++) {
                        VendorDoc vendorDoc = new VendorDoc(i);
                        long time = System.currentTimeMillis();
                        client.update(
                                String.valueOf(
                                        (int) (randomKey.nextDouble() * (insertEnd - insertStart) + insertStart)),
                                om.writeValueAsString(vendorDoc));
                        respTimes.add(time - System.currentTimeMillis());
                    }
                }
            };
            break;
        }
        case QUERY: {
            func = new Func() {
                public void run() throws JsonProcessingException {
                    Random randomKey = new Random();
                    for (long i = 0; i < operationCount; i++) {
                        int viewIndex = queryIndex != -1 ? queryIndex
                                : (int) (randomKey.nextDouble() * viewNames.length);
                        int page = pageNumber != -1 ? pageNumber : (int) (randomKey.nextDouble() * 100);
                        long timeElasped;
                        Object key = getViewKey(viewIndex);
                        if (viewIndex < 3) {
                            timeElasped = client.searchInView(viewNames[viewIndex], key, true, pageSize, page);
                        } else {
                            timeElasped = client.searchInSortedView(viewNames[viewIndex], key, true, pageSize,
                                    page);
                        }
                        respTimes.add(timeElasped);
                    }
                }
            };
            break;
        }
        case N1QL: {
            func = new Func() {
                public void run() throws JsonProcessingException {
                    Random randomKey = new Random();
                    for (long i = 0; i < operationCount; i++) {
                        int viewIndex = queryIndex != -1 ? queryIndex
                                : (int) (randomKey.nextDouble() * queries.length);
                        long timeElasped = client.searchN1QL(getN1qlQuery(viewIndex) + " limit " + pageSize);
                        respTimes.add(timeElasped);
                    }
                }
            };
            break;
        }
        default: {
            throw new UnsupportedOperationException(operationType.toString());
        }
        }
    }

    protected Object getViewKey(int viewIndex) {
        if (viewIndex == 2) {
            return null;
        }
        if (viewIndex == 9) {
            return "[VendorDoc.randomVendorCode()";
        }
        if (viewNames[viewIndex].startsWith("by_v")) {
            return VendorDoc.randomVendorCode();
        } else {
            return VendorDoc.randomComdCatId();
        }
    }

    protected String getViewVar(int viewIndex) {
        if (viewNames[viewIndex].startsWith("by_v")) {
            return VendorDoc.randomVendorCode();
        } else {
            return VendorDoc.randomComdCatId() + "";
        }
    }

    public String getN1qlQuery(int viewIndex) {
        if (viewIndex == 0) {
            return queries[viewIndex];
        }
        if (viewIndex <= 5) {
            return queries[viewIndex].replace(":vendorCode", "'" + getViewVar(viewIndex) + "'");
        }
        return queries[viewIndex].replace(":comsCategoryId", getViewVar(viewIndex));
    }

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

    public List<Long> getRespTimes() {
        return this.respTimes;
    }
}