-
-
Save chy-causer/66b15f14b16b06d3d9da0e36cd36ca04 to your computer and use it in GitHub Desktop.
This file contains 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
use Mojo::Base -strict; | |
use Mojo::Pg; | |
use Time::HiRes 'time'; | |
use Mojo::JSON qw(true false); | |
my $EXPAND = 5000; | |
my $NOEXPAND = 10000; | |
my $BOOLEXPAND = 5000; | |
my $ROWS = 2000; | |
my $pg = Mojo::Pg->new("postgresql:///$ENV{DB}"); | |
sub bench { | |
my ($cb, $iterations) = @_; | |
my $before = time; | |
$cb->() for 1 .. $iterations; | |
my $elapsed = time - $before; | |
my $avg = sprintf '%.3f', $EXPAND / $elapsed; | |
say ("\tCompleted in $elapsed seconds ($avg/s)"); | |
} | |
say "Expanding $ROWS rows $EXPAND times"; | |
bench( | |
sub { $pg->db->query("SELECT '{\"hello\" : \"world\"}'::json from generate_series(1, ?)", $ROWS)->expand->hashes; }, | |
$EXPAND | |
); | |
say "Expanding $ROWS one row at a time, $EXPAND times"; | |
bench( | |
sub { | |
my $result = $pg->db->query("SELECT '{\"hello\" : \"world\"}'::json from generate_series(1, ?)", $ROWS)->expand; | |
$result->hash for 1..$ROWS; | |
}, | |
$EXPAND | |
); | |
say "Not expanding $ROWS rows, $NOEXPAND times"; | |
bench( | |
sub { $pg->db->query("SELECT '{\"hello\" : \"world\"}'::json from generate_series(1, ?)", $ROWS)->hashes; }, | |
$EXPAND | |
); | |
say "Expanding $ROWS rows of bools cast to json, $BOOLEXPAND times"; | |
bench( | |
sub { $pg->db->query("SELECT 'true'::json, 'false'::json from generate_series(1, ?)", $ROWS)->expand->hashes }, | |
$BOOLEXPAND | |
); | |
if ( $pg->can('add_expander') ) { | |
$pg->add_expander( bool => sub { shift && true || false } ); | |
say "Expanding $ROWS rows of bools cast to json with additional expander, $BOOLEXPAND times"; | |
bench( | |
sub { $pg->db->query("SELECT 'true'::json, 'false'::json from generate_series(1, ?)", $ROWS)->expand->hashes }, | |
$BOOLEXPAND | |
); | |
# Now do the same using intuitive boolean expansion | |
say "Expanding $ROWS rows of booleans with an additional expander"; | |
bench( | |
sub { $pg->db->query("SELECT true, false from generate_series(1, ?)", $ROWS)->expand->hashes }, | |
$BOOLEXPAND | |
); | |
} | |
else { | |
say '$pg does not have expander functionality'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment