From 19c596933bbc11da6748ffb3b40fffdc83468e3f Mon Sep 17 00:00:00 2001 From: Wang Guibao Date: Mon, 14 Oct 2019 20:45:49 +0800 Subject: [PATCH] Create get_value.cpp --- doc/resource/get_value.cpp | 129 +++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 doc/resource/get_value.cpp diff --git a/doc/resource/get_value.cpp b/doc/resource/get_value.cpp new file mode 100644 index 00000000..23b329f2 --- /dev/null +++ b/doc/resource/get_value.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include + +#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 get_kvs; + std::vector 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(stop2 - start2).count(); + } + + std::cout << total_request_num << " requests, " << batch_size << " keys per req, total time us = " << time_us < 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; +} -- GitLab