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