| 1 | package Net::Analysis::Listener::HTTPClientPerf; | ||
| 2 | # $Id: TCP.pm 81 2004-11-07 15:40:29Z abworrall $ | ||
| 3 | |||
| 4 | # {{{ Boilerplate | ||
| 5 | |||
| 6 | use 5.008000; | ||
| 7 | our $VERSION = '0.01'; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | use Carp qw(carp croak confess); | ||
| 12 | |||
| 13 | use Params::Validate qw(:all); | ||
| 14 | |||
| 15 | use base qw(Net::Analysis::Listener::Base); | ||
| 16 | |||
| 17 | # }}} | ||
| 18 | |||
| 19 | use HTTP::Response; | ||
| 20 | use HTTP::Request; | ||
| 21 | use Net::Analysis::Constants qw(:packetclasses); | ||
| 22 | use Net::Analysis::Time; | ||
| 23 | use Data::Dumper; #XXXX | ||
| 24 | use PostScript::Simple; | ||
| 25 | |||
| 26 | #### Callbacks | ||
| 27 | # | ||
| 28 | # {{{ validate_configuration | ||
| 29 | |||
| 30 | sub validate_configuration { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | my %h =validate(@_,{v => {type => SCALAR, default => 1}, | ||
| 34 | tsnap => {type => SCALAR, default => 25000}, | ||
| 35 | #tsnap => {type => SCALAR, default => 100000}, | ||
| 36 | show_all_packets => {type => SCALAR, default => 0}, | ||
| 37 | file => {type => SCALAR, default => 'out.ps'}, | ||
| 38 | pdf => {type => SCALAR, default => undef}, | ||
| 39 | ggv => {type => SCALAR, default => undef}, | ||
| 40 | }); | ||
| 41 | |||
| 42 | return \%h; | ||
| 43 | } | ||
| 44 | |||
| 45 | # }}} | ||
| 46 | |||
| 47 | # {{{ setup | ||
| 48 | |||
| 49 | sub setup { | ||
| 50 | my ($self) = shift; | ||
| 51 | |||
| 52 | # Setup our data structure | ||
| 53 | $self->{open_boxes} = {}; | ||
| 54 | $self->{closed_boxes} = {}; | ||
| 55 | } | ||
| 56 | |||
| 57 | # }}} | ||
| 58 | # {{{ teardown | ||
| 59 | |||
| 60 | sub teardown { | ||
| 61 | my ($self) = shift; # No arguments to this callback | ||
| 62 | |||
| 63 | croak ("teardown has open boxes") if (keys %{$self->{open_boxes}}); | ||
| 64 | |||
| 65 | $self->_generate_report(); | ||
| 66 | } | ||
| 67 | |||
| 68 | # }}} | ||
| 69 | # {{{ tcp_packet | ||
| 70 | |||
| 71 | sub tcp_packet { | ||
| 72 | my ($self, $args) = @_; | ||
| 73 | my $pkt = $args->{pkt}; | ||
| 74 | my $k = $pkt->{socketpair_key}; | ||
| 75 | my $box = $self->_box($k); | ||
| 76 | |||
| 77 | #die Dumper($args); | ||
| 78 | |||
| 79 | confess "bad args (".join(',',keys %$args).")" if (!defined $pkt); | ||
| 80 | |||
| 81 | $self->_add_packet_to_box ($box, $pkt); | ||
| 82 | |||
| 83 | # Build overall bounds for the entire data set | ||
| 84 | if (!defined $self->{t_start} || $self->{t_start} > $pkt->{time}) { | ||
| 85 | $self->{t_start} = $pkt->{time} + 0; | ||
| 86 | } | ||
| 87 | if (!defined $self->{t_end} || $self->{t_end} < $pkt->{time}) { | ||
| 88 | $self->{t_end} = $pkt->{time} + 0; | ||
| 89 | } | ||
| 90 | |||
| 91 | $self->_trace ("[$k] - $pkt"); | ||
| 92 | } | ||
| 93 | |||
| 94 | # }}} | ||
| 95 | # {{{ tcp_session_start | ||
| 96 | |||
| 97 | sub tcp_session_start { | ||
| 98 | my ($self, $args) = @_; | ||
| 99 | my $k = $args->{socketpair_key}; | ||
| 100 | my $pkt = $args->{pkt}; # Might be undef | ||
| 101 | |||
| 102 | # Open up a new session box, based on this packet | ||
| 103 | if (exists $self->{open_boxes}{$k}) { | ||
| 104 | croak ("$k already has open box ?") | ||
| 105 | } | ||
| 106 | if (!$pkt) { | ||
| 107 | croak ("new session, no packet; what to do here ?"); | ||
| 108 | } | ||
| 109 | |||
| 110 | $self->_box($k, {t_start => $pkt->{time}}); | ||
| 111 | |||
| 112 | $self->_trace ("[$k]==== START $pkt->{time}\n"); | ||
| 113 | } | ||
| 114 | |||
| 115 | # }}} | ||
| 116 | # {{{ tcp_session_end | ||
| 117 | |||
| 118 | sub tcp_session_end { | ||
| 119 | my ($self, $args) = @_; | ||
| 120 | my $k = $args->{socketpair_key}; | ||
| 121 | my $pkt = $args->{pkt}; # Might be undef | ||
| 122 | |||
| 123 | if (exists $self->{open_boxes}{$k}) { | ||
| 124 | $self->{open_boxes}{$k}{t_end} = $pkt->{time} if (defined $pkt); | ||
| 125 | $self->{closed_boxes}{$k} = delete ($self->{open_boxes}{$k}); | ||
| 126 | |||
| 127 | } else { | ||
| 128 | #carp ("$k has no box to close [".($pkt ? "$pkt" : 'no pkt')."]"); | ||
| 129 | } | ||
| 130 | |||
| 131 | $self->_trace ("[$k]==== END\n"); | ||
| 132 | } | ||
| 133 | |||
| 134 | # }}} | ||
| 135 | # {{{ http_transaction | ||
| 136 | |||
| 137 | # args: (req,req_mono,resp,resp_mono,socketpair_key,t_elapsed,t_end,t_start) | ||
| 138 | |||
| 139 | sub http_transaction { | ||
| 140 | my ($self, $args) = @_; | ||
| 141 | my $k = $args->{socketpair_key}; | ||
| 142 | my $pkt = $args->{pkt}; # Might be undef | ||
| 143 | my $uri = $self->_full_uri($args); | ||
| 144 | my $box = $self->_box($k); | ||
| 145 | |||
| 146 | #print "args: (".join(',', (sort keys %$args)).")\n"; | ||
| 147 | |||
| 148 | my $xact = | ||
| 149 | {class => $self->_classify_http_transaction ($uri, $args), | ||
| 150 | uri => $uri, | ||
| 151 | t_xact_start => $args->{t_start}, | ||
| 152 | t_xact_end => $args->{t_end}, | ||
| 153 | t_xact_elapsed => $args->{t_elapsed}, | ||
| 154 | |||
| 155 | t_req_start => $args->{req_mono}->t_start(), | ||
| 156 | t_req_end => $args->{req_mono}->t_end(), | ||
| 157 | t_req_elapsed => $args->{req_mono}->t_elapsed(), | ||
| 158 | req_n_packets => $args->{req_mono}->n_packets(), | ||
| 159 | |||
| 160 | t_resp_start => $args->{resp_mono}->t_start(), | ||
| 161 | t_resp_end => $args->{resp_mono}->t_end(), | ||
| 162 | t_resp_elapsed => $args->{resp_mono}->t_elapsed(), | ||
| 163 | resp_n_packets => $args->{resp_mono}->n_packets(), | ||
| 164 | |||
| 165 | req_size => $args->{req_mono}->length(), | ||
| 166 | resp_size => $args->{resp_mono}->length(), | ||
| 167 | }; | ||
| 168 | |||
| 169 | $self->_trace ("[$k] == $uri ($xact->{t_xact_elapsed}s)\n"); | ||
| 170 | |||
| 171 | push (@{$box->{http_transactions}}, $xact); | ||
| 172 | } | ||
| 173 | |||
| 174 | # }}} | ||
| 175 | |||
| 176 | # {{{ as_string | ||
| 177 | |||
| 178 | sub as_string { | ||
| 179 | my ($self) = @_; | ||
| 180 | my $s = ''; | ||
| 181 | |||
| 182 | $s .= "[".ref($self)."]"; | ||
| 183 | |||
| 184 | return $s; | ||
| 185 | } | ||
| 186 | |||
| 187 | # }}} | ||
| 188 | |||
| 189 | #### Support for building data structure | ||
| 190 | # | ||
| 191 | # {{{ _trace | ||
| 192 | |||
| 193 | # This may become more clever ... | ||
| 194 | |||
| 195 | our $TRACE=0; | ||
| 196 | |||
| 197 | sub _trace { | ||
| 198 | my ($self) = shift; | ||
| 199 | |||
| 200 | return if (! $TRACE); | ||
| 201 | |||
| 202 | foreach (@_) { | ||
| 203 | my $l = $_; # Skip 'Modification of a read-only value' errors | ||
| 204 | chomp ($l); | ||
| 205 | print "$l\n"; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | # }}} | ||
| 210 | |||
| 211 | # {{{ _box | ||
| 212 | |||
| 213 | sub _box { | ||
| 214 | my ($self, $k, $h) = @_; | ||
| 215 | |||
| 216 | if (defined $h) { | ||
| 217 | $self->{open_boxes}{$k} = $h; | ||
| 218 | } | ||
| 219 | |||
| 220 | return $self->{open_boxes}{$k}; | ||
| 221 | } | ||
| 222 | |||
| 223 | # }}} | ||
| 224 | # {{{ _add_packet_to_box | ||
| 225 | |||
| 226 | # We cannot tell if the packet is in the request or the response. But that is | ||
| 227 | # OK, since we know when to flag the start and end of both the request and | ||
| 228 | # response phases. Even if they both fall in the same timebox, we also know | ||
| 229 | # how many packets are in the request and response! | ||
| 230 | |||
| 231 | sub _add_packet_to_box { | ||
| 232 | my ($self, $box, $pkt) = @_; | ||
| 233 | |||
| 234 | confess "bad packet" if (!defined $pkt); | ||
| 235 | |||
| 236 | my ($s,$real_us) = $pkt->{time}->numbers(); | ||
| 237 | my ($us) = ($real_us) - ($real_us % $self->{tsnap}); | ||
| 238 | |||
| 239 | my (%hide) = (PKT_NOCLASS, 1); | ||
| 240 | |||
| 241 | $hide{''.PKT_NONDATA} = 1 if (! $self->{show_all_packets}); | ||
| 242 | |||
| 243 | return if (exists $hide{$pkt->{class}}); | ||
| 244 | |||
| 245 | my $stack = $box->{pkt_stack}{$s}{$us} ||= []; | ||
| 246 | push (@$stack, $pkt); | ||
| 247 | |||
| 248 | if (!defined $box->{t_end} || $box->{t_end} < $pkt->{time}) { | ||
| 249 | $box->{t_end} = $pkt->{time} + 0; | ||
| 250 | } | ||
| 251 | |||
| 252 | # Now add to a global stack | ||
| 253 | my $glob_stack = $self->{global_pkt_stack}{$s}{$us} ||= []; | ||
| 254 | push (@$glob_stack, $pkt) | ||
| 255 | } | ||
| 256 | |||
| 257 | # }}} | ||
| 258 | # {{{ _classify_http_transaction | ||
| 259 | |||
| 260 | # Say, into images vs. adserver stuff vs. javascript vs. content | ||
| 261 | sub _classify_http_transaction { | ||
| 262 | my ($self, $uri, $args) = @_; | ||
| 263 | |||
| 264 | return 0; | ||
| 265 | } | ||
| 266 | |||
| 267 | # }}} | ||
| 268 | # {{{ _full_uri | ||
| 269 | |||
| 270 | sub _full_uri { | ||
| 271 | my ($self, $args) = @_; | ||
| 272 | |||
| 273 | my $u = $args->{req}->header('host'); | ||
| 274 | $u .= $args->{req}->uri()->as_string(); | ||
| 275 | |||
| 276 | return $u; | ||
| 277 | } | ||
| 278 | |||
| 279 | # }}} | ||
| 280 | |||
| 281 | #### Support for generating report | ||
| 282 | # | ||
| 283 | # {{{ _generate_report | ||
| 284 | |||
| 285 | sub _generate_report { | ||
| 286 | my ($self) = @_; | ||
| 287 | |||
| 288 | my ($gs_args) = '-dBATCH -dNOPAUSE -sDEVICE=pdfwrite'; | ||
| 289 | |||
| 290 | $self->_set_bounds(); | ||
| 291 | |||
| 292 | if ($self->{file}) { | ||
| 293 | $self->_ps_report ($self->{file}); | ||
| 294 | system ("ggv $self->{file}") if ($self->{ggv}); | ||
| 295 | |||
| 296 | if ($self->{pdf}) { | ||
| 297 | my $f = $self->{file}; | ||
| 298 | rename ($f, "$f.ps"); | ||
| 299 | system ("gs $gs_args -sOUTPUTFILE=$f $f.ps"); | ||
| 300 | unlink ("$f.ps"); | ||
| 301 | } | ||
| 302 | |||
| 303 | } else { | ||
| 304 | $self->_ascii_report(); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | # }}} | ||
| 309 | # {{{ _set_bounds | ||
| 310 | |||
| 311 | sub _set_bounds { | ||
| 312 | my ($self) = @_; | ||
| 313 | |||
| 314 | # Baseline our times. | ||
| 315 | $self->{t_base} = $self->{t_start} + 0; | ||
| 316 | $self->{t_base}->round_usec ($self->{tsnap}); | ||
| 317 | |||
| 318 | my $t_diff = $self->{t_end} - $self->{t_base}; | ||
| 319 | $t_diff->round_usec($self->{tsnap}, 'up'); | ||
| 320 | |||
| 321 | $self->{n_cols} = $t_diff->usec() / $self->{tsnap}; | ||
| 322 | } | ||
| 323 | |||
| 324 | # }}} | ||
| 325 | # {{{ _ascii_report | ||
| 326 | |||
| 327 | sub _ascii_report { | ||
| 328 | my ($self) = @_; | ||
| 329 | |||
| 330 | my ($s,$us) = $self->{t_base}->numbers(); | ||
| 331 | |||
| 332 | print "[ ] | "; | ||
| 333 | my $n = 1; | ||
| 334 | foreach my $box_k (keys %{$self->{closed_boxes}}) { | ||
| 335 | printf ("% 4d ", $n++); | ||
| 336 | } | ||
| 337 | print "\n"; | ||
| 338 | print "[ ] | "; | ||
| 339 | $n = 1; | ||
| 340 | foreach my $box_k (keys %{$self->{closed_boxes}}) { | ||
| 341 | printf ("---- "); | ||
| 342 | } | ||
| 343 | print "\n"; | ||
| 344 | |||
| 345 | for my $n (0..($self->{n_cols} - 1)) { | ||
| 346 | |||
| 347 | # Display time column | ||
| 348 | printf "[% 3d %s.%06d] ", $n, $s, $us; | ||
| 349 | if (($n * $self->{tsnap}) % 1000000) { print "* " } | ||
| 350 | else { print "- " } | ||
| 351 | |||
| 352 | foreach my $box_k (keys %{$self->{closed_boxes}}) { | ||
| 353 | my $str = ''; | ||
| 354 | my $box = $self->{closed_boxes}{$box_k}; | ||
| 355 | if (exists $box->{hist}{$s}{$us}) { | ||
| 356 | my $h = $box->{hist}{$s}{$us}; | ||
| 357 | my $n = $h->[PKT_DATA]; | ||
| 358 | |||
| 359 | if (!defined $n) { $str = '' } | ||
| 360 | elsif ($n < 100) { $str = sprintf "% 4d",$n } | ||
| 361 | else { $str = "**" } | ||
| 362 | |||
| 363 | } | ||
| 364 | printf "%-4.4s ", $str; | ||
| 365 | } | ||
| 366 | |||
| 367 | print "\n"; | ||
| 368 | |||
| 369 | # Now move our time counter on. | ||
| 370 | $us += $self->{tsnap}; | ||
| 371 | if ($us >= 1000000) { | ||
| 372 | $s++; | ||
| 373 | $us -= 1000000; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | # }}} | ||
| 379 | |||
| 380 | #### PostScript gubbins - a bit of a midnight hack, all this | ||
| 381 | # | ||
| 382 | use constant { X => 0, Y => 1}; | ||
| 383 | # {{{ _ps_report | ||
| 384 | |||
| 385 | sub _ps_report { | ||
| 386 | my ($self, $fname) = @_; | ||
| 387 | |||
| 388 | # Variables used to build up the diagram | ||
| 389 | $self->{session_slots} = []; | ||
| 390 | $self->{key}{uris} = []; | ||
| 391 | |||
| 392 | my $p = $self->{ps} = PostScript::Simple->new (papersize=>'A4', | ||
| 393 | colour => 1, | ||
| 394 | eps => 0, | ||
| 395 | landscape => 1, | ||
| 396 | units => "mm"); | ||
| 397 | $p->newpage(); | ||
| 398 | $p->setfont("Helvetica", 10); | ||
| 399 | |||
| 400 | # Various constants that control rendering | ||
| 401 | $self->{const} = {origin => [15,15], | ||
| 402 | |||
| 403 | # Size of the default timebox (i.e. packet + spacing) | ||
| 404 | tbox_w => 1, | ||
| 405 | tbox_h => 0.65, | ||
| 406 | |||
| 407 | # Packet size | ||
| 408 | packet_w => 0.8, | ||
| 409 | packet_h => 0.5, | ||
| 410 | |||
| 411 | # TCP session (should be auto :/) | ||
| 412 | session_h => 20, | ||
| 413 | session_pad_v => 2, | ||
| 414 | }; | ||
| 415 | |||
| 416 | $self->_ps_key(); | ||
| 417 | $self->_ps_axes(); | ||
| 418 | |||
| 419 | my $boxes = $self->{closed_boxes}; | ||
| 420 | my $sorter = box_sorter($boxes); | ||
| 421 | foreach my $box_k (sort $sorter keys %$boxes) | ||
| 422 | { | ||
| 423 | my $box = $self->{closed_boxes}{$box_k}; | ||
| 424 | my $y = $self->_ps_box_height ($box); | ||
| 425 | |||
| 426 | $self->_ps_box ([$self->{const}{origin}[X], $y], $box); | ||
| 427 | } | ||
| 428 | |||
| 429 | # Now generate cumulative packets at bottom | ||
| 430 | $self->_ps_box_packets ($self->{const}{origin}, $self->{global_pkt_stack}); | ||
| 431 | |||
| 432 | $p->output ($fname); | ||
| 433 | } | ||
| 434 | |||
| 435 | # }}} | ||
| 436 | # {{{ _ps_axes | ||
| 437 | |||
| 438 | sub _ps_axes { | ||
| 439 | my ($self) = @_; | ||
| 440 | my $p = $self->{ps}; | ||
| 441 | my $tbox_width = 1; | ||
| 442 | my ($x,$y) = @{$self->{const}{origin}}; | ||
| 443 | my $x_max = $x + $self->{n_cols}*$tbox_width; | ||
| 444 | my ($height) = $self->{const}{session_h} - $self->{const}{session_pad_v} ; | ||
| 445 | |||
| 446 | # Bottom box | ||
| 447 | $p->setcolour (230,230,230); | ||
| 448 | $self->_ps_rect ([$x,$y], $x_max - $x, $height, 'filled'); | ||
| 449 | |||
| 450 | # X axis | ||
| 451 | $y -= 2.5; | ||
| 452 | |||
| 453 | $p->setfont("Helvetica", 10); | ||
| 454 | $p->setcolour(180, 180, 180); # Axes colour | ||
| 455 | $p->setlinewidth( 0.5 ); | ||
| 456 | |||
| 457 | $p->line($x-0.25,$y, $x_max,$y); # X axis | ||
| 458 | $p->line($x,$y, $x,$y-1); # First tick | ||
| 459 | $p->text($x-3, $y-4, $self->{t_base}->as_string('time')); # Base time | ||
| 460 | |||
| 461 | # Ticks & labels for X axis | ||
| 462 | my $n = 1; | ||
| 463 | my $x_tick = $self->_ps_time_to_x ($self->{t_base} + $n); | ||
| 464 | while ($x_tick < $x_max) { | ||
| 465 | $p->line($x_tick,$y, $x_tick,$y-1); | ||
| 466 | $p->text($x_tick-3, $y-4, "+${n}s"); | ||
| 467 | |||
| 468 | $x_tick = $self->_ps_time_to_x ($self->{t_base} + ++$n); | ||
| 469 | } | ||
| 470 | |||
| 471 | return; | ||
| 472 | |||
| 473 | # Y axis | ||
| 474 | $x -= 2.5; | ||
| 475 | $y += 2.5; | ||
| 476 | $p->line($x,$y-0.25, $x,$y + $self->{const}{session_h}); | ||
| 477 | $p->line($x,$y, $x-1,$y); # First tick | ||
| 478 | |||
| 479 | my $pkts_per_mm = 1 / $self->{const}{tbox_h}; | ||
| 480 | my $cols_per_sec = 1000000 / $self->{tsnap}; | ||
| 481 | |||
| 482 | #### This furniture doesn't seem that useful at the mo | ||
| 483 | # | ||
| 484 | # A perfect 56K modem is 33,600 bits/sec in one dir, == 4.2K, or 3 packets | ||
| 485 | # ADSL is 512,000 bits/sec downstream, == 64K, or 48 packets | ||
| 486 | # Ethernet is 100Mb/sec, == 12 MB /sec, 6000 packets/sec (ish) | ||
| 487 | #my $y_56k = $y + $pkts_per_mm * ( 3 / $cols_per_sec); | ||
| 488 | #my $y_ADSL = $y + $pkts_per_mm * ( 48 / $cols_per_sec); | ||
| 489 | #my $y_ethernet = $y + $pkts_per_mm * (6000 / $cols_per_sec); | ||
| 490 | #$p->line($x,$y_56k, $x-1,$y_56k); | ||
| 491 | #$p->line($x,$y_ADSL, $x-1,$y_ADSL); | ||
| 492 | #$p->line($x,$y_ethernet, $x-1,$y_ethernet); | ||
| 493 | #print "$y, $y_56k, $y_ADSL, $y_ethernet ($pkts_per_mm, $cols_per_sec)\n"; | ||
| 494 | } | ||
| 495 | |||
| 496 | # }}} | ||
| 497 | # {{{ _ps_box | ||
| 498 | |||
| 499 | sub _ps_box { | ||
| 500 | my ($self, $o, $box) = @_; | ||
| 501 | my ($p) = $self->{ps}; | ||
| 502 | |||
| 503 | my ($height) = $self->{const}{session_h} - $self->{const}{session_pad_v} ; | ||
| 504 | |||
| 505 | my ($s,$us) = $self->{t_base}->numbers(); | ||
| 506 | |||
| 507 | my $box_x1 = $self->_ps_time_to_x ($box->{t_start}); | ||
| 508 | my $t_end = $box->{t_end}; | ||
| 509 | $t_end->round_usec ($self->{tsnap}, 'up'); | ||
| 510 | my $box_x2 = $self->_ps_time_to_x ($t_end, 'no snap'); | ||
| 511 | my $box_w = $box_x2 - $box_x1; | ||
| 512 | |||
| 513 | $p->setlinewidth ( 0.1 ); | ||
| 514 | $p->setcolour ($self->_ps_get_box_colour()); | ||
| 515 | $self->_ps_rect ([$box_x1, $o->[Y]], $box_w, $height, 'filled'); | ||
| 516 | |||
| 517 | my ($req_pkts); | ||
| 518 | my $uri_offset = -1.6 ; | ||
| 519 | for my $hx (@{$box->{http_transactions}}) { | ||
| 520 | my $x1 = $self->_ps_time_to_x ($hx->{t_xact_start}); | ||
| 521 | my $width = $self->_ps_time_to_x ($hx->{t_xact_end}) - $x1 +1;#+1 for final column | ||
| 522 | |||
| 523 | # Set colour based on $hx->{class} | ||
| 524 | $p->setcolour(128,128,128); # Slopey colour | ||
| 525 | |||
| 526 | $p->setlinewidth( 0.1 ); | ||
| 527 | $self->_ps_slopey ([$x1, $o->[Y]], $width, $height); | ||
| 528 | |||
| 529 | # Print URL | ||
| 530 | push (@{$self->{key}{uris}}, $hx->{uri}); | ||
| 531 | my $n = scalar (@{$self->{key}{uris}}); | ||
| 532 | $p->setfont("Helvetica", 5); | ||
| 533 | $p->setcolour(0,0,0); # Packet colour | ||
| 534 | $p->text($x1, $o->[Y]+$uri_offset, $n); | ||
| 535 | } | ||
| 536 | |||
| 537 | $self->_ps_box_packets ($o, $box->{pkt_stack}); | ||
| 538 | } | ||
| 539 | |||
| 540 | # }}} | ||
| 541 | # {{{ _ps_box_packets | ||
| 542 | |||
| 543 | sub _ps_box_packets { | ||
| 544 | my ($self, $o, $stack) = @_; | ||
| 545 | |||
| 546 | for my $s (sort keys %$stack) { | ||
| 547 | for my $us (sort keys (%{$stack->{$s}})) { | ||
| 548 | my $x = $self->_ps_time_to_x (Net::Analysis::Time->new($s,$us)); | ||
| 549 | my $y = $o->[Y]; | ||
| 550 | my $pkts = $stack->{$s}{$us}; | ||
| 551 | |||
| 552 | foreach my $pkt (@$pkts) { | ||
| 553 | $self->_ps_packet ([$x, $y], $pkt); | ||
| 554 | $y += $self->{const}{tbox_h}; | ||
| 555 | } | ||
| 556 | } | ||
| 557 | } | ||
| 558 | } | ||
| 559 | |||
| 560 | # }}} | ||
| 561 | # {{{ _ps_packet | ||
| 562 | |||
| 563 | sub _ps_packet { | ||
| 564 | my ($self, $o, $pkt) = @_; | ||
| 565 | my $p = $self->{ps}; | ||
| 566 | my ($x,$y) = @$o; | ||
| 567 | my $type = $pkt->{class}; | ||
| 568 | |||
| 569 | my ($w ,$h) = ($self->{const}{packet_w}, $self->{const}{packet_h}); | ||
| 570 | |||
| 571 | # Base packet | ||
| 572 | $p->setlinewidth( 0.01 ); | ||
| 573 | $p->setcolour(0,0,0); # Normal packet colour | ||
| 574 | |||
| 575 | if ($type == PKT_DUP_DATA) { | ||
| 576 | $p->setcolour(128,0,0); | ||
| 577 | $self->_ps_rect ($o, $w, $h, 'filled'); | ||
| 578 | |||
| 579 | #$p->setcolour(255,255,255); | ||
| 580 | #$p->line ($x,$y, $x+$w, $y+$h); | ||
| 581 | #$p->line ($x+$w,$y, $x, $y+$h); | ||
| 582 | |||
| 583 | } elsif ($type == PKT_FUTURE_DATA) { | ||
| 584 | $p->setcolour(0,0,128); | ||
| 585 | $self->_ps_rect ($o, $w, $h, 'filled'); | ||
| 586 | |||
| 587 | # Draw a single line | ||
| 588 | #$p->setcolour(255,255,255); | ||
| 589 | #$p->line ($x+$w,$y, $x, $y+$h); | ||
| 590 | |||
| 591 | } elsif ($type == PKT_NONDATA) { | ||
| 592 | $self->_ps_rect ($o, $w, $h); | ||
| 593 | |||
| 594 | } elsif ($type == PKT_DATA) { | ||
| 595 | my $dest_port; | ||
| 596 | ($dest_port) = ($pkt->{to} =~ /:([\d]+)$/); | ||
| 597 | |||
| 598 | if ($dest_port < 1024) { | ||
| 599 | # Assume to be a request packet | ||
| 600 | my ($radius) = ( ($w < $h) ? $w : $h ) / 2.0; | ||
| 601 | $p->circle( {filled => 1}, $x+$w/2.0, $y+$h/2.0, $radius); | ||
| 602 | } else { | ||
| 603 | # Assume to be a response packet | ||
| 604 | $self->_ps_rect ($o, $w, $h, 'filled'); | ||
| 605 | } | ||
| 606 | |||
| 607 | } else { | ||
| 608 | carp "how to render $type packet ?\n"; | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | # }}} | ||
| 613 | # {{{ _ps_rect | ||
| 614 | |||
| 615 | sub _ps_rect { | ||
| 616 | my ($self, $o, $w, $h, $filled) = @_; | ||
| 617 | my $p = $self->{ps}; | ||
| 618 | |||
| 619 | my $opt = {offset => $o}; | ||
| 620 | $opt->{filled} = 1 if ($filled); | ||
| 621 | |||
| 622 | $p->polygon($opt, 0,0, $w,0, $w,$h, 0,$h, 0,0); | ||
| 623 | } | ||
| 624 | |||
| 625 | # }}} | ||
| 626 | # {{{ _ps_slopey | ||
| 627 | |||
| 628 | sub _ps_slopey { | ||
| 629 | my ($self, $o, $w, $h, $filled) = @_; | ||
| 630 | my $p = $self->{ps}; | ||
| 631 | |||
| 632 | my $opt = {offset => $o}; | ||
| 633 | $opt->{filled} = 1 if ($filled); | ||
| 634 | |||
| 635 | $p->polygon($opt, 0,0, $w,0, $w,$h*0.8, 0,$h, 0,0); | ||
| 636 | } | ||
| 637 | |||
| 638 | # }}} | ||
| 639 | |||
| 640 | # {{{ _ps_time_to_x | ||
| 641 | |||
| 642 | # This handy routine takes a time, and works out the PostScript X position. | ||
| 643 | # Snaps into a timebox by default, but optional last parameter prevents this. | ||
| 644 | |||
| 645 | sub _ps_time_to_x { | ||
| 646 | my ($self, $time, $nosnap) = @_; | ||
| 647 | my $o = $self->{const}{origin}; | ||
| 648 | my $tbox_width = $self->{const}{tbox_w}; | ||
| 649 | |||
| 650 | my $diff = $time - $self->{t_base}; | ||
| 651 | $diff->round_usec ($self->{tsnap}) unless ($nosnap); | ||
| 652 | |||
| 653 | return $o->[X] + $tbox_width * ($diff->usec() / $self->{tsnap}); | ||
| 654 | } | ||
| 655 | |||
| 656 | # }}} | ||
| 657 | # {{{ _ps_box_height | ||
| 658 | |||
| 659 | sub _ps_box_height { | ||
| 660 | my ($self, $box) = @_; | ||
| 661 | |||
| 662 | my $slots = $self->{session_slots}; | ||
| 663 | |||
| 664 | my $slot_n; | ||
| 665 | foreach my $n (0 .. $#$slots + 1) { | ||
| 666 | # Fill in next empty slot, or nextr slot that is now clear | ||
| 667 | if (! $slots->[$n] || $box->{t_start} > $slots->[$n]) { | ||
| 668 | $slots->[$n] = $box->{t_end}; | ||
| 669 | $slot_n = $n; | ||
| 670 | last; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | $slot_n++; # Leave space for the cumulative count | ||
| 675 | |||
| 676 | return ($self->{const}{origin}[Y] + 4 + $slot_n * $self->{const}{session_h}); | ||
| 677 | } | ||
| 678 | |||
| 679 | # }}} | ||
| 680 | # {{{ _ps_key | ||
| 681 | |||
| 682 | sub _ps_key { | ||
| 683 | my ($self) = @_; | ||
| 684 | my ($p) = $self->{ps}; | ||
| 685 | |||
| 686 | my ($x, $y) = ($self->{const}{origin}[X], 195); | ||
| 687 | my $n = 1; | ||
| 688 | my ($req_b, $resp_b) = (0,0); | ||
| 689 | |||
| 690 | # URL list | ||
| 691 | $p->setcolour(0,0,0); | ||
| 692 | $p->setfont("Courier", 5); | ||
| 693 | |||
| 694 | my $boxes = $self->{closed_boxes}; | ||
| 695 | my $sorter = box_sorter($boxes); | ||
| 696 | foreach my $box_k (sort $sorter keys %$boxes) { | ||
| 697 | my $box = $self->{closed_boxes}{$box_k}; | ||
| 698 | |||
| 699 | for my $hx (@{$box->{http_transactions}}) { | ||
| 700 | $p->text($x, $y - $n*2, sprintf ("% 3d) % 6db %s", $n, | ||
| 701 | $hx->{resp_size}, | ||
| 702 | "http://$hx->{uri}")); | ||
| 703 | $req_b += $hx->{req_size}; | ||
| 704 | $resp_b += $hx->{resp_size}; | ||
| 705 | $n++; | ||
| 706 | } | ||
| 707 | } | ||
| 708 | $p->text ($x, $y - $n*2, sprintf ("--- % 7db (+% 05db in reqs)", | ||
| 709 | $resp_b, $req_b)); | ||
| 710 | |||
| 711 | # Key | ||
| 712 | my ($height) = $self->{const}{session_h} - $self->{const}{session_pad_v} ; | ||
| 713 | ($x, $y) = ($self->{const}{origin}[X]+220, 195); | ||
| 714 | |||
| 715 | $p->setlinewidth( 0.2 ); | ||
| 716 | $p->setcolour (255,255,255); | ||
| 717 | $self->_ps_rect ([$x-6, $y-$height-6], 57, $height+9, 'filled'); | ||
| 718 | |||
| 719 | $p->setcolour (0,0,0); | ||
| 720 | $self->_ps_rect ([$x-5, $y-$height-5], 55, $height+7,); | ||
| 721 | |||
| 722 | $p->setfont("Helvetica", 12); | ||
| 723 | $p->setcolour (0,0,0); | ||
| 724 | $p->text ($x-14, $y-3, "Key"); | ||
| 725 | |||
| 726 | $p->setfont("Helvetica", 6); | ||
| 727 | |||
| 728 | # Session box | ||
| 729 | $p->setcolour ($self->_ps_get_box_colour()); | ||
| 730 | $self->_ps_rect ([$x, $y-$height], 5, $height, 'filled'); | ||
| 731 | $p->setcolour (0,0,0); | ||
| 732 | $p->text ($x-3, $y-$height-3, "TCP session"); | ||
| 733 | |||
| 734 | # HTTP transaction | ||
| 735 | $x += 14; | ||
| 736 | $p->setcolour(128,128,128); | ||
| 737 | $p->setlinewidth( 0.1 ); | ||
| 738 | $self->_ps_slopey ([$x, $y-$height], 5, $height); | ||
| 739 | $p->setcolour (0,0,0); | ||
| 740 | $p->text ($x-3, $y-$height-3, "HTTP request"); | ||
| 741 | |||
| 742 | # Packet types | ||
| 743 | $x += 14; | ||
| 744 | $y -= 4; | ||
| 745 | $self->_ps_packet ([$x, $y], {class => PKT_DATA, to => '1.2.3.4:80'}); | ||
| 746 | $p->setcolour (0,0,0); | ||
| 747 | $p->text ($x+2, $y, 'request packet'); | ||
| 748 | |||
| 749 | $self->_ps_packet ([$x, $y-3], {class => PKT_DATA, to => '1.2.3.4:3333'}); | ||
| 750 | $p->setcolour (0,0,0); | ||
| 751 | $p->text ($x+2, $y-3, 'response packet'); | ||
| 752 | |||
| 753 | $self->_ps_packet ([$x, $y-6], {class => PKT_FUTURE_DATA}); | ||
| 754 | $p->setcolour (0,0,0); | ||
| 755 | $p->text ($x+2, $y-6, 'out of order packet'); | ||
| 756 | |||
| 757 | $self->_ps_packet ([$x, $y-9], {class => PKT_DUP_DATA}); | ||
| 758 | $p->setcolour (0,0,0); | ||
| 759 | $p->text ($x+2, $y-9, 'duplicate packet'); | ||
| 760 | |||
| 761 | if (exists $self->{show_all_packets}) { | ||
| 762 | $self->_ps_packet ([$x, $y-12], {class => PKT_NONDATA}); | ||
| 763 | $p->setcolour (0,0,0); | ||
| 764 | $p->text ($x+2, $y-12, 'non-data packet'); | ||
| 765 | } | ||
| 766 | } | ||
| 767 | |||
| 768 | # }}} | ||
| 769 | # {{{ _ps_get_box_colour | ||
| 770 | |||
| 771 | { | ||
| 772 | my ($col_n) = 0; | ||
| 773 | my (@pastels) = ([255, 230, 230], | ||
| 774 | [230, 255, 230], | ||
| 775 | [230, 230, 255], | ||
| 776 | |||
| 777 | [255, 255, 230], | ||
| 778 | [255, 230, 255], | ||
| 779 | [230, 255, 255], | ||
| 780 | |||
| 781 | [210, 230, 255], | ||
| 782 | [210, 255, 230], | ||
| 783 | [230, 210, 255], | ||
| 784 | [230, 255, 210], | ||
| 785 | [255, 210, 230], | ||
| 786 | [255, 230, 210], | ||
| 787 | ); | ||
| 788 | |||
| 789 | sub _ps_get_box_colour { | ||
| 790 | return @{$pastels[$col_n++ % scalar(@pastels)]}; | ||
| 791 | } | ||
| 792 | } | ||
| 793 | |||
| 794 | # }}} | ||
| 795 | |||
| 796 | # {{{ box_sorter | ||
| 797 | |||
| 798 | sub box_sorter { | ||
| 799 | my ($boxes) = @_; | ||
| 800 | |||
| 801 | return sub { $boxes->{$a}{t_start} <=> $boxes->{$b}{t_start} }; | ||
| 802 | } | ||
| 803 | |||
| 804 | # }}} | ||
| 805 | |||
| 806 | 1; | ||
| 807 | __END__ |