File: socket-vs-perl

package info (click to toggle)
libdata-validate-ip-perl 0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 352 kB
  • sloc: perl: 1,069; sh: 23; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,007 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
use strict;
use warnings;

use Benchmark qw( cmpthese );

use Data::Validate::IP;

unless ($Data::Validate::IP::HAS_SOCKET) {
    warn "Cannot load inet_pton from Socket\n";
    exit;
}

my @bad = ('a' .. 'z', undef, 0);

my @ipv4 = @bad;
for (1 .. 500) {
    push @ipv4, _random_ipv4();
}

my @ipv6 = @bad;
push @ipv6, '::', '::0';
for (1 .. 500) {
    push @ipv6, _random_ipv6();
}

cmpthese(
    10000,
    {
        'pure Perl ipv4' =>
            sub { Data::Validate::IP::_slow_is_ipv4($_) for @ipv4 },
        'Socket ipv4' =>
            sub { Data::Validate::IP::_fast_is_ipv4($_) for @ipv4 },
    }
);

cmpthese(
    10000,
    {
        'pure Perl ipv6' =>
            sub { Data::Validate::IP::_slow_is_ipv6($_) for @ipv6 },
        'Socket ipv6' =>
            sub { Data::Validate::IP::_fast_is_ipv6($_) for @ipv6 },
    }
);

sub _random_ipv4 {
    return join '.', map { int(rand(256)) } 1 .. 4;
}

sub _random_ipv6 {
    return join '::', map { sprintf('%4x', int(rand(2**16))) } 1 .. 8;
}