| 1 |
#!/usr/bin/perl -w |
| 2 |
#/**************************************************************************/ |
| 3 |
#/* */ |
| 4 |
#/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */ |
| 5 |
#/* */ |
| 6 |
#/* NXSCRIPTS, NX protocol compression and NX extensions to this software */ |
| 7 |
#/* are copyright of the aforementioned persons and companies. */ |
| 8 |
#/* */ |
| 9 |
#/* Redistribution and use of the present software is allowed according */ |
| 10 |
#/* to terms specified in the file LICENSE.nxcomp which comes in the */ |
| 11 |
#/* source distribution. */ |
| 12 |
#/* */ |
| 13 |
#/* All rights reserved. */ |
| 14 |
#/* */ |
| 15 |
#/* NOTE: This software has received contributions from various other */ |
| 16 |
#/* contributors, only the core maintainers and supporters are listed as */ |
| 17 |
#/* copyright holders. Please contact us, if you feel you should be listed */ |
| 18 |
#/* as copyright holder, as well. */ |
| 19 |
#/* */ |
| 20 |
#/**************************************************************************/ |
| 21 |
|
| 22 |
use strict; |
| 23 |
use Getopt::Long; |
| 24 |
use IO::Socket; |
| 25 |
use Time::HiRes qw(gettimeofday tv_interval sleep ); |
| 26 |
use IO::Socket::INET; |
| 27 |
|
| 28 |
my ($opt_batch, $opt_count, $opt_host, $opt_port, $opt_debug, $opt_blocksize, $opt_dump, $opt_delay); |
| 29 |
my ($cmd_echo, $cmd_pingbench, $cmd_rand_read_bench, $cmd_fast_read_bench, $cmd_fast_write_bench); |
| 30 |
my ($cmd_rand_write_bench, $cmd_fast_echo_bench, $cmd_rand_echo_bench, $cmd_all_bench); |
| 31 |
my ($cmd_char_write_bench, $cmd_char_read_bench); |
| 32 |
my ($cmd_help); |
| 33 |
|
| 34 |
|
| 35 |
$opt_host = "127.0.0.1"; |
| 36 |
$opt_count = 10000; |
| 37 |
$opt_blocksize = 1024; |
| 38 |
$opt_delay = 0; |
| 39 |
|
| 40 |
Getopt::Long::Configure ("bundling"); |
| 41 |
|
| 42 |
GetOptions( |
| 43 |
"d|dump" => \$opt_dump, |
| 44 |
"c|count=i" => \$opt_count, |
| 45 |
"H|host=s" => \$opt_host, |
| 46 |
"P|port=i" => \$opt_port, |
| 47 |
"D|debug" => \$opt_debug, |
| 48 |
"e|delay=f" => \$opt_delay, |
| 49 |
"b|blocksize=i" => \$opt_blocksize, |
| 50 |
"E|echo=s" => \$cmd_echo, |
| 51 |
"pingbench" => \$cmd_pingbench, |
| 52 |
"randreadbench" => \$cmd_rand_read_bench, |
| 53 |
"fastreadbench" => \$cmd_fast_read_bench, |
| 54 |
"fastwritebench" => \$cmd_fast_write_bench, |
| 55 |
"randwritebench" => \$cmd_rand_write_bench, |
| 56 |
"fastechobench" => \$cmd_fast_echo_bench, |
| 57 |
"randechobench" => \$cmd_rand_echo_bench, |
| 58 |
"charwritebench" => \$cmd_char_write_bench, |
| 59 |
"charreadbench" => \$cmd_char_read_bench, |
| 60 |
"a|allbench" => \$cmd_all_bench, |
| 61 |
"h|help" => \$cmd_help, |
| 62 |
) or die "Getopt failed"; |
| 63 |
|
| 64 |
|
| 65 |
if ($cmd_help) { |
| 66 |
print <<HELP; |
| 67 |
Usage: $0 --port <port> [options] <command> |
| 68 |
NX Slave Channel demo, benchmark and tester. |
| 69 |
|
| 70 |
Options: |
| 71 |
-b, --blocksize=INT Block size for testing. 1024 bytes by default. |
| 72 |
-c, --count=NUM Number of blocks or pings to issue. |
| 73 |
-D, --debug Output protocol data for debugging |
| 74 |
-d, --dump Dump benchmark data in tab separated format, for |
| 75 |
graphing. |
| 76 |
-e, --delay=FLOAT Delay between blocks or pings. None by default. |
| 77 |
-H, --host=HOST Host to connect to. 'localhost' by default. |
| 78 |
-P, --port=PORT Port to connect to. Mandatory. |
| 79 |
|
| 80 |
Benchmarks: |
| 81 |
-a, --allbench Run all the benchmarks |
| 82 |
--fastechobench Benchmark sending a single repeated character, |
| 83 |
and receiving it back. |
| 84 |
--fastreadbench Benchmark reading a single repeated character. |
| 85 |
--fastwritebench Benchmark writing a single repeated character. |
| 86 |
--pingbench Benchmark ping time. |
| 87 |
--randechobench Benchmark sending random data, and receiving it |
| 88 |
back |
| 89 |
--randreadbench Benchmark reading random, incompressible data. |
| 90 |
--randwritebench Benchmark writing random, incompressible data. |
| 91 |
|
| 92 |
Other commands: |
| 93 |
-E, --echo=STR Send STR to the slave channel handler, and print |
| 94 |
the response. |
| 95 |
-h, --help Show this text |
| 96 |
|
| 97 |
Example: |
| 98 |
Test basic connectivity: |
| 99 |
$0 --port 42000 --echo "hi" |
| 100 |
|
| 101 |
Connect to port 42000 and run all the benchmarks: |
| 102 |
$0 --port 42000 -a |
| 103 |
|
| 104 |
HELP |
| 105 |
exit(0); |
| 106 |
} |
| 107 |
|
| 108 |
if (!$opt_port) { |
| 109 |
print STDERR "Syntax: $0 --port <port> <command> [arguments]\nUse $0 --help for more information.\n\n"; |
| 110 |
exit(1); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
my $socket = IO::Socket::INET->new(PeerAddr => $opt_host, PeerPort => $opt_port, Proto => 'tcp'); |
| 115 |
if (!$socket) { |
| 116 |
die "Can't connect to $opt_host:$opt_port: $!"; |
| 117 |
} |
| 118 |
|
| 119 |
my @greeting = read_until_prompt(); |
| 120 |
my $sl = StatusLine->new(); |
| 121 |
my $random_fh; |
| 122 |
|
| 123 |
if ( $cmd_all_bench ) { |
| 124 |
$cmd_pingbench = 1; |
| 125 |
$cmd_rand_read_bench = 1; |
| 126 |
$cmd_fast_read_bench = 1; |
| 127 |
$cmd_fast_write_bench = 1; |
| 128 |
$cmd_rand_write_bench = 1; |
| 129 |
$cmd_fast_echo_bench = 1; |
| 130 |
$cmd_rand_echo_bench = 1; |
| 131 |
$cmd_all_bench = 1; |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
if ( $cmd_echo ) { |
| 136 |
send_cmd("echo $cmd_echo"); |
| 137 |
print read_until_prompt() . "\n"; |
| 138 |
} |
| 139 |
|
| 140 |
if ( $cmd_pingbench ) { |
| 141 |
my $t0 = [gettimeofday()]; |
| 142 |
for(my $i=0;$i<$opt_count;$i++) { |
| 143 |
send_cmd("echo $i"); |
| 144 |
read_until_prompt(); |
| 145 |
|
| 146 |
my $elapsed = tv_interval($t0, [gettimeofday()]); |
| 147 |
if ( $opt_dump ) { |
| 148 |
print "$elapsed\t$i\n"; |
| 149 |
} else { |
| 150 |
$sl->set("Pinged " . ($i+1) . " times, ${elapsed}s elapsed, " . $opt_count / $elapsed . "/s"); |
| 151 |
} |
| 152 |
|
| 153 |
sleep($opt_delay) if ($opt_delay>0); |
| 154 |
} |
| 155 |
|
| 156 |
$sl->show_last(); |
| 157 |
print STDERR "\n\n"; |
| 158 |
} |
| 159 |
|
| 160 |
if ( $cmd_rand_read_bench ) { |
| 161 |
read_bench("Random read", "randgen"); |
| 162 |
} |
| 163 |
|
| 164 |
if ( $cmd_fast_read_bench ) { |
| 165 |
read_bench("Fast read", "fastgen"); |
| 166 |
} |
| 167 |
|
| 168 |
if ( $cmd_fast_write_bench ) { |
| 169 |
write_bench("Fast write", "discard", sub { "x" x $opt_blocksize }, 0); |
| 170 |
} |
| 171 |
|
| 172 |
if ( $cmd_rand_write_bench ) { |
| 173 |
write_bench("Random write", "discard", \&get_random_bytes, 0); |
| 174 |
} |
| 175 |
|
| 176 |
if ( $cmd_fast_echo_bench ) { |
| 177 |
write_bench("Fast echo", "blkecho $opt_blocksize", sub { "x" x $opt_blocksize }, 1); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if ( $cmd_rand_echo_bench ) { |
| 182 |
write_bench("Random echo", "blkecho $opt_blocksize", \&get_random_bytes, 1); |
| 183 |
} |
| 184 |
|
| 185 |
if ( $cmd_char_write_bench ) { |
| 186 |
for(my $i=0;$i<=255;$i++) { |
| 187 |
write_bench("Fast write $i", "discard", sub { chr($i) x $opt_blocksize }, 0); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if ( $cmd_char_read_bench ) { |
| 192 |
for(my $i=0;$i<=255;$i++) { |
| 193 |
read_bench("Fast read $i", "fastgen $i"); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
sub get_random_bytes { |
| 198 |
if (!$random_fh) { |
| 199 |
open($random_fh, '<', "/dev/urandom") or die "Can't open /dev/urandom: $!"; |
| 200 |
} |
| 201 |
|
| 202 |
my $buf=""; |
| 203 |
while(length($buf) < $opt_blocksize) { |
| 204 |
my $tmp; |
| 205 |
sysread($random_fh, $tmp, $opt_blocksize - length($buf)); |
| 206 |
$buf .= $tmp; |
| 207 |
} |
| 208 |
|
| 209 |
return $buf; |
| 210 |
} |
| 211 |
|
| 212 |
sub read_bench { |
| 213 |
my ($desc, $command) = @_; |
| 214 |
init(); |
| 215 |
send_cmd($command); |
| 216 |
|
| 217 |
my $t0 = [gettimeofday()]; |
| 218 |
my $bytes = 0; |
| 219 |
|
| 220 |
while($bytes < $opt_count * $opt_blocksize) { |
| 221 |
my $junk = ""; |
| 222 |
while(length($junk) < $opt_blocksize) { |
| 223 |
$junk .= read_sock($opt_blocksize - length($junk)); |
| 224 |
} |
| 225 |
|
| 226 |
$bytes += length($junk); |
| 227 |
|
| 228 |
my $elapsed = tv_interval($t0, [gettimeofday()]); |
| 229 |
if ( $opt_dump ) { |
| 230 |
print "$elapsed\t$bytes\n"; |
| 231 |
} else { |
| 232 |
$sl->set("$desc $bytes bytes, ${elapsed}s elapsed, " . sprintf("%0.3f", ($bytes / $elapsed) / (1024*1024)) . " MB/s"); |
| 233 |
} |
| 234 |
|
| 235 |
sleep($opt_delay) if ($opt_delay>0); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
$sl->show_last(); |
| 240 |
print STDERR "\n\n"; |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
sub write_bench { |
| 245 |
my ($desc, $command, $generator, $do_read) = @_; |
| 246 |
init(); |
| 247 |
send_cmd($command); |
| 248 |
|
| 249 |
my $t0 = [gettimeofday()]; |
| 250 |
my $bytes = 0; |
| 251 |
|
| 252 |
while($bytes < $opt_count * $opt_blocksize) { |
| 253 |
|
| 254 |
my $junk = $generator->(); |
| 255 |
$bytes += length($junk); |
| 256 |
|
| 257 |
write_sock($junk); |
| 258 |
|
| 259 |
if ( $do_read ) { |
| 260 |
my $readbuf = ""; |
| 261 |
while(length($readbuf) < $opt_blocksize) { |
| 262 |
$readbuf .= read_sock($opt_blocksize-length($readbuf)); |
| 263 |
} |
| 264 |
|
| 265 |
if ( $junk ne $readbuf ) { |
| 266 |
die "Agent returned different data! Sent:\n$junk\nReceived:\n$readbuf\n"; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
my $elapsed = tv_interval($t0, [gettimeofday()]); |
| 271 |
if ( $opt_dump ) { |
| 272 |
print "$elapsed\t$bytes\n"; |
| 273 |
} else { |
| 274 |
$sl->set("$desc $bytes bytes, ${elapsed}s elapsed, " . sprintf("%0.3f", ($bytes / $elapsed) / (1024*1024)) . " MB/s"); |
| 275 |
} |
| 276 |
|
| 277 |
sleep($opt_delay) if ($opt_delay>0); |
| 278 |
} |
| 279 |
|
| 280 |
$sl->show_last(); |
| 281 |
print STDERR "\n\n"; |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
sub read_until_prompt { |
| 288 |
my $buf; |
| 289 |
my $tmp; |
| 290 |
my @lines; |
| 291 |
my $ret = ""; |
| 292 |
|
| 293 |
while(1) { |
| 294 |
$buf .= read_sock(1024); |
| 295 |
|
| 296 |
while ( $buf =~ /^(.*?)\n/m ) { |
| 297 |
dbg("LINE: '$1'\n"); |
| 298 |
push @lines, $1; |
| 299 |
$ret .= "$1\n"; |
| 300 |
$buf =~ s/^(.*?)\n//m; |
| 301 |
} |
| 302 |
|
| 303 |
dbg("BUF: '$buf'\n"); |
| 304 |
if ( $buf =~ /^(Agent|Proxy|\?)> / ) { |
| 305 |
dbg("PROMPT: '$buf'\n"); |
| 306 |
return wantarray ? @lines : $ret; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
sub init { |
| 312 |
if ( $socket ) { |
| 313 |
close $socket; |
| 314 |
} |
| 315 |
|
| 316 |
$socket = IO::Socket::INET->new(PeerAddr => $opt_host, PeerPort => $opt_port, Proto => 'tcp'); |
| 317 |
|
| 318 |
if (!$socket) { |
| 319 |
die "Can't connect to $opt_host:$opt_port: $!"; |
| 320 |
} |
| 321 |
|
| 322 |
my @greeting = read_until_prompt(); |
| 323 |
} |
| 324 |
|
| 325 |
sub read_sock { |
| 326 |
my ($len) = @_; |
| 327 |
my $buf; |
| 328 |
my $ret = sysread($socket, $buf, $len); |
| 329 |
if (!defined $ret) { |
| 330 |
die "Error reading $len bytes from socket: $!"; |
| 331 |
} |
| 332 |
|
| 333 |
if ( $ret == 0 ) { |
| 334 |
die "Socket unexpectedly closed when trying to read $len bytes"; |
| 335 |
} |
| 336 |
|
| 337 |
dbg("READ: '$buf', length $ret\n"); |
| 338 |
return $buf; |
| 339 |
} |
| 340 |
|
| 341 |
sub write_sock { |
| 342 |
my ($data) = @_; |
| 343 |
dbg("SEND: '$data'\n"); |
| 344 |
|
| 345 |
my $written = 0; |
| 346 |
my $total = length($data); |
| 347 |
|
| 348 |
while($written < $total) { |
| 349 |
my $ret = syswrite($socket, $data, $total, $written); |
| 350 |
|
| 351 |
if (!$ret) { |
| 352 |
die "Error writing '$data' to socket: $!"; |
| 353 |
} |
| 354 |
if ( $ret == 0 ) { |
| 355 |
die "Socket closed when writing '$data' to socket"; |
| 356 |
} |
| 357 |
|
| 358 |
$written += $ret; |
| 359 |
} |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
sub send_cmd { |
| 365 |
my ($cmd) = @_; |
| 366 |
write_sock("$cmd\n"); |
| 367 |
} |
| 368 |
|
| 369 |
sub dbg { |
| 370 |
my ($str) = @_; |
| 371 |
if ( $opt_debug ) { |
| 372 |
$str =~ s/[^[:print:]\r\n\t]/./g; |
| 373 |
print STDERR $str; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
package StatusLine; |
| 379 |
use Time::HiRes qw(gettimeofday tv_interval ); |
| 380 |
|
| 381 |
sub new { |
| 382 |
my $class = shift; |
| 383 |
|
| 384 |
my $self = { |
| 385 |
prev_len => 0 |
| 386 |
}; |
| 387 |
|
| 388 |
bless $self, $class; |
| 389 |
return $self; |
| 390 |
} |
| 391 |
|
| 392 |
sub set { |
| 393 |
my ($self, $str) = @_; |
| 394 |
|
| 395 |
my $now = [gettimeofday()]; |
| 396 |
$self->{last_str} = $str; |
| 397 |
|
| 398 |
if ( !defined $self->{prev_time} || tv_interval($self->{prev_time}, $now) >= 0.1 ) { |
| 399 |
print STDERR "\r" . (" " x $self->{prev_len}) . "\r$str"; |
| 400 |
$self->{prev_len} = length($str); |
| 401 |
$self->{prev_time} = $now; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
sub clear { |
| 406 |
my ($self) = @_; |
| 407 |
undef $self->{prev_time}; |
| 408 |
$self->set(""); |
| 409 |
} |
| 410 |
|
| 411 |
sub show_last { |
| 412 |
my ($self) = @_; |
| 413 |
undef $self->{prev_time}; |
| 414 |
$self->set( $self->{last_str} ) if ( $self->{last_str} ); |
| 415 |
} |