Skip to content

Commit

Permalink
added i function to ojo
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 10, 2014
1 parent e690351 commit a2f0556
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Changes
@@ -1,7 +1,8 @@

5.27 2014-08-10
5.27 2014-08-11
- Added support for nested helpers.
- Added get_helper method to Mojolicious::Renderer.
- Added i function to ojo.

5.26 2014-08-09
- Improved WebSocket performance.
Expand Down
18 changes: 18 additions & 0 deletions lib/ojo.pm
Expand Up @@ -5,6 +5,7 @@ use Mojo::ByteStream 'b';
use Mojo::Collection 'c';
use Mojo::DOM;
use Mojo::JSON 'j';
use Time::HiRes qw(gettimeofday tv_interval);
use Mojo::Util qw(dumper monkey_patch);

# Silent one-liners
Expand All @@ -29,6 +30,7 @@ sub import {
d => sub { _request($ua, 'DELETE', @_) },
g => sub { _request($ua, 'GET', @_) },
h => sub { _request($ua, 'HEAD', @_) },
i => \&_timer,
j => \&j,
o => sub { _request($ua, 'OPTIONS', @_) },
p => sub { _request($ua, 'POST', @_) },
Expand All @@ -50,6 +52,12 @@ sub _request {
return $tx->res;
}

sub _timer (&@) {
my $start = [gettimeofday];
$_[0]->() for 1 .. $_[1] // 1;
return sprintf '%f', tv_interval($start, [gettimeofday]);
}

1;

=encoding utf8
Expand Down Expand Up @@ -134,6 +142,16 @@ L<Mojo::Message::Response> object.
Perform C<HEAD> request with L<Mojo::UserAgent/"head"> and return resulting
L<Mojo::Message::Response> object.
=head2 i
my $seconds = i {...};
my $seconds = i {...} 100;
Measure time it took to execture block in floating seconds, with an optional
number of iterations, which defaults to C<1>.
$ perl -Mojo -E 'say i { say g("mojolicio.us")->code }'
=head2 j
my $bytes = j([1, 2, 3]);
Expand Down
8 changes: 8 additions & 0 deletions t/mojolicious/ojo.t
Expand Up @@ -9,6 +9,7 @@ BEGIN {

use Test::More;
use ojo;
use Time::HiRes 'usleep';

# Application
a('/' => sub { $_->render(data => $_->req->method . $_->req->body) })
Expand Down Expand Up @@ -49,4 +50,11 @@ is c(1, 2, 3)->join('-'), '1-2-3', 'right result';
# Dumper
is r([1, 2]), "[\n 1,\n 2\n]\n", 'right result';

# Timer
my $i = 0;
ok i { ++$i and usleep 0.025 * 1000000 } >= 0.025, 'got a time';
is $i, 1, 'block has been invoked once';
i { $i++ } 10;
is $i, 11, 'block has been invoked ten times';

done_testing();

0 comments on commit a2f0556

Please sign in to comment.