Skip to content

Commit 63f467e

Browse files
authored
Perl: Fix non-functional benchmarks (TechEmpower#9151)
* Perl: unify the maximum requests per child value * Fix Perl Dancer * Fix Perl Web::Simple * Fix Perl Plack * Fix Perl Mojolicious * Perl Kelp: provide a default benchmark to silence the warning * Perl Mojolicious: minor adjustments
1 parent 3a6fcdb commit 63f467e

20 files changed

+256
-247
lines changed

frameworks/Perl/dancer/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
* Dancer
1010
* Dancer::Plugin::Database
11-
* DBD::mysql
11+
* DBD::MariaDB
1212
* Starman (if using Starman as web server)
1313
* Plack (for plackup)
1414
* nginx (if you want to front Dancer with nginx, nginx.conf provided)
@@ -22,3 +22,4 @@ Something along the lines of
2222
if you want to front it with nginx, otherwise
2323

2424
plackup -E production -s Starman --port=8080 --workers=2 -a ./app.pl
25+

frameworks/Perl/dancer/app.pl

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
set serializer => 'JSON';
1010

11-
my $dsn = "dbi:mysql:database=hello_world;host=tfb-database;port=3306";
12-
my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', { mysql_auto_reconnect=>1 } );
11+
my $dsn = "dbi:MariaDB:database=hello_world;host=tfb-database;port=3306";
12+
my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass' );
1313
my $sth = $dbh->prepare("SELECT * FROM World where id = ?");
1414

1515
get '/json' => sub {
@@ -20,7 +20,7 @@
2020
my $queries = params->{queries} || 1;
2121
$queries = 1 if ( $queries !~ /^\d+$/ || $queries < 1 );
2222
$queries = 500 if $queries > 500;
23-
23+
2424
my @response;
2525
for ( 1 .. $queries ) {
2626
my $id = int rand 10000 + 1;
@@ -42,3 +42,4 @@
4242
};
4343

4444
Dancer->dance;
45+

frameworks/Perl/dancer/benchmark_config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"display_name": "dancer",
2020
"notes": "",
2121
"versus": "",
22-
"tags": ["broken"]
22+
"tags": []
2323
}
2424
}]
2525
}
26+

frameworks/Perl/dancer/dancer.dockerfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM perl:5.26
1+
FROM perl:5.40
22

33
RUN apt-get update -yqq && apt-get install -yqq nginx
44

@@ -10,12 +10,13 @@ RUN cpanm --notest --no-man-page \
1010
1111
Dancer::Plugin::[email protected] \
1212
13-
13+
1414
1515
1616
1717

1818
EXPOSE 8080
1919

2020
CMD nginx -c /dancer/nginx.conf && \
21-
plackup -E production -s Starman --workers=$(nproc) -l /tmp/perl-dancer.sock -a ./app.pl
21+
plackup -E production -s Starman --workers=$(nproc) --max-requests=100000 -l /tmp/perl-dancer.sock -a ./app.pl
22+

