Skip to content

Commit f7dab75

Browse files
committed
Added "experiments"
1 parent 8450c66 commit f7dab75

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

experiments/bench.pl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use warnings;
2+
use 5.026;
3+
use Time::HiRes qw/gettimeofday tv_interval/;
4+
5+
my $t0 = [gettimeofday];
6+
my @primes = join ',', grep {prime($_)} 1..1000000;
7+
my $elapsed = tv_interval($t0);
8+
printf "%.3f\n", $elapsed;
9+
10+
# http://www.rosettacode.org/wiki/Primality_by_trial_division#Perl
11+
sub prime {
12+
my $n = shift;
13+
$n % $_ or return for 2 .. sqrt $n;
14+
$n > 1
15+
}
16+
17+
# A quick test: This program, when run
18+
# from WebPerl (Firefox): ~7.4s
19+
# natively (same machine): ~2.3s
20+
# => roughly 3.2 times slower

experiments/sockserv.pl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env perl
2+
use warnings;
3+
use strict;
4+
use Data::Dump;
5+
use IO::Socket;
6+
7+
# $ git clone https://github.com/novnc/websockify
8+
# $ cd websockify
9+
# $ ./run 2345 localhost:2346
10+
11+
my $serv = IO::Socket::INET->new(
12+
LocalAddr => 'localhost',
13+
LocalPort => 2346,
14+
Proto => 'tcp',
15+
Listen => 5,
16+
Reuse => 1 ) or die $@;
17+
18+
# really dumb server
19+
print "Listening...\n";
20+
while (my $client = $serv->accept()) {
21+
print "Got a client...\n";
22+
print $client "Hello, Perl!\n";
23+
}
24+

experiments/socktest.pl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use warnings;
2+
use 5.028;
3+
use Socket;
4+
use Fcntl qw/F_GETFL F_SETFL O_NONBLOCK/;
5+
use IO::Select;
6+
use Data::Dumper;
7+
$Data::Dumper::Useqq=1;
8+
9+
my $port = 2345;
10+
my $iaddr = inet_aton("localhost") || die "host not found";
11+
my $paddr = sockaddr_in($port, $iaddr);
12+
13+
# Note: Emscripten apparently doesn't like NONBLOCK being passed to socket(),
14+
# and I couldn't get setsockopt to work yet - but the following works.
15+
# https://github.com/kripken/emscripten/blob/d08bf13/tests/sockets/test_sockets_echo_client.c#L166
16+
# everything is async - need "our $sock" here so it doesn't go out of scope at end of file
17+
socket(our $sock, PF_INET, SOCK_STREAM, getprotobyname("tcp")) or die "socket: $!";
18+
my $flags = fcntl($sock, F_GETFL, 0) or die "get flags: $!";
19+
fcntl($sock, F_SETFL, $flags | O_NONBLOCK) or die "set flags: $!";
20+
connect $sock, $paddr or !$!{EINPROGRESS} && die "connect: $!";
21+
22+
# so far so good... but probably should just use something like IO::Async instead
23+
24+

0 commit comments

Comments
 (0)