All pastes #904258 Raw Edit

Something

public text v1 · immutable
·published 2008-02-14 20:36 UTC
rendered paste body
#!/usr/bin/perl

# Script to profile speeds of optimusfs
# $Id: optimusfs_stress.pl 1932 2008-02-14 20:30:28Z mish $

use Time::HiRes;  # high resolution timing required
use Time::Stopwatch;
use Statistics::Descriptive;
use Getopt::Long;
use Data::Dumper;
use HTML::Lint;
use URI::Escape;
use URI;

use strict;
use warnings;
use threads;
use threads::shared;

use Test::More qw/no_plan/;
use Test::WWW::Mechanize;

my $help = 0;
my $threads = 1;
my $host1 = 'dev01';
my $host2 = 'dev02';
my $target_url = 'http://dildomall.com';
my $start_url = "/Optimus";
my $test_for = 500; # seconds
my $no_test_errors = 0;

GetOptions(
	"host1=s" => \$host1,
	"host2=s" => \$host2,
	"fork=i" => \$threads,
	"url=s"  => \$start_url,
	"time=i" => \$test_for,
	"help|h" => \$help,
	"error|e" => \$no_test_errors,
);

if ($help) {
	usage();
	exit;
}

sub usage {
	print qq {Usage: $0 [-hfput --help]
host(1|2): host to run tests on. default: dev01
f[ork]: how many threads to spawn. default: 1
u[rl]:  what url to start at. default: /Optimus
t[ime]: how long to run the tests for in seconds. default: 500
e[rror]: don't run tests that intentionally generate errors
[h]elp: this help
		};
} #'



sub run_tests {
    my @args = @_;

	my @threads;
	
	# spawn threads
	for (1 ... $threads) {
		my $thread = threads->create(\&test_optimus, @args);
		push @threads, $thread;
	}
	
	# wait for threads to finish
	$_->join foreach @threads;
}

sub test_optimus {
    my $test_old = shift;

    my $testhost = $test_old ? $host1 : $host2;

	my $base_url = "http://" . $testhost;
	my $op_url = "$base_url$start_url";

	if ($test_old) { # test old optimusfs
        my $mech = new Test::WWW::Mechanize(stack_depth => 1, max_redirect => 1);

		$mech->get_ok("$op_url/st.html", "Retrieved $op_url");

        $mech->field(targetURL => $target_url);
        $mech->submit_form;

        is($mech->status, "302", "Got redirect");
        is($mech->response->header('Location'), $target_url, "Got correct targetUrl redirect");

	} else { # test perlbal optimusfs
        my $mech = new Test::WWW::Mechanize(stack_depth => 1, max_redirect => 0);

        my $url = "$op_url/?targetUrl=" . uri_escape($target_url);
        $mech->get($url);

        is($mech->status, "302", "Got redirect");
        is($mech->response->header('Location'), $target_url, "Got correct targetUrl redirect");
    }
}

my $results = '';

sub tests {
    my ($old) = @_;

    my $host = $old ? $host1 : $host2;
    $results .= " | - $host -\n";

    my $stat = Statistics::Descriptive::Sparse->new();

    tie my $time, 'Time::Stopwatch';
    tie my $wait, 'Time::Stopwatch';

    # test speed
    while ($wait < $test_for) {
        my $diff = 0;
        $time = 0; run_tests($old); $diff += $time;
        $time = 0;                 $diff -= $time;
        $stat->add_data($diff);
    }

    $results .= " |   count: " . $stat->count() . " iterations\n" .
          " |   mean:  " . $stat->mean() . " seconds\n" .
          " |   s.d.:  " . $stat->standard_deviation . " seconds\n\n";
}

tests(1);
tests(0);

print "\n --- RESULTS ---\n$results\n";