get_value.cpp 3.2 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 22 23 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#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

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;
}

void thread_worker()
{
    // get values
    std::vector<std::string> get_kvs;
    std::vector<std::string> get_kvs_res;
    get_kvs_res.reserve(total_request_num);
    uint64_t time_us = 0;

    for (int i = 0; i < total_request_num; ++i) {
        get_kvs.clear();
        get_kvs_res.clear();

        for(int j = i * batch_size; j <  (i + 1) * batch_size; j++) {
            get_kvs.push_back(std::to_string(i % key_size));
        }
        auto start2 = std::chrono::steady_clock::now();
        redis->mget(get_kvs.begin(), get_kvs.end(), get_kvs_res.begin());
        auto stop2 = std::chrono::steady_clock::now();
        time_us += std::chrono::duration_cast<std::chrono::microseconds>(stop2 - start2).count();
    }

    std::cout << total_request_num << " requests, " << batch_size << " keys per req, total time us = " << time_us <<std::endl;
    std::cout << "Average " << time_us / total_request_num << "us per req" << std::endl;
}

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;

    for (int i = 0; i < thread_num; ++i) {
        workers.push_back(std::thread(thread_worker));
    }

    for (int i = 0; i < thread_num; ++i) {
        workers[i].join();
    }
    return 0;
}