get_value.cpp 4.0 KB
Newer Older
W
Wang Guibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <unistd.h>
#include <iostream>
#include <string>
#include <thread>

#include "sw/redis++/redis++.h"

std::string host = "127.0.0.0";
int port = 6379;
std::string auth;
std::string cluster_node;
int cluster_port = 0;
bool benchmark = false;
int key_len = 8;
int val_len = 40;
int total_request_num = 1000;
int thread_num = 1;
int pool_size = 5;
int batch_size = 100;
int key_size = 10000000;        // keys in redis server

W
Wang Guibao 已提交
22 23
std::vector<uint64_t> times_us;

W
Wang Guibao 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
sw::redis::Redis *redis;

int parse_options(int argc, char **argv)
{
    int opt = 0;
    while ((opt = getopt(argc, argv, "h:p:a:n:c:k:v:r:t:b:s:")) != -1) {
        try {
            switch (opt) {
            case 'h':
                host = optarg;
                break;

            case 'p':
                port = std::stoi(optarg);
                break;

            case 'a':
                auth = optarg;
                break;

            case 'n':
                cluster_node = optarg;
                break;

            case 'c':
                cluster_port = std::stoi(optarg);
                break;

            case 'b':
                batch_size = std::stoi(optarg);
                break;

            case 'k':
                key_len = std::stoi(optarg);
                break;

            case 'v':
                val_len = std::stoi(optarg);
                break;

            case 'r':
                total_request_num = std::stoi(optarg);
                break;

            case 't':
                thread_num = std::stoi(optarg);
                break;

            case 's':
                pool_size = std::stoi(optarg);
                break;

            default:
                break;
            }
        } catch (const sw::redis::Error &e) {
            std::cerr << "Unknow option" << std::endl;
        } catch (const std::exception &e) {
            std::cerr << "Invalid command line option" << std::endl;
        }
    }

    return 0;
}

W
Wang Guibao 已提交
89
void thread_worker(int thread_id)
W
Wang Guibao 已提交
90 91 92
{
    // get values
    for (int i = 0; i < total_request_num; ++i) {
W
Wang Guibao 已提交
93 94
        std::vector<std::string> get_kvs;
        std::vector<std::string> get_kvs_res;
W
Wang Guibao 已提交
95

W
Wang Guibao 已提交
96
         for(int j = i * batch_size; j <  (i + 1) * batch_size; j++) {
W
Wang Guibao 已提交
97 98 99
            get_kvs.push_back(std::to_string(i % key_size));
        }
        auto start2 = std::chrono::steady_clock::now();
W
Wang Guibao 已提交
100
        redis->mget(get_kvs.begin(), get_kvs.end(), std::back_inserter(get_kvs_res));
W
Wang Guibao 已提交
101
        auto stop2 = std::chrono::steady_clock::now();
W
Wang Guibao 已提交
102
        times_us[thread_id] += std::chrono::duration_cast<std::chrono::microseconds>(stop2 - start2).count();
W
Wang Guibao 已提交
103 104
    }

W
Wang Guibao 已提交
105
    // Per-thread statistics
W
Wang Guibao 已提交
106 107 108
    std::cout << total_request_num << " requests, " << batch_size << " keys per req, total time us = " << times_us[thread_id] <<std::endl;
    std::cout << "Average " << times_us[thread_id] / total_request_num << "us per req" << std::endl;
    std::cout << "qps: " << (double)total_request_num / times_us[thread_id] * 1000000 << std::endl;
W
Wang Guibao 已提交
109 110 111 112 113 114 115 116 117 118
}

int main(int argc, char **argv)
{
    parse_options(argc, argv);

    std::string connstr = std::string("tcp://") + host + std::string(":") + std::to_string(port);
    redis = new sw::redis::Redis(connstr);

    std::vector<std::thread> workers;
W
Wang Guibao 已提交
119
    times_us.reserve(thread_num);
W
Wang Guibao 已提交
120 121

    for (int i = 0; i < thread_num; ++i) {
W
Wang Guibao 已提交
122 123
        times_us[i] = 0;
        workers.push_back(std::thread(thread_worker, i));
W
Wang Guibao 已提交
124 125 126 127 128
    }

    for (int i = 0; i < thread_num; ++i) {
        workers[i].join();
    }
W
Wang Guibao 已提交
129

W
Wang Guibao 已提交
130
    // times_total_us is average running time of each thread
W
Wang Guibao 已提交
131 132 133 134 135
    uint64_t times_total_us = 0;
    for (int i = 0; i < thread_num; ++i) {
        times_total_us += times_us[i];
    }
    times_total_us /= thread_num;
W
Wang Guibao 已提交
136 137
    
    // Total requests should be sum of requests sent by each thread
W
Wang Guibao 已提交
138 139 140 141 142 143
    total_request_num *= thread_num;

    std::cout << total_request_num << " requests, " << batch_size << " keys per req, total time us = " << times_total_us <<std::endl;
    std::cout << "Average " << times_total_us / total_request_num << "us per req" << std::endl;
    std::cout << "qps: " << (double)total_request_num / times_total_us * 1000000 << std::endl;

W
Wang Guibao 已提交
144 145
    return 0;
}