Skip to content

Commit

Permalink
Show requests per second and allow resetting the metric
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 13, 2020
1 parent 8b2cccf commit c79ae7f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
30 changes: 17 additions & 13 deletions lib/Mojolicious/Plugin/Status.pm
Expand Up @@ -10,14 +10,6 @@ use Mojo::Util 'humanize_bytes';

our $VERSION = '1.05';

my $STATS = {
info => 0,
success => 0,
redirect => 0,
client_error => 0,
server_error => 0
};

sub register {
my ($self, $app, $config) = @_;

Expand All @@ -29,7 +21,7 @@ sub register {
# Initialize cache
my $map = $self->{map} = Mojo::MemoryMap->new($config->{size});
$map->writer->store(
{processed => 0, started => time, stats => $STATS, slowest => []});
{processed => 0, started => time, stats => _stats(), slowest => []});

# Only the two built-in servers are supported for now
$app->hook(before_server_start => sub { $self->_start(@_) });
Expand Down Expand Up @@ -95,7 +87,7 @@ sub _dashboard {

my $map = $c->stash('mojo_status')->{map};
if ($c->param('reset')) {
$map->writer->change(sub { @{$_}{qw(slowest stats)} = ([], $STATS) });
$map->writer->change(sub { @{$_}{qw(slowest stats)} = ([], _stats()) });
return $c->redirect_to('mojo_status');
}

Expand All @@ -104,6 +96,7 @@ sub _dashboard {
html => sub {
$c->render(
'mojo-status/dashboard',
now => time,
usage => humanize_bytes($map->usage),
size => humanize_bytes($map->size),
activity => _activity($all),
Expand Down Expand Up @@ -176,12 +169,12 @@ sub _rendered {
my $map = $self->{map};
my $conn = $map->writer->fetch->{workers}{$$}{connections}{$id};
return unless $conn && (my $req = $conn->{request});
$req->{runtime} = time - $req->{started};
$req->{time} = time - $req->{started};
@{$req}{qw(client status worker)} = ($conn->{client}, $c->res->code, $$);

$map->writer->change(sub {
my $slowest = $_->{slowest};
@$slowest = sort { $b->{runtime} <=> $a->{runtime} } @$slowest, $req;
@$slowest = sort { $b->{time} <=> $a->{time} } @$slowest, $req;
my %seen;
@$slowest = grep { !$seen{"$_->{method} $_->{path}"}++ } @$slowest;
pop @$slowest while @$slowest > $self->{slowest};
Expand All @@ -207,7 +200,7 @@ sub _slowest {
my $str = "$req->{method} $req->{path}";
$str .= "?$req->{query}" if $req->{query};
$str .= "$req->{status}" if $req->{status};
my $time = sprintf '%.2f', $req->{runtime};
my $time = sprintf '%.2f', $req->{time};
push @table, [$time, $str, @{$req}{qw(request_id worker client started)}];
}

Expand Down Expand Up @@ -240,6 +233,17 @@ sub _start {
Mojo::IOLoop->recurring(5 => sub { $self->_resources });
}

sub _stats {
return {
started => time,
info => 0,
success => 0,
redirect => 0,
client_error => 0,
server_error => 0
};
}

sub _stream {
my ($self, $id) = @_;

Expand Down
Expand Up @@ -50,7 +50,11 @@
<i class="fas fa-stream"></i>
</th>
<th class="fit text-left noleftpad" scope="row">Requests:</th>
<td><%= $all->{processed} %></td>
% my $stats = $all->{stats};
% my $recent = $stats->{info} + $stats->{success} + $stats->{redirect}
% + $stats->{client_error} + $stats->{server_error};
% my $rps = ($recent > 1 ? $recent : 0) / ($now - $stats->{started});
<td><%= $all->{processed} %> (<%= sprintf '%.3f', $rps %>/s)</td>
</tr>
<tr>
<th class="fit text-left noleftpad" scope="row">
Expand All @@ -68,7 +72,7 @@
<div class="col-md-2">
<div class="stats">
<div class="stats-body">
<%= $all->{stats}{success} %>
<%= $all->{stats}{info} + $all->{stats}{success} %>
</div>
<div class="stats-description">Success</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions t/status.t
Expand Up @@ -35,9 +35,9 @@ get '/subprocess' => sub {
my $t = Test::Mojo->new;

# Basics
$t->get_ok('/status.json')->status_is(200)->json_is('/processed', 2);
$t->get_ok('/status.json')->status_is(200)->json_is('/processed', 0);
$t->get_ok('/')->status_is(200)->content_is('Hello Mojo!');
$t->get_ok('/status.json')->status_is(200)->json_is('/processed', 6);
$t->get_ok('/status.json')->status_is(200)->json_is('/processed', 4);

# Bundled static files
$t->get_ok('/mojo-status/bootstrap/bootstrap.js')->status_is(200)
Expand Down Expand Up @@ -68,12 +68,12 @@ $t->get_ok('/mojo-status/logo-black.png')->status_is(200)
->content_type_is('image/png');

# JSON
$t->get_ok('/status.json')->status_is(200)->json_is('/processed', 50)
$t->get_ok('/status.json')->status_is(200)->json_is('/processed', 48)
->json_has('/started')->json_has("/workers/$$/connections")
->json_has("/workers/$$/maxrss")->json_has("/workers/$$/processed")
->json_has("/workers/$$/started")->json_has("/workers/$$/stime")
->json_has("/workers/$$/utime")->json_has('/slowest/0')
->json_has('/slowest/0/runtime')->json_has('/slowest/0/path')
->json_has('/slowest/0/time')->json_has('/slowest/0/path')
->json_has('/slowest/0/request_id')->json_has('/slowest/1')
->json_has('/slowest/4')->json_hasnt('/slowest/5');

Expand Down

0 comments on commit c79ae7f

Please sign in to comment.