3 Einträge, 1 Seite |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
[code] #!/usr/bin/perl -w use LWP::Parallel::UserAgent; use Time::HiRes qw(gettimeofday tv_interval); use strict; ### # Configuration ### my $nof_parallel_connections = 1; my $nof_requests_total = 1; my $timeout = 10; my $maxurls = 2; my @urls; my @tmpurls; my $linkadressen = "links.dat"; my $cnt=0; my $zufall; ################################################## # Derived Class for latency timing ################################################## package MyParallelAgent; @MyParallelAgent::ISA = qw(LWP::Parallel::UserAgent); use strict; ## zufaellige url auswahl open(TXT,$linkadressen); while (<TXT>) { push (@tmpurls, $_); #packe alle urls aus der textdatei in das array @tmpurls } close (TXT); while ($cnt < $maxurls) { $zufall = int(rand(@tmpurls +1)); #print "<<< $zufall >>>\n"; push (@urls, ($tmpurls[$zufall])); #suche zufaellig eine url aus dem array und haenge das in die url liste $cnt++; } ### # Is called when connection is opened ### sub on_connect { my ($self, $request, $response, $entry) = @_; $self->{__start_times}->{$entry} = [Time::HiRes::gettimeofday]; } ### # Are called when connection is closed ### sub on_return { my ($self, $request, $response, $entry) = @_; my $start = $self->{__start_times}->{$entry}; $self->{__latency_total} += Time::HiRes::tv_interval($start); } sub on_failure { on_return(@_); # Same procedure } ### # Access function for new instance var ### sub get_latency_total { return shift->{__latency_total}; } ################################################## package main; ################################################## ### # Init parallel user agent ### my $ua = MyParallelAgent->new(); $ua->agent("pounder/1.0"); $ua->max_req($nof_parallel_connections); $ua->redirect(0); # No redirects ### # Register all requests ### foreach (1..$nof_requests_total) { foreach my $url (@urls) { my $request = HTTP::Request->new('GET', $url); $ua->register($request); } } ### # Launch processes and check time ### my $start_time = [gettimeofday]; my $results = $ua->wait($timeout); my $total_time = tv_interval($start_time); ### # Requests all done, check results ### my $succeeded = 0; my %errors = (); foreach my $entry (values %$results) { my $response = $entry->response(); if($response->is_success()) { $succeeded++; # Another satisfied customer } else { # Error, save the message $response->message("TIMEOUT") unless $response->code(); $errors{$response->message}++; } } ### # Format errors if any from %errors ### my $errors = join(',', map "$_ ($errors{$_})", keys %errors); $errors = "NONE" unless $errors; ### # Format results ### #@urls = map {($_,".")} @urls; my @P = ( "URL(s)" => join("\n\t\t ", @urls), "Total Requests" => $nof_requests_total * @urls, "Parallel Agents" => $nof_parallel_connections * @urls, "Succeeded" => sprintf("$succeeded (%.2f%%)\n", $succeeded * 100 / ( $nof_requests_total * @urls ) ), "Errors" => $errors, "Total Time" => sprintf("%.2f secs\n", $total_time), "Throughput" => sprintf("%.2f Requests/sec\n", ( $nof_requests_total * @urls ) / $total_time), "Latency" => sprintf("%.2f secs/Request", ($ua->get_latency_total() || 0) / ( $nof_requests_total * @urls ) ), ); my ($left, $right); ### # Print out statistics ### format STDOUT = @<<<<<<<<<<<<<<< @* "$left:", $right . while(($left, $right) = splice(@P, 0, 2)) { write;
3 Einträge, 1 Seite |