1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#!/usr/bin/perl
use strict;
use warnings;
use lib './lib';
use Benchmark qw( cmpthese );
use Markdent::Handler::CaptureEvents;
use Markdent::Handler::HTMLStream;
use Markdent::Parser;
use Storable qw( nfreeze thaw );
use Text::Markdown qw( markdown );
use File::Find qw( find );
use File::Slurp qw( read_file );
my $reps = shift || 10;
my %files;
find(
{
wanted => sub {
return unless $File::Find::name =~ /\.text$/;
$files{$File::Find::name} = read_file($File::Find::name);
},
no_chdir => 1,
},
't/mdtest-data'
);
my %captured;
for my $file ( keys %files ) {
my $ch = Markdent::Handler::CaptureEvents->new();
my $parser = Markdent::Parser->new( handler => $ch );
$parser->parse( markdown => $files{$file} );
$captured{$file} = nfreeze( $ch->captured_events() );
}
cmpthese(
$reps, {
'parse from scratch' => \&parse,
'replay from captured events' => \&replay,
'Text::Markdown' => \&tm,
},
);
sub parse {
for my $file ( keys %files ) {
my $html = _html_handler();
my $parser = Markdent::Parser->new( handler => $html );
$parser->parse( markdown => $files{$file} );
}
}
sub replay {
for my $file ( keys %files ) {
my $html = _html_handler();
my $captured = thaw( $captured{$file} );
$captured->replay_events($html);
}
}
sub _html_handler {
my $buffer = q{};
open my $fh, '>', \$buffer or die $!;
return Markdent::Handler::HTMLStream->new(
title => 'Benchmark',
output => $fh,
);
}
sub tm {
for my $file ( keys %files ) {
my $html = markdown( $files{$file} );
}
}
|