Skip to content

Instantly share code, notes, and snippets.

/timer.diff Secret

Created August 10, 2014 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/47928ea73169b0fc7e7d to your computer and use it in GitHub Desktop.
Save anonymous/47928ea73169b0fc7e7d to your computer and use it in GitHub Desktop.
diff --git a/lib/ojo.pm b/lib/ojo.pm
index 05b5337..6ea158a 100644
--- a/lib/ojo.pm
+++ b/lib/ojo.pm
@@ -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
@@ -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', @_) },
@@ -50,6 +52,12 @@ sub _request {
return $tx->res;
}
+sub _timer (&) {
+ my $start = [gettimeofday];
+ shift->();
+ return sprintf '%f', tv_interval($start, [gettimeofday]);
+}
+
1;
=encoding utf8
@@ -134,6 +142,14 @@ 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 {...};
+
+Measure time it took to execture block in floating seconds.
+
+ $ perl -Mojo -E 'say i { say g("mojolicio.us")->code }'
+
=head2 j
my $bytes = j([1, 2, 3]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment