diff --git a/experiments/bench.pl b/experiments/bench.pl new file mode 100644 index 0000000..d56fa78 --- /dev/null +++ b/experiments/bench.pl @@ -0,0 +1,20 @@ +use warnings; +use 5.026; +use Time::HiRes qw/gettimeofday tv_interval/; + +my $t0 = [gettimeofday]; +my @primes = join ',', grep {prime($_)} 1..1000000; +my $elapsed = tv_interval($t0); +printf "%.3f\n", $elapsed; + +# http://www.rosettacode.org/wiki/Primality_by_trial_division#Perl +sub prime { + my $n = shift; + $n % $_ or return for 2 .. sqrt $n; + $n > 1 +} + +# A quick test: This program, when run +# from WebPerl (Firefox): ~7.4s +# natively (same machine): ~2.3s +# => roughly 3.2 times slower diff --git a/experiments/sockserv.pl b/experiments/sockserv.pl new file mode 100644 index 0000000..3800123 --- /dev/null +++ b/experiments/sockserv.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +use warnings; +use strict; +use Data::Dump; +use IO::Socket; + +# $ git clone https://github.com/novnc/websockify +# $ cd websockify +# $ ./run 2345 localhost:2346 + +my $serv = IO::Socket::INET->new( + LocalAddr => 'localhost', + LocalPort => 2346, + Proto => 'tcp', + Listen => 5, + Reuse => 1 ) or die $@; + +# really dumb server +print "Listening...\n"; +while (my $client = $serv->accept()) { + print "Got a client...\n"; + print $client "Hello, Perl!\n"; +} + diff --git a/experiments/socktest.pl b/experiments/socktest.pl new file mode 100644 index 0000000..67561a1 --- /dev/null +++ b/experiments/socktest.pl @@ -0,0 +1,24 @@ +use warnings; +use 5.028; +use Socket; +use Fcntl qw/F_GETFL F_SETFL O_NONBLOCK/; +use IO::Select; +use Data::Dumper; +$Data::Dumper::Useqq=1; + +my $port = 2345; +my $iaddr = inet_aton("localhost") || die "host not found"; +my $paddr = sockaddr_in($port, $iaddr); + +# Note: Emscripten apparently doesn't like NONBLOCK being passed to socket(), +# and I couldn't get setsockopt to work yet - but the following works. +# https://github.com/kripken/emscripten/blob/d08bf13/tests/sockets/test_sockets_echo_client.c#L166 +# everything is async - need "our $sock" here so it doesn't go out of scope at end of file +socket(our $sock, PF_INET, SOCK_STREAM, getprotobyname("tcp")) or die "socket: $!"; +my $flags = fcntl($sock, F_GETFL, 0) or die "get flags: $!"; +fcntl($sock, F_SETFL, $flags | O_NONBLOCK) or die "set flags: $!"; +connect $sock, $paddr or !$!{EINPROGRESS} && die "connect: $!"; + +# so far so good... but probably should just use something like IO::Async instead + +