| 1 |
|
| #!/usr/bin/perl -w |
| 2 |
|
| |
| 3 |
|
| use 5.010; |
| 4 |
|
| use strict; |
| 5 |
|
| use warnings; |
| 6 |
|
| |
| 7 |
|
| use lib qw( |
| 8 |
|
| lib |
| 9 |
|
| t/tlib |
| 10 |
|
| ); |
| 11 |
|
| |
| 12 |
|
| # NAME: Redis::CappedCollection benchmark |
| 13 |
|
| |
| 14 |
|
| use bytes; |
| 15 |
|
| use Carp; |
| 16 |
|
| use Time::HiRes qw( gettimeofday ); |
| 17 |
|
| use Redis '1.976'; |
| 18 |
|
| use List::Util qw( min sum ); |
| 19 |
|
| use Getopt::Long qw( GetOptions ); |
| 20 |
|
| use Data::Dumper; |
| 21 |
|
| |
| 22 |
|
| use Redis::CappedCollection qw( |
| 23 |
|
| $DEFAULT_SERVER |
| 24 |
|
| $DEFAULT_PORT |
| 25 |
|
| ); |
| 26 |
|
| |
| 27 |
|
| # ENVIRONMENT ------------------------------------------------------------------ |
| 28 |
|
| |
| 29 |
|
| #-- declarations --------------------------------------------------------------- |
| 30 |
|
| |
| 31 |
|
| use constant { |
| 32 |
|
| PORTION_TIME => 10, |
| 33 |
|
| INFO_TIME => 60, |
| 34 |
|
| VISITOR_ID_LEN => 20, |
| 35 |
|
| LOST_PRODUCTIVITY => 3, # allowable percentage of lost productivity |
| 36 |
|
| }; |
| 37 |
|
| |
| 38 |
|
| my $host = $DEFAULT_SERVER; |
| 39 |
|
| my $port = $DEFAULT_PORT; |
| 40 |
|
| my $empty_port; |
| 41 |
|
| my $server; |
| 42 |
|
| my $redis; |
| 43 |
|
| my $max_size = 512 * 1024 * 1024; # 512MB |
| 44 |
|
| my $cleanup_bytes = 0; |
| 45 |
|
| my $cleanup_items = 0; |
| 46 |
|
| my $max_lists = 2_000_000; |
| 47 |
|
| my $data_len = 200; |
| 48 |
|
| my $run_time = 0; |
| 49 |
|
| my $receive = 0; |
| 50 |
|
| my $dump = 0; |
| 51 |
|
| |
| 52 |
|
| my %bench = (); |
| 53 |
|
| my $help = 0; |
| 54 |
|
| my $work_exit = 0; |
| 55 |
|
| my $coll; |
| 56 |
|
| my $coll_name = $$.''; |
| 57 |
|
| my $visitors = 0; |
| 58 |
|
| my $rate = 0; |
| 59 |
|
| |
| 60 |
|
| my $final_message = 'The End!'; |
| 61 |
|
| |
| 62 |
|
| #-- setting up facilities |
| 63 |
|
| |
| 64 |
|
| my $ret = GetOptions( |
| 65 |
|
| 'host=s' => \$host, |
| 66 |
|
| 'port=i' => \$port, |
| 67 |
|
| 'coll_name=s' => \$coll_name, |
| 68 |
|
| 'max_size=i' => \$max_size, |
| 69 |
|
| 'cleanup_bytes=i' => \$cleanup_bytes, |
| 70 |
|
| 'cleanup_items=i' => \$cleanup_items, |
| 71 |
|
| 'data_len=i' => \$data_len, |
| 72 |
|
| 'visitors=i' => \$max_lists, |
| 73 |
|
| 'run_time=i' => \$run_time, |
| 74 |
|
| 'rate=f' => \$rate, |
| 75 |
|
| 'receive' => \$receive, |
| 76 |
|
| 'dump=i' => \$dump, |
| 77 |
|
| "help|?" => \$help, |
| 78 |
|
| ); |
| 79 |
|
| |
| 80 |
|
| if ( !$ret || $help ) |
| 81 |
|
| { |
| 82 |
|
| print <<'HELP'; |
| 83 |
|
| Usage: $0 [--host="..."] [--port=...] [--coll_name="..."] [--max_size=...] [--cleanup_bytes=...] [--cleanup_items=...] [--data_len=...] [--visitors=...] [--run_time=...] [--rate=...] [--dump=...] [--receive] [--help] |
| 84 |
|
| |
| 85 |
|
| Start a Redis client, connect to the Redis server, randomly inserts or receives data |
| 86 |
|
| |
| 87 |
|
| Options: |
| 88 |
|
| --help |
| 89 |
|
| Display this help and exit. |
| 90 |
|
| |
| 91 |
|
| --host="..." |
| 92 |
|
| The server should contain an IP address of Redis server. |
| 93 |
|
| If not provided, '127.0.0.1' is used as the default. |
| 94 |
|
| --port=N |
| 95 |
|
| The server port. |
| 96 |
|
| Default 6379 (the default for Redis server). |
| 97 |
|
| --coll_name="..." |
| 98 |
|
| Collection name. |
| 99 |
|
| Default = process pid. |
| 100 |
|
| To be used along with '--receive' option. |
| 101 |
|
| --max_size=N |
| 102 |
|
| The maximum size (bytes) of capped collection data volume. |
| 103 |
|
| Default 512MB. |
| 104 |
|
| --cleanup_bytes=N |
| 105 |
|
| Size (bytes) of the data to be freed should size of the collection exceed 'max_size'. |
| 106 |
|
| Default 0 - no advance cleanup performed. |
| 107 |
|
| --cleanup_items=N |
| 108 |
|
| Number of elements to be removed from collection during advance cleanup. |
| 109 |
|
| Default 0 - no advance cleanup performed. |
| 110 |
|
| --data_len=... |
| 111 |
|
| Data unit size. |
| 112 |
|
| Default 200 bytes. |
| 113 |
|
| --visitors=N |
| 114 |
|
| The number of lists to be created. |
| 115 |
|
| Default 2_000_000. |
| 116 |
|
| --run_time=... |
| 117 |
|
| Maximum run time in seconds |
| 118 |
|
| Default 0 - unlimited. |
| 119 |
|
| --rate=N |
| 120 |
|
| Exponentially distribute data using provided RATE. |
| 121 |
|
| Default 0 - no exponental distribution. |
| 122 |
|
| --dump=N |
| 123 |
|
| Data dump volume. |
| 124 |
|
| Default 0 - do not perform dump. |
| 125 |
|
| WARNING: removes all keys from the current database. |
| 126 |
|
| --receive |
| 127 |
|
| Receive data instead of writing. |
| 128 |
|
| HELP |
| 129 |
|
| exit 1; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| my $item = '*' x $data_len; |
| 133 |
|
| my $upper = 0; |
| 134 |
|
| $upper = exponential( 0.99999 ) if $rate; |
| 135 |
|
| my $no_line = 0; |
| 136 |
|
| |
| 137 |
|
| $| = 1; |
| 138 |
|
| |
| 139 |
|
| #-- definition of the functions |
| 140 |
|
| |
| 141 |
|
| $SIG{INT} = \&tsk; |
| 142 |
|
| |
| 143 |
|
| sub tsk { |
| 144 |
|
| $SIG{INT} = "IGNORE"; |
| 145 |
|
| $final_message = 'Execution is interrupted!'; |
| 146 |
|
| ++$work_exit; |
| 147 |
|
| return; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| sub client_info { |
| 151 |
|
| my $redis_info = $redis->info; |
| 152 |
|
| print "WARNING: Do not forget to manually delete the test data.\n"; |
| 153 |
|
| print '-' x 78, "\n"; |
| 154 |
|
| print "CLIENT INFO:\n", |
| 155 |
|
| "server $server", "\n", |
| 156 |
|
| "redis_version $redis_info->{redis_version}", "\n", |
| 157 |
|
| "arch_bits $redis_info->{arch_bits}", "\n", |
| 158 |
|
| "VISITOR_ID_LEN ", VISITOR_ID_LEN, "\n", |
| 159 |
|
| "PORTION_TIME ", PORTION_TIME, "\n", |
| 160 |
|
| "INFO_TIME ", INFO_TIME, "\n", |
| 161 |
|
| "coll_name $coll_name", "\n", |
| 162 |
|
| "data_len $data_len", "\n", |
| 163 |
|
| "maxmemory-policy ", ( $redis->config_get( 'maxmemory-policy' ) )[1], "\n", |
| 164 |
|
| "maxmemory ", ( $redis->config_get( 'maxmemory' ) )[1], "\n", |
| 165 |
|
| "max_size $max_size", "\n", |
| 166 |
|
| "cleanup_bytes $cleanup_bytes", "\n", |
| 167 |
|
| "cleanup_items $cleanup_items", "\n", |
| 168 |
|
| "max_lists $max_lists", "\n", |
| 169 |
|
| "rate $rate", "\n", |
| 170 |
|
| "dump $dump", "\n", |
| 171 |
|
| "run_time $run_time", "\n", |
| 172 |
|
| '-' x 78, "\n", |
| 173 |
|
| ; |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
|
| sub redis_info { |
| 177 |
|
| my $redis_info = $redis->info; |
| 178 |
|
| my $rss = $redis_info->{used_memory_rss}; |
| 179 |
|
| my $short_rss = ( $rss > 1024 * 1024 * 1024 ) ? |
| 180 |
|
| $rss / ( 1024 * 1024 * 1024 ) |
| 181 |
|
| : $rss / ( 1024 * 1024 ); |
| 182 |
|
| my $info = $coll->collection_info; |
| 183 |
|
| |
| 184 |
|
| print |
| 185 |
|
| "\n", |
| 186 |
|
| '-' x 78, |
| 187 |
|
| "\n", |
| 188 |
|
| "data lists $info->{lists}\n", |
| 189 |
|
| "data items $info->{items}\n", |
| 190 |
|
| "mem_fragmentation_ratio $redis_info->{mem_fragmentation_ratio}\n", |
| 191 |
|
| "used_memory_human $redis_info->{used_memory_human}\n", |
| 192 |
|
| "used_memory_rss ", sprintf( "%.2f", $short_rss ), ( $rss > 1024 * 1024 * 1024 ) ? 'G' : 'M', "\n", |
| 193 |
|
| "used_memory_peak_human $redis_info->{used_memory_peak_human}\n", |
| 194 |
|
| "used_memory_lua $redis_info->{used_memory_lua}\n", |
| 195 |
|
| "db0 ", $redis_info->{db0} || "", "\n", |
| 196 |
|
| '-' x 78, |
| 197 |
|
| "\n", |
| 198 |
|
| ; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
|
| # Send a request to Redis |
| 202 |
|
| sub call_redis { |
| 203 |
|
| my $method = shift; |
| 204 |
|
| |
| 205 |
|
| my @return = $redis->$method( @_ ); |
| 206 |
|
| |
| 207 |
|
| return wantarray ? @return : $return[0]; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
|
| sub exponential |
| 211 |
|
| { |
| 212 |
|
| my $x = shift // rand; |
| 213 |
|
| return log(1 - $x) / -$rate; |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
|
| # Outputs random value in [0..LIMIT] range exponentially |
| 217 |
|
| # distributed with RATE. The higher is rate, the more skewed is |
| 218 |
|
| # the distribution (try rates from 0.1 to 3 to see how it changes). |
| 219 |
|
| sub get_exponentially_id { |
| 220 |
|
| # exponential distribution may theoretically produce big numbers |
| 221 |
|
| # (with low probability though) so we have to make an upper limit |
| 222 |
|
| # and treat all randoms greater than limit to be equal to it. |
| 223 |
|
| |
| 224 |
|
| my $r = exponential(); # exponentially distributed floating random |
| 225 |
|
| $r = $upper if $r > $upper; |
| 226 |
|
| return int( $r / $upper * ( $max_lists - 1 ) ); # convert to integer in (0..limit) range |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
|
| sub measurement_info { |
| 230 |
|
| my $measurement = shift; |
| 231 |
|
| |
| 232 |
|
| print |
| 233 |
|
| sprintf( "mean %.2f op/sec, mem %7s, rate has fallen by ", |
| 234 |
|
| $measurement->{mean}, |
| 235 |
|
| $measurement->{used_memory}, |
| 236 |
|
| ), |
| 237 |
|
| $measurement->{fallen} eq 'N/A' ? 'N/A' : sprintf( "%.2f%%", $measurement->{fallen} ), |
| 238 |
|
| "\n"; |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
|
| sub _get_value { |
| 242 |
|
| my $coll = shift; |
| 243 |
|
| my $key = shift; |
| 244 |
|
| my $type = shift; |
| 245 |
|
| |
| 246 |
|
| return $coll->_call_redis( 'GET', $key ) if $type eq 'string'; |
| 247 |
|
| return $coll->_call_redis( 'LRANGE', $key, 0, -1 ) if $type eq 'list'; |
| 248 |
|
| return $coll->_call_redis( 'SMEMBERS', $key ) if $type eq 'set'; |
| 249 |
|
| |
| 250 |
|
| if ( $type eq 'zset' ) { |
| 251 |
|
| my %hash; |
| 252 |
|
| my @zsets = $coll->_call_redis( 'ZRANGE', $key, 0, -1, 'WITHSCORES' ); |
| 253 |
|
| for ( my $loop = 0; $loop < scalar( @zsets ) / 2; $loop++ ) |
| 254 |
|
| { |
| 255 |
|
| my $value = $zsets[ $loop * 2 ]; |
| 256 |
|
| my $score = $zsets[ ( $loop * 2 ) + 1 ]; |
| 257 |
|
| $hash{ $score } = $value; |
| 258 |
|
| } |
| 259 |
|
| return [ {%hash} ]; |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
|
| if ( $type eq 'hash' ) { |
| 263 |
|
| my %hash; |
| 264 |
|
| foreach my $item ( $coll->_call_redis( 'HKEYS', $key ) ) |
| 265 |
|
| { |
| 266 |
|
| $hash{ $item } = $coll->_call_redis( 'HGET', $key, $item ); |
| 267 |
|
| } |
| 268 |
|
| return {%hash}; |
| 269 |
|
| } |
| 270 |
|
| } |
| 271 |
|
| |
| 272 |
|
| sub redis_dump { |
| 273 |
|
| my $coll = shift; |
| 274 |
|
| |
| 275 |
|
| my $redis_info = $redis->info; |
| 276 |
|
| my %keys; |
| 277 |
|
| |
| 278 |
|
| printf( "used_memory %dB / %s\n", $redis_info->{used_memory}, $redis_info->{used_memory_human} ); |
| 279 |
|
| |
| 280 |
|
| foreach my $key ( $coll->_call_redis( 'KEYS', '*' ) ) { |
| 281 |
|
| my $type = $coll->_call_redis( 'TYPE', $key ); |
| 282 |
|
| my $show_name = $key; |
| 283 |
|
| my $encoding = $coll->_call_redis( 'OBJECT', 'ENCODING', $key ); |
| 284 |
|
| my $refcount = $coll->_call_redis( 'OBJECT', 'REFCOUNT', $key ); |
| 285 |
|
| $show_name .= " ($type/$encoding: $refcount)"; |
| 286 |
|
| |
| 287 |
|
| $keys{ $show_name } = _get_value( $coll, $key, $type ); |
| 288 |
|
| } |
| 289 |
|
| return \%keys; |
| 290 |
|
| } |
| 291 |
|
| |
| 292 |
|
| # INSTRUCTIONS ----------------------------------------------------------------- |
| 293 |
|
| |
| 294 |
|
| $server = "$host:$port"; |
| 295 |
|
| $redis = Redis->new( server => $server ); |
| 296 |
|
| my $current_maxmemory_policy = ( $redis->config_get( 'maxmemory-policy' ) )[1]; |
| 297 |
|
| my $current_maxmemory = ( $redis->config_get( 'maxmemory' ) )[1]; |
| 298 |
|
| |
| 299 |
|
| $redis->config_set( maxmemory => $max_size ); |
| 300 |
|
| $redis->config_set( 'maxmemory-policy' => 'noeviction' ); |
| 301 |
|
| |
| 302 |
|
| $coll = Redis::CappedCollection->create( |
| 303 |
|
| redis => $redis, |
| 304 |
|
| name => $coll_name, |
| 305 |
|
| $receive ? () : ( |
| 306 |
|
| cleanup_bytes => $cleanup_bytes, |
| 307 |
|
| cleanup_items => $cleanup_items, |
| 308 |
|
| ), |
| 309 |
|
| ); |
| 310 |
|
| |
| 311 |
|
| client_info(); |
| 312 |
|
| |
| 313 |
|
| if ( $receive and $coll_name eq $$.'' ) |
| 314 |
|
| { |
| 315 |
|
| print "There is no data to be read\n"; |
| 316 |
|
| goto THE_END; |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
|
| my @measurements = (); |
| 320 |
|
| |
| 321 |
|
| $redis->flushall if $dump; |
| 322 |
|
| |
| 323 |
|
| my $measurement = { |
| 324 |
|
| speed => [], |
| 325 |
|
| mean => 0, |
| 326 |
|
| fallen => 0, |
| 327 |
|
| used_memory => '', |
| 328 |
|
| }; |
| 329 |
|
| |
| 330 |
|
| my $list_id; |
| 331 |
|
| my ( $secs, $count ) = ( 0, 0 ); |
| 332 |
|
| my ( $time_before, $time_after ); |
| 333 |
|
| my ( $start_time, $last_stats_reports_time, $last_info_reports_time ); |
| 334 |
|
| |
| 335 |
|
| my $data_id = 0; |
| 336 |
|
| |
| 337 |
|
| $start_time = $last_stats_reports_time = $last_info_reports_time = time; |
| 338 |
|
| while ( !$work_exit ) |
| 339 |
|
| { |
| 340 |
|
| if ( $run_time ) { last if gettimeofday - $start_time > $run_time; } |
| 341 |
|
| last if $work_exit; |
| 342 |
|
| |
| 343 |
|
| my $id = ( !$receive && $rate ) ? get_exponentially_id() : int( rand $max_lists ); |
| 344 |
|
| $list_id = sprintf( '%0'.VISITOR_ID_LEN.'d', $id ); |
| 345 |
|
| $time_before = gettimeofday; |
| 346 |
|
| my @ret = $receive ? $coll->receive( $list_id ) : $coll->insert( $list_id, $data_id++, $item ); |
| 347 |
|
| $time_after = gettimeofday; |
| 348 |
|
| $secs += $time_after - $time_before; |
| 349 |
|
| ++$count; |
| 350 |
|
| |
| 351 |
|
| if ( $dump and $count >= $dump ) |
| 352 |
|
| { |
| 353 |
|
| my $dump = redis_dump( $coll ); |
| 354 |
|
| $Data::Dumper::Sortkeys = 1; |
| 355 |
|
| print Dumper( $dump ); |
| 356 |
|
| goto THE_END; |
| 357 |
|
| } |
| 358 |
|
| |
| 359 |
|
| my $redis_info = $redis->info; |
| 360 |
|
| $measurement->{used_memory} = $redis_info->{used_memory_human}, |
| 361 |
|
| |
| 362 |
|
| my $time_from_stats = $time_after - $last_stats_reports_time; |
| 363 |
|
| if ( $time_from_stats > PORTION_TIME ) |
| 364 |
|
| { |
| 365 |
|
| my $speed = int( $count / $secs ); |
| 366 |
|
| print '[', scalar localtime, '] ', |
| 367 |
|
| $receive ? 'reads' : 'inserts', ', ', |
| 368 |
|
| $secs ? sprintf( '%d', $speed ) : 'N/A', ' op/sec ', |
| 369 |
|
| ' ' x 5, "\r"; |
| 370 |
|
| |
| 371 |
|
| if ( gettimeofday - $last_info_reports_time > INFO_TIME ) |
| 372 |
|
| { |
| 373 |
|
| redis_info(); |
| 374 |
|
| $last_info_reports_time = gettimeofday; |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
|
| $secs = 0; |
| 378 |
|
| $count = 0; |
| 379 |
|
| $last_stats_reports_time = gettimeofday; |
| 380 |
|
| } |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
|
| goto THE_FINISH if $work_exit; |
| 384 |
|
| |
| 385 |
|
| # POSTCONDITIONS --------------------------------------------------------------- |
| 386 |
|
| |
| 387 |
|
| THE_FINISH: |
| 388 |
|
| |
| 389 |
|
| print |
| 390 |
|
| "\n", |
| 391 |
|
| $final_message, |
| 392 |
|
| "\n", |
| 393 |
|
| ; |
| 394 |
|
| THE_END: |
| 395 |
|
| $redis->config_set( maxmemory => $current_maxmemory ); |
| 396 |
|
| $redis->config_set( 'maxmemory-policy' => $current_maxmemory_policy ); |
| 397 |
|
| $redis->quit; |
| 398 |
|
| |
| 399 |
|
| exit; |