File: benchmark.pl

package info (click to toggle)
libhttp-headers-fast-perl 0.22-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: perl: 1,036; makefile: 2
file content (136 lines) | stat: -rw-r--r-- 3,719 bytes parent folder | download | duplicates (4)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use strict;
use warnings;
use Benchmark qw/cmpthese/;
use HTTP::Headers;
use HTTP::Headers::Fast;

my %source = (
    'Connection'     => 'close',
    'Date'           => 'Tue, 11 Nov 2008 01:16:37 GMT',
    'Content-Length' => 3744,
    'Content-Type'   => 'text/html',
    'Status'         => 200,
);

my @cases = (
    push_header => sub {
        my $h = HTTP::Headers->new;
        my $f = HTTP::Headers::Fast->new;

        cmpthese(
            100000 => {
                orig => sub { $h->push_header('X-Foo' => 1) },
                fast => sub { $f->push_header('X-Foo' => 1) },
            },
        );
    },
    push_header_many => sub {
        my $h = HTTP::Headers->new;
        my $f = HTTP::Headers::Fast->new;

        cmpthese(
            300000 => {
                orig => sub { $h->push_header('X-Foo' => 1, 'X-Bar' => 2) },
                fast => sub { $f->push_header('X-Foo' => 1, 'X-Bar' => 2) },
            },
        );
    },
    get_date => sub {
        my $h = HTTP::Headers->new;
        $h->date(1226370757);
        my $f = HTTP::Headers::Fast->new;
        $f->date(1226370757);

        cmpthese(
            30000 => {
                orig => sub { $h->date },
                fast => sub { $f->date },
            },
        );
    },
    set_date => sub {
        my $h = HTTP::Headers->new;
        my $f = HTTP::Headers::Fast->new;

        cmpthese(
            100000 => {
                orig => sub { $h->date(1226370757) },
                fast => sub { $f->date(1226370757) },
            },
        );
    },
    scan => sub {
        my $h = HTTP::Headers->new(%source);
        my $f = HTTP::Headers::Fast->new(%source);

        cmpthese(
            100000 => {
                orig => sub { $h->scan(sub { }) },
                fast => sub { $f->scan(sub { }) },
            },
        );
    },
    get_header => sub {
        my $h = HTTP::Headers->new;
        my $f = HTTP::Headers::Fast->new;

        cmpthese(
            1000000 => {
                orig => sub { $h->header('Content-Length') },
                fast => sub { $f->header('Content-Length') },
            },
        );
    },
    set_header => sub {
        my $h = HTTP::Headers->new;
        my $f = HTTP::Headers::Fast->new;

        cmpthese(
            1000000 => {
                orig => sub { $h->header('Content-Length' => 100) },
                fast => sub { $f->header('Content-Length' => 100) },
            },
        );
    },
    get_content_length => sub {
        my $h = HTTP::Headers->new;
        my $f = HTTP::Headers::Fast->new;

        cmpthese(
            1000000 => {
                orig => sub { $h->content_length },
                fast => sub { $f->content_length },
            },
        );
    },
    as_string_without_sort => sub {
        my $h = HTTP::Headers->new(%source);
        my $f = HTTP::Headers::Fast->new(%source);
        cmpthese(
            100000 => {
                orig           => sub { $h->as_string },
                fast_as_str    => sub { $f->as_string },
                fast_as_str_wo => sub { $f->as_string_without_sort },
            },
        );
    },
    as_string => sub {
        my $h = HTTP::Headers->new(%source);
        my $f = HTTP::Headers::Fast->new(%source);
        cmpthese(
            100000 => {
                orig => sub { $h->as_string },
                fast => sub { $f->as_string },
            },
        );
    },
);
my $only = shift @ARGV;
print "HTTP::Headers $HTTP::Headers::VERSION, HTTP::Headers::Fast $HTTP::Headers::Fast::VERSION\n";
while (my ($name, $code) = splice(@cases, 0, 2)) {
    next if $only && $only ne $name;
    print "-- $name\n";
    $code->();
    print "\n";
}