Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 177 lines (145 sloc) 5.17 KB
#! /opt/kronometrix/perl/bin/perl
use lib './lib';
use Kronometrix::Webrec::Queue;
use POSIX qw(pause);
use Time::HiRes qw(time alarm setitimer ITIMER_REAL);
use Getopt::Std;
use File::Basename;
use strict;
use warnings;
use feature ':5.20';
our $VERSION = '1.2.7';
our $LASTREV = '2017-09-20';
### Configuration variables
my $cfile = 'webrec.json'; # webloads configuration file
my $kfile = 'kronometrix.json'; # general configuration file
my $recname = basename($0);
my $osname = "$^O";
my $agent = "kdr/1.3.4 webrec $osname";
my $timeout = 30;
### Process command line args
my %opts;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
getopts('ahlvdVg:s:', \%opts) or HELP_MESSAGE();
if ($opts{h}) {
HELP_MESSAGE();
}
if ($opts{V}) {
VERSION_MESSAGE();
exit 0;
}
# verbose and debug flags
my $verbose = exists $opts{v} || exists $opts{d} || 0;
my $debug = exists $opts{d} || 0;
# Time between requests
print STDERR "-s switch in invalid in async mode\n" and HELP_MESSAGE()
if exists $opts{a} && exists $opts{s};
my $nap_time = exists $opts{s} ? $opts{s} : 0.1;
# Maximum number of concurrent requests
my $num_concurrent_reqs = exists $opts{g} ? $opts{g} : 25;
my $max_concurrent_reqs = exists $opts{a} ? $num_concurrent_reqs : 1;
# logging option
my $log;
if ($opts{l}) {
# kronometrix.json config file
my $kdata = Kronometrix::Webrec::Queue->open_config($kfile);
# get log destinations
my ($baselog, $curlog) = get_log($kdata);
# Redirect STDOUT, STDERR to their log files
my $rlog = $baselog . "/webrec.log";
open STDERR, '>>', $rlog
or die "error: open_file - cannot open $rlog $!";
my $klog = $curlog . "/webrec.krd";
open STDOUT, '>>', $klog
or die "error: open_file - cannot open $klog $!";
}
# process [interval [count]]
my ( $interval, $loop_max );
my $precision = 0;
if ( defined $ARGV[0] ) {
$interval = $ARGV[0];
$loop_max = defined $ARGV[1] ? $ARGV[1] : 2**32;
$precision = 3 if $interval =~ /\./;
usage() if $interval == 0;
}
else {
$interval = 1;
$loop_max = 1;
}
### Parse config file
my $queue = Kronometrix::Webrec::Queue->new(
config_file => $cfile,
max_concurrent_requests => $max_concurrent_reqs,
precision => $precision,
agent_name => $agent,
timeout => $timeout,
nap_time => $nap_time,
verbose => $verbose,
debug => $debug,
);
$queue->write_verbose("INFO: Interval: $interval - Iterations: $loop_max - "
. "Num of concurrent requests: $max_concurrent_reqs");
### Main loop
# Set timer between queue executions
local $SIG{ALRM} = sub {};
local $| = 1;
setitimer(ITIMER_REAL, $interval, $interval);
while ($loop_max) {
$queue->process;
pause;
$loop_max--;
$queue->write_verbose("INFO: Remaining iterations: $loop_max");
}
$queue->write_log("INFO: Iterations finished");
### Auxiliary subroutines
# get log defintion
sub get_log {
my ($data) = @_;
my $bpath = $data->{'log'}->{'base_path'};
my $cpath = $data->{'log'}->{'current_path'};
return ( $bpath, $cpath );
}
# Usage - print usage and exit.
sub HELP_MESSAGE {
print STDERR <<END;
USAGE: webrec [-dlhvV] [-s secs] [-a [-g concurrent]] | [interval [count]]
OPTIONS:
-a : perform requests asynchronously
-g : the number of concurrent requests. Default is 25.
-s : sleep delay between transactions, default 0.1
-l : log raw data to krd file, no STDOUT
-h : help information
-v : verbose mode
-d : debug information
-V : release version
interval : maximum number of seconds between samples
count : number of times to repeat the request cycle. Defaults to 2**32
FIELDS:
#01 timestamp : seconds since Epoch, time
#02 request : the HTTP request name
#03 ttime : total time, entire operation lasted, seconds
#04 ctime : connect time it took from the start until the TCP
connect to the remote host (or proxy) was completed, seconds
#05 dnstime : namelookup time, it took from the start until the name
resolving was completed, seconds
#06 ptime : protocol time, it took from the start until the file
transfer was just about to begin, seconds
#07 pktime : first packet time, it took from the start until the first
byte was just about to be transferred, seconds
#08 size : page size, the total amount of bytes that were downloaded
#09 status : response status code, the numerical response code
that was found in the last retrieved HTTP(S) transfer
NOTES
webrec reports TTFB (time to first byte) for one or many HTTP workloads,
applications, as an indication of the responsiveness. TTFB measures the
duration from the virtual user making an HTTP request to the first byte
of the page being received by the browser. This time is made up of the
socket connection time, the time taken to send the HTTP request, and the
time taken to get the first byte of the page.
END
exit 0;
}
# Revision - print revision and exit
sub VERSION_MESSAGE {
print STDERR "$recname: $VERSION, $LASTREV\n";
}