frameworks/Perl/kelp/benchmark_config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"framework": "kelp",
33
"tests": [{
4-
"gazelle-mysql": {
4+
"default": {
55
"dockerfile": "kelp.dockerfile",
66
"plaintext_url": "/plaintext",
77
"json_url": "/json",

frameworks/Perl/kelp/run.pl

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
],
6363
);
6464

65+
# default is gazelle-mysql (techempower will warn if there is no default)
66+
$test_name = 'kelp-gazelle-mysql'
67+
if $test_name eq 'kelp';
68+
6569
die "invalid test name $test_name"
6670
unless $test_name =~ m{^kelp-(\w+)-(\w+)$};
6771

frameworks/Perl/mojolicious/app.pl

+51-65
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,101 @@
1+
use v5.36;
12
use Mojolicious::Lite;
23
use Mojo::Pg;
34
use Mojo::Promise;
45

5-
use Cpanel::JSON::XS 'encode_json';
66
use Scalar::Util 'looks_like_number';
7-
use Data::Dumper;
87

98
# configuration
109

10+
use constant MAX_DB_CONCURRENCY => 50;
11+
1112
{
1213
my $nproc = `nproc`;
1314
app->config(hypnotoad => {
14-
accepts => 0,
15-
clients => int( 256 / $nproc ) + 1,
15+
accepts => 100000,
16+
clients => MAX_DB_CONCURRENCY,
1617
graceful_timeout => 1,
1718
requests => 10000,
1819
workers => $nproc,
1920
backlog => 256
2021
});
2122
}
2223

23-
{
24-
my $db_host = 'tfb-database';
25-
helper pg => sub { state $pg = Mojo::Pg->new('postgresql://benchmarkdbuser:benchmarkdbpass@' . $db_host . '/hello_world')->max_connections(50) };
26-
}
27-
28-
helper render_json => sub {
29-
my $c = shift;
30-
$c->res->headers->content_type('application/json');
31-
$c->render( data => encode_json(shift) );
32-
};
33-
3424
# Routes
3525

36-
get '/json' => sub { shift->helpers->render_json({message => 'Hello, World!'}) };
26+
get '/json' => sub ($c) {
27+
$c->render(json => {message => 'Hello, World!'});
28+
};
3729

38-
get '/db' => sub { shift->helpers->render_query(1, {single => 1}) };
30+
get '/db' => sub ($c) {
31+
$c->helpers->render_query(1, {single => 1});
32+
};
3933

40-
get '/queries' => sub {
41-
my $c = shift;
34+
get '/queries' => sub ($c) {
4235
$c->helpers->render_query(scalar $c->param('queries'));
4336
};
4437

45-
get '/fortunes' => sub {
46-
my $c = shift;
38+
get '/fortunes' => sub ($c) {
4739
$c->render_later;
48-
my $docs = $c->helpers->pg->db->query_p('SELECT id, message FROM Fortune')
49-
->then(sub{
50-
my $docs = $_[0]->arrays;
51-
push @$docs, [0, 'Additional fortune added at request time.'];
52-
$c->render(fortunes => docs => $docs->sort(sub{ $a->[1] cmp $b->[1] }) )
53-
});
40+
41+
$c->helpers->pg->db->query_p('SELECT id, message FROM Fortune')
42+
->then(sub ($query) {
43+
my $docs = $query->arrays;
44+
push @$docs, [0, 'Additional fortune added at request time.'];
45+
46+
$c->render(fortunes => docs => $docs->sort(sub { $a->[1] cmp $b->[1] }));
47+
});
5448
};
5549

56-
get '/updates' => sub {
57-
my $c = shift;
50+
get '/updates' => sub ($c) {
5851
$c->helpers->render_query(scalar $c->param('queries'), {update => 1});
5952
};
6053

6154
get '/plaintext' => { text => 'Hello, World!', format => 'txt' };
6255

6356
# Additional helpers (shared code)
6457

65-
helper 'render_query' => sub {
66-
my ($self, $q, $args) = @_;
58+
helper pg => sub {
59+
state $pg = Mojo::Pg
60+
->new('postgresql://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world')
61+
->max_connections(MAX_DB_CONCURRENCY + 1);
62+
};
63+
64+
helper 'render_query' => sub ($self, $q, $args = {}) {
6765
$self->render_later;
68-
$args ||= {};
69-
my $update = $args->{update};
7066

7167
$q = 1 unless looks_like_number($q);
7268
$q = 1 if $q < 1;
7369
$q = 500 if $q > 500;
7470

75-
my $r = [];
76-
my $tx = $self->tx;
71+
Mojo::Promise->map({concurrency => MAX_DB_CONCURRENCY}, sub {
72+
my $db = $self->helpers->pg->db;
73+
my $id = 1 + int rand 10_000;
7774

75+
my $query = $db->query('SELECT id, randomnumber FROM World WHERE id=?', $id);
76+
my $number = $query->array->[1];
7877

79-
my @queries;
80-
foreach (1 .. $q) {
81-
my $id = 1 + int rand 10_000;
78+
if ($args->{update}) {
79+
$number = 1 + int rand 10_000;
80+
$db->query('UPDATE World SET randomnumber=? WHERE id=?', $number, $id);
81+
}
8282

83-
push @queries, $self->helpers->pg->db->query_p('SELECT id,randomnumber FROM World WHERE id=?', $id)
84-
->then(sub{
85-
my $randomNumber = $_[0]->array->[0];
86-
87-
return Mojo::Promise->new->resolve($id, $randomNumber)
88-
->then(sub{
89-
if($update) {
90-
$randomNumber = 1 + int rand 10_000;
91-
return Mojo::Promise->all(
92-
Mojo::Promise->new->resolve($_[0], $randomNumber),
93-
$self->helpers->pg->db->query_p('UPDATE World SET randomnumber=? WHERE id=?', $randomNumber, $id)
94-
)
95-
->then(sub {
96-
return $_[0];
97-
})
98-
}
99-
return [shift, shift];
100-
})
101-
});
102-
}
83+
return Mojo::Promise->resolve([$id, $number]);
84+
}, 1 .. $q)
85+
->then(sub (@responses) {
86+
my @results;
10387

104-
Mojo::Promise->all(@queries)
105-
->then(sub{
106-
my @responses = @_;
10788
foreach my $resp (@responses) {
108-
push @$r, { id => $resp->[0][0], randomNumber => $resp->[0][1] };
89+
push @results, { id => $resp->[0][0], randomNumber => $resp->[0][1] };
10990
}
110-
$r = $r->[0] if $args->{single};
111-
$self->helpers->render_json($r);
112-
})
11391

92+
if ($args->{single}) {
93+
$self->render(json => $results[0]);
94+
}
95+
else {
96+
$self->render(json => \@results);
97+
}
98+
});
11499
};
115100

116101
app->start;
@@ -133,3 +118,4 @@
133118
</table>
134119
</body>
135120
</html>
121+

frameworks/Perl/mojolicious/cpanfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
requires 'Mojolicious', '7.84';
22
requires 'Mojo::Pg', '4.08';
3-
requires 'Cpanel::JSON::XS', '4.02';
3+
requires 'Cpanel::JSON::XS', '4.38';
44
requires 'EV', '4.22';
55

66
recommends 'IO::Socket::IP', '0.36';
77
recommends 'IO::Socket::SSL';
8+

0 commit comments

Comments
 (0)