Debian Codegraph @

author/benchmark/simple.pl

raw
1 use strict;
2 use warnings;
3 use Benchmark ':all';
4 use LWP::UserAgent;
5 use WWW::Curl::Easy 4.14;
6 use HTTP::Lite;
7 use Furl::HTTP qw/HEADERS_NONE HEADERS_AS_ARRAYREF/;
8 use Furl;
9 use Config;
10 use Getopt::Long;
11
12 GetOptions(
13 'busize=i' => \my $bufsize,
14 );
15
16 printf `git rev-parse HEAD`;
17 printf "Perl/%vd on %s\n", $^V, $Config{archname};
18 printf "Furl/$Furl::VERSION, LWP/$LWP::VERSION, WWW::Curl/$WWW::Curl::VERSION, HTTP::Lite/$HTTP::Lite::VERSION, libcurl[@{[ WWW::Curl::Easy::version() ]}]\n";
19
20 my $url = shift @ARGV || 'http://192.168.1.3:80/';
21
22 my $ua = LWP::UserAgent->new(parse_head => 0, keep_alive => 1);
23 my $curl = WWW::Curl::Easy->new();
24 my $furl_low = Furl::HTTP->new(header_format => HEADERS_NONE);
25 my $furl_high = Furl->new();
26 $furl_high->{bufsize} = $bufsize if defined $bufsize;
27 $furl_low->{bufsize} = $bufsize if defined $bufsize;
28 my $uri = URI->new($url);
29 my $host = $uri->host;
30 my $scheme = $uri->scheme;
31 my $port = $uri->port;
32 my $path_query = $uri->path_query;
33 my $lite = HTTP::Lite->new();
34 $lite->http11_mode(1);
35
36 my $res = $ua->get($url);
37 print "--\n";
38 print $res->headers_as_string;
39 print "--\n";
40 printf "bufsize: %d\n", $furl_low->{bufsize};
41 print "--\n\n";
42 my $body_content_length = length($res->content);
43 $body_content_length == $res->content_length or die;
44
45 cmpthese(
46 -1, {
47 http_lite => sub {
48 my $req = $lite->request($url)
49 or die;
50 $lite->status == 200 or die;
51 length($lite->body) == $body_content_length or die "Lite failed: @{[ length($lite->body) ]} != $body_content_length";
52 $lite->reset(); # This is *required* for re-use instance.
53 },
54 lwp => sub {
55 my $res = $ua->get($url);
56 $res->code == 200 or die;
57 length($res->content) == $body_content_length or die;
58 },
59 curl => sub {
60 my @headers;
61 $curl->setopt(CURLOPT_HEADER, 0);
62 $curl->setopt(CURLOPT_NOPROGRESS, 1);
63 $curl->setopt(CURLOPT_URL, $url);
64 $curl->setopt(CURLOPT_HTTPGET, 1);
65 $curl->setopt(CURLOPT_HEADERFUNCTION, sub {
66 push @headers, @_;
67 length($_[0]);
68 });
69 my $content = '';
70 $curl->setopt(CURLOPT_WRITEDATA, \$content);
71 my $ret = $curl->perform();
72 $ret == 0 or die "$ret : " . $curl->strerror($ret);
73 my $code = $curl->getinfo(CURLINFO_HTTP_CODE);
74 $code == 200 or die "oops: $code";
75 length($content) == $body_content_length or die;
76 },
77 furl_high => sub {
78 my $res = $furl_high->request(
79 method => 'GET',
80 host => $host,
81 port => $port,
82 scheme => $scheme,
83 path_query => $path_query,
84 headers => [ 'Content-Length' => 0 ]
85 );
86 $res->code == 200 or die "oops";
87 length($res->content) == $body_content_length or die;
88 },
89 furl_low => sub {
90 my ( $version, $code, $msg, $headers, $content ) = $furl_low->request(
91 method => 'GET',
92 host => $host,
93 port => $port,
94 scheme => $scheme,
95 path_query => $path_query,
96 headers => [ 'Content-Length' => 0 ]
97 );
98 $code == 200 or die "oops: $code, $content";
99 length($content) == $body_content_length or die;
100 },
101 },
102 );