<commit_before># Copyright (C) 2008-2010, <NAME>.

package Mojolicious::Plugin::RequestTimer;

use strict;
use warnings;

use base 'Mojolicious::Plugin';

use Time::HiRes ();

# I don't trust that doctor.
# I bet I've lost more patients than he's even treated.
sub register {
    my ($self, $app) = @_;

    # Start timer
    $app->plugins->add_hook(
        before_dispatch => sub {
            my ($self, $c) = @_;
            $c->stash('mojo.started' => [Time::HiRes::gettimeofday()]);
        }
    );

    # End timer
    $app->plugins->add_hook(
        after_dispatch => sub {
            my ($self, $c) = @_;
            return unless my $started = $c->stash('mojo.started');
            my $elapsed = sprintf '%f',
              Time::HiRes::tv_interval($started,
                [Time::HiRes::gettimeofday()]);
            my $rps = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
            $c->app->log->debug("Request took $elapsed seconds ($rps/s).");
        }
    );
}

1;
__END__

=head1 NAME

Mojolicious::Plugin::RequestTimer - Request Timer Plugin

=head1 SYNOPSIS

    # Mojolicious
    $self->plugin('request_timer');<commit_msg>improved log message<commit_after># Copyright (C) 2008-2010, <NAME>.

package Mojolicious::Plugin::RequestTimer;

use strict;
use warnings;

use base 'Mojolicious::Plugin';

use Time::HiRes ();

# I don't trust that doctor.
# I bet I've lost more patients than he's even treated.
sub register {
    my ($self, $app) = @_;

    # Start timer
    $app->plugins->add_hook(
        before_dispatch => sub {
            my ($self, $c) = @_;
            $c->stash('mojo.started' => [Time::HiRes::gettimeofday()]);
        }
    );

    # End timer
    $app->plugins->add_hook(
        after_dispatch => sub {
            my ($self, $c) = @_;
            return unless my $started = $c->stash('mojo.started');
            my $elapsed = sprintf '%f',
              Time::HiRes::tv_interval($started,
                [Time::HiRes::gettimeofday()]);
            my $rps     = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
            my $res     = $c->res;
            my $code    = $res->code;
            my $message = $res->message || $res->default_message;
            $c->app->log->debug("$code $message (${elapsed}s, $rps/s).");
        }
    );
}

1;
__END__

=head1 NAME

Mojolicious::Plugin::RequestTimer - Request Timer Plugin

=head1 SYNOPSIS

    # Mojolicious
    $self->plugin('request_timer');
{'fraction_non_alphanumeric': 0.16896831844029245, 'fraction_numerical': 0.011372867587327376, 'mean_word_length': 2.3328822733423547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5921922', 'n_tokens_mistral': 861, 'n_tokens_neox': 817, 'n_words': 190}