File Coverage

blib/lib/Surveyor/Benchmark/HTMLEntities.pm
Criterion Covered Total %
statement 12 30 40.0
branch 0 2 0.0
condition n/a
subroutine 4 8 50.0
pod 4 4 100.0
total 20 44 45.4


line stmt bran cond sub pod time code
1             package Surveyor::Benchmark::HTMLEntities;
2 1     1   768 use strict;
  1         3  
  1         26  
3 1     1   4 use warnings;
  1         1  
  1         53  
4              
5             our $VERSION = '1.021';
6             our $HTML;
7              
8 1     1   494 use HTML::Entities;
  1         5880  
  1         89  
9 1     1   496 use HTML::Escape;
  1         1315  
  1         314  
10              
11             =encoding utf8
12              
13             =head1 NAME
14              
15             Surveyor::Benchmark::HTMLEntities - Benchmark HTML entity escaping
16              
17             =head1 SYNOPSIS
18              
19             Install L to get the C program.
20              
21             To test the defaults:
22              
23             % survey -p Surveyor::Benchmark::HTMLEntities URL
24              
25             To compare pure Perl behavior:
26              
27             % env PERL_ONLY=1 survey -p Surveyor::Benchmark::HTMLEntities URL
28              
29             =head1 DESCRIPTION
30              
31             L provides a limited functionality HTML entity escaper.
32             It only handles C<< ><&"' >>. As such, it can be quite a bit faster
33             because it does less.
34              
35             Here are some numbers from my Mid-2012 MacBook Air:
36              
37             XS versus pure Perl:
38              
39             Benchmark: timing 10000 iterations of html_entities, html_escape...
40             html_entities: 14 wallclock secs (14.09 usr + 0.01 sys = 14.10 CPU) @ 709.22/s (n=10000)
41             html_escape: 1 wallclock secs ( 0.68 usr + 0.00 sys = 0.68 CPU) @ 14705.88/s (n=10000)
42              
43             fair fight:
44              
45             Benchmark: timing 10000 iterations of html_entities, html_escape...
46             html_entities: 14 wallclock secs (13.79 usr + 0.01 sys = 13.80 CPU) @ 724.64/s (n=10000)
47             html_escape: 7 wallclock secs ( 7.57 usr + 0.01 sys = 7.58 CPU) @ 1319.26/s (n=10000)
48              
49             =over 4
50              
51             =item set_up( URL )
52              
53             Fetch the web page and store it for use in the benchmarks.
54              
55             =cut
56              
57             sub set_up {
58 0     0 1   my( $self, @args ) = @_;
59 0           require Mojo::UserAgent;
60              
61 0           print "Fetching $args[0]\n";
62 0           my $tx = Mojo::UserAgent->new->get( $args[0] );
63              
64 0           my $code = $tx->res->code;
65 0 0         die "Status $code: Could not fetch $args[0]\n"
66             unless $code == 200;
67              
68 0           $HTML = $tx->res->body;
69 0           print "HTML is " . length($HTML) . " bytes\n";
70              
71 0           my %counts;
72 0           $counts{'>'} = $HTML =~ tr/>//;
73 0           $counts{'<'} = $HTML =~ tr/
74 0           $counts{'&'} = $HTML =~ tr/&//;
75 0           $counts{"'"} = $HTML =~ tr/'//;
76 0           $counts{'"'} = $HTML =~ tr/"//;
77              
78             printf qq(> (%d)\n< (%d)\n& (%d)\n' (%d)\n" (%d)\n),
79 0           @counts{ qw(> < & ' ") };
80             }
81              
82             =item tear_down
83              
84             =cut
85              
86             sub tear_down {
87 0     0 1   1;
88             }
89              
90             =item bench_html_escape
91              
92             Use HTML::Escape to encode. This is an XS module.
93              
94             HTML::Escape only encodes the C<< ><&"' >>.
95              
96             =cut
97              
98             sub bench_html_escape {
99 0     0 1   my $escaped = HTML::Escape::escape_html( $HTML );
100             }
101              
102             =item bench_html_entities
103              
104             Use HTML::Entities to encode. This is an pure Perl module.
105              
106             I tell C to only encode C<< ><&"' >> so it
107             matches what HTML::Escape will do. Otherwise, C
108             escapes wide characters too.
109              
110             =cut
111              
112             sub bench_html_entities {
113 0     0 1   my $escaped = HTML::Entities::encode_entities( $HTML, q(<>&"') );
114             }
115              
116             =back
117              
118             =head1 TO DO
119              
120              
121             =head1 SEE ALSO
122              
123              
124             =head1 SOURCE AVAILABILITY
125              
126             This source is in Github:
127              
128             https://github.com/briandfoy/surveyor-benchmark-htmlentities
129              
130             =head1 AUTHOR
131              
132             brian d foy, C<< >>
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             Copyright © 2013-2018, brian d foy . All rights reserved.
137              
138             You may redistribute this under the terms of Artistic License 2.0.
139              
140             =cut
141              
142             1;