#!/usr/bin/perl # # HTTP Test and record # # Copyright, Clint Byrum, 2004. All Rights Reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # $Id: proxytest.pl,v 1.1 2005/01/06 19:41:45 clint Exp $ # use LWP::UserAgent; use Time::HiRes qw( time ); use Socket; use DBI; use Getopt::Long; $debugmode=''; $noupdate=''; GetOptions('debug|d' => \$debugmode ,'noupdate|n'=> \$noupdate ); # If we're not updating the tables.. might as well debug. if($noupdate) { $debugmode=1; } # connect to sql db $dbuser='changeme'; $dbpass='changepass'; $dsource='dbi:mysql:database=cdntest'; unless($noupdate) { our $dbh = DBI->connect($dsource,$dbuser,$dbpass); if(!$dbh) { die "Could not connect to data source $source - ".$DBI::errstr; } } if($ARGV[0]) { $file=$ARGV[0]; open(STDIN,"<$file") || die $!; } unless($noupdate) { $sql="INSERT INTO speedtest (date,site,proxy,retcode,result,farmip,local_result,local_retcode ,content_len,local_content_len) VALUES (FROM_UNIXTIME(?),?,?,?,?,?,?,?,?,?);"; our $sth = $dbh->prepare($sql); if(!$sth) { die "Error preparing SQL - ".$dbh->errstr; } } @sites = ('/put' ,'/things/that/go' ,'/after/GET/here'); our $proxyname = 'change.this.to.the.proxy.hostname'; our $httpname = 'change.this.to.the.desired.host.header'; our $originame = 'change.this.to.the.origin.server.name'; # This is *usually* $httpname our $ua = LWP::UserAgent->new(timeout => 30); my $url; my $site; foreach $site (@sites) { &get_url($site,'anon'); } sub get_url { my $site=shift; my $mode=shift; if($debugmode) { print STDERR "DEBUG: mode=$mode site=$site\n"; } $starttime=time; # lookup IP then pass to LWP - record IP also (undef,undef,undef,undef,@farmips) = gethostbyname($proxyname); #$selection=rand 1; #$selection=sprintf('%.0f',$selection); $selection=0; $farmip=inet_ntoa($farmips[$selection]); $proxyurl="http://$farmip"; $url = $proxyurl."$site"; if($debugmode){print "DEBUG: requesting $url\n"}; $req = HTTP::Request->new(GET => $url); $req->header('Host' => "$httpname"); $res=$ua->request($req); $nowtime=time; $timelapsed=sprintf("%5.4f",$nowtime-$starttime); # now get local result $req = HTTP::Request->new(GET => "http://$originame".$site); if ($debugmode) {print "DEBUG: requesting $site\n";} $lstarttime=time; $lres=$ua->request($req); $lnowtime=time; $ltimelapsed=sprintf("%5.4f",$lnowtime-$lstarttime); $proxy=""; if($code =~ /500/ and $debugmode) { if($res->header("Content-Type") =~ /text\//) { print "DEBUG: content=[".$res->content."]\n"; } else { print "DEBUG: content=[Cannot display ".$res->header("Content-Type") ."]\n"; } } $retcode=$res->code; $lretcode=$lres->code; $clen=length($res->content); $lclen=length($lres->content); if($debugmode) { print STDERR "DEBUG: start=$starttime site=$site proxy=$proxy\n"; print STDERR "DEBUG: retcode=$retcode time=$timelapsed\n"; print STDERR "DEBUG: lretcode=$lretcode ltime=$ltimelapsed\n"; print STDERR "DEBUG: clen=$clen lclen=$lclen farmip=$farmip\n"; } unless ($noupdate) { $sth->execute($starttime,$site,$proxy,$retcode ,$timelapsed,$farmip,$ltimelapsed,$lretcode ,$clen,$lclen); } }