Acme-Parataxis
view release on metacpan or search on metacpan
eg/db_driver.pl view on Meta::CPAN
}
else {
my $r = await_read( $s, 5000 );
return if !defined $r || $r <= 0;
my $n = sysread( $s, my $chunk, 65536 );
return if !defined $n || $n == 0;
$dbh->{buf} .= $chunk;
}
}
return substr( $dbh->{buf}, 0, $i + 1, '' );
}
# The blocking part of the driver, and the one CV every concurrent fiber enters
# and suspends inside. The counter opens before the first wait, so it brackets
# the whole residency of a fiber here rather than just the tail of it, and
# every exit runs through the single return so the count cannot drift.
sub read_reply {
my ($dbh) = @_;
$INSIDE++;
$INSIDE_MAX = $INSIDE if $INSIDE > $INSIDE_MAX;
my ( @rows, $ok );
if ( defined( my $head = _line($dbh) ) ) {
chomp $head;
if ( $head =~ /\A[0-9]+\z/ ) {
my $whole = 1;
for ( 1 .. $head ) {
my $l = _line($dbh);
if ( !defined $l ) { $whole = 0; last }
chomp $l;
push @rows, $l;
}
my $end = $whole ? _line($dbh) : undef;
$ok = $whole && defined $end && $end eq ".\n";
}
}
$INSIDE--;
return $ok ? \@rows : undef;
}
sub dbh_selectall {
my ( $dbh, $sql ) = @_;
return if !_put( $dbh, "QUERY $sql\n" );
return read_reply($dbh);
}
sub dbh_disconnect {
my ($dbh) = @_;
close $dbh->{sock};
}
my ( $pfh, $ppath ) = tempfile( 'parataxis-db-XXXXXX', TMPDIR => 1, UNLINK => 1 );
close $pfh;
# The database program goes in a file, not on a -e. Handing a multi-line program
# to the child as one command-line argument means trusting the platform to keep
# the newlines through the round trip, and it does not: on Windows the argument
# is re-split and the child was handed "use" as its whole program, dying with
# "syntax error at -e line 1, at EOF". A file path cannot be mangled that way.
my ( $sfh, $spath ) = tempfile( 'parataxis-db-server-XXXXXX', TMPDIR => 1, UNLINK => 1 );
print $sfh $SERVER or die "cannot write the database program: $!";
close $sfh or die "cannot write the database program: $!";
open my $child, '-|', $^X, $spath, $ppath, $QUERY_LATENCY_MS or die "cannot start the database: $!";
for ( 1 .. 200 ) {
if ( open my $rfh, '<', $ppath ) {
chomp( my $l = <$rfh> // '' );
close $rfh;
if ( $l && $l =~ /\A([0-9]+)\z/ ) { $PORT = $1; last }
}
select undef, undef, undef, 0.05;
}
die "the database never reported a port\n" if !$PORT;
say "parataxis database driver: talking to a blocking server on 127.0.0.1:$PORT";
say sprintf ' %d queries, %d rows expected, %dms server latency per query', scalar @QUERIES, $EXPECTED_ROWS, $QUERY_LATENCY_MS;
run(
sub {
# 1. The synchronous driver. One connection, every query in sequence,
# every call blocking the whole interpreter.
my $t0 = time;
my @blocking;
my $dbh_block = dbh_connect('block') or die "connect: $!";
for my $sql (@QUERIES) {
my $rows = dbh_selectall( $dbh_block, "SELECT * FROM widgets WHERE id <= $sql" );
die "the synchronous driver got no reply\n" if !$rows;
push @blocking, @$rows;
}
dbh_disconnect($dbh_block);
my $t_block = time() - $t0;
say sprintf ' synchronous : %d rows in %6.3fs (%.1fms per query, one call at a time)', scalar @blocking, $t_block,
$t_block * 1000 / scalar @QUERIES;
# 2. The asynchronous driver. Same protocol, same SQL, a parked
# transport, and one connection per query so every wait is in flight
# at the same time.
( $INSIDE, $INSIDE_MAX ) = ( 0, 0 );
my $t1 = time;
my @jobs = map {
spawn(
sub {
my $sql = $QUERIES[$_];
my $dbh = dbh_connect('park') or die "connect: $!";
my $rows = dbh_selectall( $dbh, "SELECT * FROM widgets WHERE id <= $sql" );
dbh_disconnect($dbh);
$rows;
}
);
} 0 .. $#QUERIES;
my @parked;
for my $j (@jobs) {
my $rows = $j->await;
die "a parked query never came back\n" if !$rows;
push @parked, @$rows;
}
my $t_park = time() - $t1;
say sprintf ' asynchronous: %d rows in %6.3fs (%.1fms per query, all in flight at once)', scalar @parked, $t_park,
$t_park * 1000 / scalar @QUERIES;
say sprintf ' %d fibers were resident in the one shared closure read_reply() at the same time', $INSIDE_MAX;
say '';
# 3. The point of the demo, and the self-test. The overlap is checked
# two ways on purpose: $INSIDE_MAX is the mechanism (fibers really
# were resident in the shared closure together) and is exact, while
# the wall clock is the consequence and is only given a loose bound
( run in 0.827 second using v1.01-cache-2.11-cpan-234fa220139 )