-
-
Save kwakwaversal/15b7f84df2ae60d4ccbb to your computer and use it in GitHub Desktop.
#perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Modern::Perl; | |
use DBI; | |
use DBD::Pg qw(:async); | |
use Coro; | |
use AnyEvent; | |
use Time::HiRes qw(time); | |
sub query($) { | |
my $start = time; | |
my $sql = shift; | |
my $dbh = DBI->connect('dbi:Pg:host=10.0.1.120;dbname=postgres'); | |
my $sth = $dbh->prepare($sql, { pg_async => PG_ASYNC }); | |
$sth->execute; | |
until ($dbh->pg_ready) { | |
my $cb = rouse_cb; | |
my $readable = AE::io ($dbh->{pg_socket}, 0, $cb); # pg_socket is readable | |
my $timeout = AE::timer (1, 0, $cb); # or one second timeout | |
rouse_wait($cb); | |
} | |
$sth->pg_result; | |
printf "Query completed in %.5fs\n", time - $start; | |
} | |
my $start = time; | |
my @threads = ( | |
async { query 'SELECT pg_sleep(1)' }, | |
async { query 'SELECT pg_sleep(1)' }, | |
async { query 'SELECT pg_sleep(1)' }, | |
); | |
$_->join for @threads; | |
printf "Program completed in %.5fs\n", time - $start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment