<commit_before>use v5.16;
use Plack::Builder;
use Plack::Request;
use JSON::XS 'encode_json';
use DBI;

my $dbh = DBI->connect_cached(
    'dbi:mysql:database=hello_world;host=localhost;port=3306', 
    qw(benchmarkdbuser benchmarkdbpass)
) || die $!;

my $sth = $dbh->prepare_cached('SELECT randomNumber FROM World WHERE id = ?');
my $header = [qw(Content-Type application/json)];

builder {
    mount '/json' => sub { [ 200, $header, [ encode_json({ message => 'Hello, World!' })] ] },
    mount '/db' => sub {
        my @rs = map { id => $_->[0] + 0, randomNumber => $_->[1] },
            grep exists $_->[1],
            map [$_, $sth->execute($_) && $sth->fetchrow_array],
            map int rand 10000 + 1,
            1..Plack::Request->new(shift)->param('queries')//1;
        [ 200, $header, [ encode_json( @rs > 1 ? \@rs : $rs[0] ) ] ] } };
<commit_msg>rewrote app.psgi. Do not use Plack::Builder and Plack::Response, use $dbi->selectrow_arrayref for more performance<commit_after>use strict;
use v5.16;
use utf8;
use JSON::XS qw(encode_json);
use DBI;

my $dbh = DBI->connect_cached(
    'dbi:mysql:database=hello_world;host=localhost;port=3306', 
    'benchmarkdbuser',
    'benchmarkdbpass',
    { AutoInactiveDestroy => 1, mysql_enable_utf8 => 1 }
) || die $!;

my $query = 'SELECT id, randomNumber FROM World WHERE id = ?';
my $header = [qw(Content-Type application/json)];

my $app = sub {
    my $env = shift;
    if ( $env->{PATH_INFO} eq '/json' ) {
        return [ 200, $header, [ encode_json({ message => 'Hello, World!' }) ]];
    }
    elsif ( $env->{PATH_INFO} eq '/db' ) {
        my ($n) = ($env->{QUERY_STRING} || "" ) =~ m!queries=(\d+)!;
        $n //= 1;
        my @rs = map {{id=>$_->[0]+0,randomNumber=>$_->[1]+0}} 
            map { $dbh->selectrow_arrayref($query,{},int rand 10000 + 1) } 1..$n;
        return [ 200, $header, [ encode_json( @rs > 1 ? \@rs : $rs[0] ) ]];
    }
    [ 404, [], ['not found']];
};

$app;
{'fraction_non_alphanumeric': 0.2054164537557486, 'fraction_numerical': 0.031170158405723045, 'mean_word_length': 3.130801687763713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7079579', 'n_tokens_mistral': 771, 'n_tokens_neox': 717, 'n_words': 166}