[Catalyst-commits] r7088 -
trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket
andyg at dev.catalyst.perl.org
andyg at dev.catalyst.perl.org
Thu Nov 1 17:14:54 GMT 2007
Author: andyg
Date: 2007-11-01 17:14:53 +0000 (Thu, 01 Nov 2007)
New Revision: 7088
Modified:
trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm
Log:
View some engine stats by going to /poe_engine_stats
Modified: trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm
===================================================================
--- trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm 2007-11-01 00:55:24 UTC (rev 7087)
+++ trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm 2007-11-01 17:14:53 UTC (rev 7088)
@@ -42,6 +42,11 @@
$config->{idle_timeout} = 30; # kill idle children > min_spare after this time
}
+ # Enable stats webpage?
+ if ( !defined $config->{enable_stats} ) {
+ $config->{enable_stats} = 1;
+ }
+
if ( HAS_AIO ) {
# Try to serve everything under /static using AIO
$config->{aio_static} ||= 1;
@@ -59,18 +64,21 @@
my $self = $class->SUPER::new(
name => 'Catalyst Server',
config => $config,
- mime => MIME::Types->new( only_complete => 1 ),
children => {}, # child wheels
child_busy => {}, # empty for idle children, otherwise contains $con
stats => {
- started => time(),
- num_reqs => 0,
+ started => time(),
+ num_reqs => 0,
+ num_static => 0,
},
stats_child => {}, # per-child stats
);
- # preload the type index hash so it's not built on the first request
- $self->{mime}->create_type_index;
+ if ( $config->{aio_static} ) {
+ $self->{mime} = MIME::Types->new( only_complete => 1 );
+ # preload the type index hash so it's not built on the first request
+ $self->{mime}->create_type_index;
+ }
POE::Session->create(
object_states => [
@@ -112,9 +120,7 @@
$kernel->sig( HUP => 'restart' );
# dump state on USR1
- if ( DEBUG ) {
- $kernel->sig( USR1 => 'dump_state' );
- }
+ $kernel->sig( USR1 => 'dump_state' );
# Fork child proc(s)
for ( 1 .. $self->{config}->{start_servers} ) {
@@ -169,6 +175,7 @@
$self->{child_busy}->{ $wheel->ID } = 0;
$self->{stats_child}->{ $wheel->ID } = {
+ pid => $wheel->PID,
started => time(),
num_reqs => 0,
last_req => 0, # last time child handled a request
@@ -390,12 +397,21 @@
my $file = $self->{config}->{aio_static_path} . '/' . uri_unescape($1);
DEBUG && warn "[$con] directly serving static file $file\n";
+ $self->{stats}->{num_static}++;
+
$con->x->{_req} = HTTP::Request->parse( $input );
aio_stat( $file, $con->callback( 'stat_file', $file ) );
return;
}
+ if ( $self->{config}->{enable_stats} && $input =~ m{^GET /poe_engine_stats} ) {
+ $con->x->{_req} = HTTP::Request->parse( $input );
+ $con->call( 'display_stats' );
+
+ return 1;
+ }
+
# Find a free child to send the request to
for my $wheel_id ( keys %{ $self->{children} } ) {
next if $self->{child_busy}->{ $wheel_id };
@@ -460,7 +476,52 @@
# Send the data along to the child for handling
$self->{children}->{ $wheel_id }->put( $conn . $$input );
}
+
+sub display_stats {
+ my ( $self, $server, $con ) = @_;
+ my $time = time();
+
+ my $total_reqs = $self->{stats}->{num_reqs};
+ my $static_reqs = $self->{stats}->{num_static};
+ my $uptime = ( $time - $self->{stats}->{started} ) || 1;
+ my $req_per_sec = sprintf "%.2f", ( $total_reqs / $uptime );
+
+ my $output = qq{
+<html><body>
+ <h2>Catalyst POE Engine Stats:</h2><hr />
+ <table border="1">
+ <tr><td>Uptime:</td><td>$uptime sec</td></tr>
+ <tr><td>Total Requests:</td><td>$total_reqs</td></tr>
+ <tr><td>AIO Static Requests:</td><td>$static_reqs</td></tr>
+ <tr><td>Requests/second:</td><td>$req_per_sec</td></tr>
+ </table>
+ <hr />
+ <h3>Children:</h3>
+ <table border="1">
+ <tr><th>ID</th><th>PID</th><th>Uptime</th><th>Requests</th><th>Requests/sec</th><th>Idle</th></tr>
+};
+
+ while ( my ($id, $child) = each %{ $self->{stats_child} } ) {
+ my $pid = $child->{pid};
+ my $uptime = ( $time - $child->{started} ) || 1;
+ my $num_reqs = $child->{num_reqs};
+ my $reqs_per_sec = sprintf "%.2f", ( $num_reqs / $uptime );
+ my $idle = ( $child->{last_req} ) ? ( $time - $child->{last_req} ) : $uptime;
+
+ $output .= qq{
+ <tr><td>$id</td><td>$pid</td><td>$uptime sec</td><td>$num_reqs</td><td>$reqs_per_sec</td><td>$idle</td></tr>
+ };
+ }
+
+ $output .= qq{
+ </table>
+</body></html>
+ };
+
+ $con->call( simple_response => 200, $output );
+}
+
### Child handlers
sub child_error {
@@ -614,12 +675,12 @@
}
sub simple_response {
- my ( $self, $server, $con, $code ) = @_;
+ my ( $self, $server, $con, $code, $content ) = @_;
my $r = $con->x->{_r} ||= HTTP::Response->new();
$r->code( $code );
- my $content = status_message($code);
+ $content ||= status_message($code);
$r->content_type( 'text/html' );
$con->call( static_finish => $content );
More information about the Catalyst-commits
mailing list