forked from mrpi/redis-cplusplus-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
140 lines (121 loc) · 3.39 KB
/
Copy pathbenchmark.cpp
File metadata and controls
140 lines (121 loc) · 3.39 KB
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
130
131
132
133
134
135
136
137
138
139
140
#include "functions.h"
#include "../redisclient.h"
void benchmark_mset(redis::client & c, int TEST_SIZE, boost::optional<const string &> opt_val = boost::none)
{
block_duration b("Writing keys with MSET", TEST_SIZE);
redis::client::string_pair_vector keyValuePairs;
for(int i=0; i < TEST_SIZE; i++)
{
stringstream ss;
ss << "key_" << i;
string val = boost::lexical_cast<string>(i);
if(opt_val)
val = *opt_val;
keyValuePairs.push_back( make_pair( ss.str(), val ) );
if( keyValuePairs.size()*val.size() > (1024*1024*16) )
{
c.mset( keyValuePairs );
keyValuePairs.clear();
}
}
c.mset( keyValuePairs );
keyValuePairs.clear();
}
void benchmark_get(redis::client & c, int TEST_SIZE)
{
block_duration b("Reading keys with GET", TEST_SIZE);
redis::client::string_vector keys;
for(int i=0; i < TEST_SIZE; i++)
{
stringstream ss;
ss << "key_" << i;
string val = c.get( ss.str() );
}
}
void benchmark_set(redis::client & c, int TEST_SIZE, boost::optional<const string &> opt_val = boost::none)
{
block_duration b("Writing keys with SET", TEST_SIZE);
redis::client::string_vector keys;
for(int i=0; i < TEST_SIZE; i++)
{
stringstream ss;
ss << "key_" << i;
string val = boost::lexical_cast<string>(i);
if(opt_val)
val = *opt_val;
c.set( ss.str(), val );
}
}
void benchmark_mget(redis::client & c, int TEST_SIZE)
{
block_duration b("Reading keys with MGET", TEST_SIZE);
redis::client::string_vector keys;
for(int i=0; i < TEST_SIZE; i++)
{
stringstream ss;
ss << "key_" << i;
keys.push_back( ss.str() );
if( keys.size() == 250 )
{
redis::client::string_vector out;
c.mget( keys, out );
for(int i1=0; i1 < (int) keys.size(); i1++)
{
//assert( boost::lexical_cast<int>( keys[i1].substr(4) ) == boost::lexical_cast<int>( out[i1] ) );
//ASSERT_EQUAL( boost::lexical_cast<int>( keys[i1].substr(4) ), boost::lexical_cast<int>( out[i1] ) );
}
keys.clear();
}
}
redis::client::string_vector out;
c.mget( keys, out );
for(int i1=0; i1 < (int) keys.size(); i1++)
{
//assert( boost::lexical_cast<int>( keys[i1].substr(4) ) == boost::lexical_cast<int>( out[i1] ) );
//ASSERT_EQUAL( boost::lexical_cast<int>( keys[i1].substr(4) ), boost::lexical_cast<int>( out[i1] ) );
}
keys.clear();
}
void benchmark_incr(redis::client & c, int TEST_SIZE)
{
block_duration dur("Incrementing with shared_int", TEST_SIZE);
// Increment
for(int i=0; i < TEST_SIZE; i++)
{
stringstream ss;
ss << "key_" << i;
redis::distributed_int sh_int(ss.str(), c);
ASSERT_EQUAL( i+1, ++sh_int );
}
}
void benchmark(redis::client & c, int TEST_SIZE)
{
c.flushdb();
benchmark_set (c, TEST_SIZE);
c.flushdb();
benchmark_mset(c, TEST_SIZE);
benchmark_get (c, TEST_SIZE);
benchmark_mget(c, TEST_SIZE);
benchmark_incr(c, TEST_SIZE);
///
/// Large values
///
string val;
for(int i=0; i < 256; i++)
{
val += (char) i;
}
do {
cout << endl;
cout << "====== Value size: " << val.size() << " ======" << endl;
cout << endl;
TEST_SIZE = TEST_SIZE/1.35;
c.flushdb();
benchmark_set (c, TEST_SIZE, val);
c.flushdb();
benchmark_mset(c, TEST_SIZE, val);
benchmark_get (c, TEST_SIZE);
benchmark_mget(c, TEST_SIZE);
val.append(val);
} while( val.size() <= (1024*1024)*2 );
}