Debian Codegraph @

bench/socket-vs-perl

raw
1 use strict;
2 use warnings;
3
4 use Benchmark qw( cmpthese );
5
6 use Data::Validate::IP;
7
8 unless ($Data::Validate::IP::HAS_SOCKET) {
9 warn "Cannot load inet_pton from Socket\n";
10 exit;
11 }
12
13 my @bad = ('a' .. 'z', undef, 0);
14
15 my @ipv4 = @bad;
16 for (1 .. 500) {
17 push @ipv4, _random_ipv4();
18 }
19
20 my @ipv6 = @bad;
21 push @ipv6, '::', '::0';
22 for (1 .. 500) {
23 push @ipv6, _random_ipv6();
24 }
25
26 cmpthese(
27 10000,
28 {
29 'pure Perl ipv4' =>
30 sub { Data::Validate::IP::_slow_is_ipv4($_) for @ipv4 },
31 'Socket ipv4' =>
32 sub { Data::Validate::IP::_fast_is_ipv4($_) for @ipv4 },
33 }
34 );
35
36 cmpthese(
37 10000,
38 {
39 'pure Perl ipv6' =>
40 sub { Data::Validate::IP::_slow_is_ipv6($_) for @ipv6 },
41 'Socket ipv6' =>
42 sub { Data::Validate::IP::_fast_is_ipv6($_) for @ipv6 },
43 }
44 );
45
46 sub _random_ipv4 {
47 return join '.', map { int(rand(256)) } 1 .. 4;
48 }
49
50 sub _random_ipv6 {
51 return join '::', map { sprintf('%4x', int(rand(2**16))) } 1 .. 8;
52 }