| 1 | package WWW::Selenium::NetworkCapture; | ||
| 2 | |||
| 3 | # ABSTRACT: capture and report request data from Selenium / Firefox | ||
| 4 | # Nicked from http://code.google.com/p/selenium-profiler/source/browse/trunk/web_profiler.py | ||
| 5 | |||
| 6 | =pod | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | WWW::Selenium::NetworkCapture - capture and report request data from Selenium / Firefox | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version 1.58 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | our $VERSION = '0.01'; | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | use WWW::Selenium; | ||
| 22 | use WWW::Selenium::NetworkCapture; | ||
| 23 | |||
| 24 | my $sel = WWW::Selenium->new(); | ||
| 25 | |||
| 26 | $sel->{session_id} = $sel->get_string( | ||
| 27 | "getNewBrowserSession", | ||
| 28 | $sel->{browser_start_command}, | ||
| 29 | $sel->{browser_url}, | ||
| 30 | undef, | ||
| 31 | 'captureNetworkTraffic=true' | ||
| 32 | ); | ||
| 33 | |||
| 34 | |||
| 35 | $sel->open($path); | ||
| 36 | my $traffic_xml = $sel->get_string('captureNetworkTraffic', 'xml'); | ||
| 37 | my $netcap = WWW::Selenium::NetworkCapture->new($traffic_xml); | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | use XML::Simple qw/XMLin/; | ||
| 42 | use DateTime::Format::Strptime; | ||
| 43 | |||
| 44 | =head1 FUNCTIONS | ||
| 45 | |||
| 46 | All functions listed below are exported to the calling namespace. | ||
| 47 | |||
| 48 | =head2 new($xml_blob) | ||
| 49 | |||
| 50 | Creates and returns a new instance of this class. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my ($class, $xml_blob) = @_; | ||
| 56 | |||
| 57 | my %self; | ||
| 58 | $self->{xml_blob} = $xml_blob; | ||
| 59 | if (length($xml_blob) < 50) { | ||
| 60 | die; | ||
| 61 | } else { | ||
| 62 | $self->{dom} = XMLin($xml_blob); | ||
| 63 | } | ||
| 64 | |||
| 65 | bless $self, $class or die "Can't bless $class: $!"; | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 get_content_size() | ||
| 70 | |||
| 71 | Returns the sum in bytes of all content received since the start of the session. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub get_content_size { | ||
| 76 | my ($self) = @_; | ||
| 77 | |||
| 78 | my @byte_sizes; | ||
| 79 | foreach my $child (@{$self->{dom}->{entry}}) { | ||
| 80 | push @byte_sizes, $child->{bytes}; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $total_size; | ||
| 84 | $total_size += $_ for @byte_sizes; | ||
| 85 | $total_size = $total_size / 1000.0; | ||
| 86 | |||
| 87 | return $total_size; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 get_num_requests() | ||
| 91 | |||
| 92 | Returns the total number of requests made since the start of the session. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub get_num_requests { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | return scalar @{$self->{dom}->{entry}}; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 get_http_status_codes() | ||
| 103 | |||
| 104 | Returns the total number of requests made since the start of the session. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub get_http_status_codes { | ||
| 109 | my ($self) = @_; | ||
| 110 | |||
| 111 | my %status_map; | ||
| 112 | foreach my $child (@{$self->{dom}->{entry}}) { | ||
| 113 | if ($status_map{$child->{statusCode}}) { | ||
| 114 | $status_map{$child->{statusCode}} += 1; | ||
| 115 | } else { | ||
| 116 | $status_map{$child->{statusCode}} = 1; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | return %status_map; | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 get_http_details() | ||
| 124 | |||
| 125 | Returns the HTTP details of requests made since the start of the session. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub get_http_details { | ||
| 130 | my ($self) = @_; | ||
| 131 | |||
| 132 | my @http_details; | ||
| 133 | foreach my $child (@{$self->{dom}->{entry}}) { | ||
| 134 | my $url = $child->{url}; | ||
| 135 | my $url_stem = (split '\?', $url)[0]; | ||
| 136 | my $doc = '/' . (split '/', $url_stem)[-1]; | ||
| 137 | my $status = int $child->{statusCode}; | ||
| 138 | my $method = ($child->{method} =~ /([^']+)/)[0]; | ||
| 139 | my $size = int $child->{bytes}; | ||
| 140 | my $time = int $child->{timeInMillis}; | ||
| 141 | push @http_details, [$status, $method, $doc, $size, $time]; | ||
| 142 | } | ||
| 143 | |||
| 144 | return @http_details; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 get_file_extension_stats() | ||
| 148 | |||
| 149 | Returns the file extension statistics of each request made during the session | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub get_file_extension_stats { | ||
| 154 | my ($self) = @_; | ||
| 155 | |||
| 156 | my %file_ext_map = {}; # k=extension v=(count,size) | ||
| 157 | foreach my $child (@{$self->{dom}->{entry}}) { | ||
| 158 | my $size = $child->{bytes} / 1000.0; | ||
| 159 | my $url = $child->{url}; | ||
| 160 | my $url_stem = (split '\?', $url)[0]; | ||
| 161 | my $doc = '/' . (split '/', $url_stem)[-1]; | ||
| 162 | use vars qw/$file_ext/; | ||
| 163 | if ($doc =~ /\./ && $doc !~ /\.$/) { | ||
| 164 | $file_ext = (split '\.', $doc)[-1]; | ||
| 165 | } else { | ||
| 166 | $file_ext = 'unknown'; | ||
| 167 | } | ||
| 168 | |||
| 169 | if ($file_ext_map{$file_ext}) { | ||
| 170 | ${$file_ext_map{$file_ext}}[0] += 1; | ||
| 171 | ${$file_ext_map{$file_ext}}[1] += $size; | ||
| 172 | } else { | ||
| 173 | $file_ext_map{$file_ext} = [1, $size]; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | return %file_ext_map; | ||
| 178 | } | ||
| 179 | |||
| 180 | =head2 get_network_times() | ||
| 181 | |||
| 182 | Returns ($start_first_request, $end_first_request, $end_last_request); | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub get_network_times { | ||
| 187 | my ($self) = @_; | ||
| 188 | |||
| 189 | my (@timings, @start_times, @end_times); | ||
| 190 | foreach my $child (@{$self->{dom}->{entry}}) { | ||
| 191 | push @timings, $child->{timeInMillis}; | ||
| 192 | push @start_times, $child->{start}; | ||
| 193 | push @end_times, $child->{end}; | ||
| 194 | } | ||
| 195 | @start_times = sort @start_times; | ||
| 196 | @end_times = sort @end_times; | ||
| 197 | my $start_first_request = $self->_convert_time($start_times[0]); | ||
| 198 | my $end_first_request = $self->_convert_time($end_times[0]); | ||
| 199 | my $end_last_request = $self->_convert_time($end_times[-1]); | ||
| 200 | |||
| 201 | return ($start_first_request, $end_first_request, $end_last_request); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub _convert_time { | ||
| 205 | my ($self, $date_string) = @_; | ||
| 206 | |||
| 207 | if ($date_string =~ /-/) { | ||
| 208 | my $split_char = '-'; | ||
| 209 | } else { | ||
| 210 | my $split_char = '\+'; | ||
| 211 | } | ||
| 212 | |||
| 213 | $date_string =~ s/-//g; | ||
| 214 | my $strp = DateTime::Format::Strptime->new( | ||
| 215 | pattern => '%Y%m%dT%H:%M:%S', | ||
| 216 | ); | ||
| 217 | my $dt = $strp->parse_datetime($date_string); | ||
| 218 | return $dt; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; | ||
| 222 | |||
| 223 | __END__ |