File: benchmark

package info (click to toggle)
libmaxmind-db-reader-perl 1.000014-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,040 kB
  • sloc: perl: 1,668; makefile: 10
file content (44 lines) | stat: -rw-r--r-- 1,030 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl

use strict;
use warnings;

use Data::Printer;
use Getopt::Long;
use MaxMind::DB::Reader;
use Time::HiRes qw( gettimeofday tv_interval );
use Try::Tiny;

sub main {
    my $file;
    my $iterations = 10000;
    GetOptions(
        'file:s' => \$file,
        'iterations:i' => \$iterations,
    );

    my $reader = MaxMind::DB::Reader->new( file => $file );

    my $start = [gettimeofday];
    for my $i (1 .. $iterations) {
        print "$i\n" if $i % 1000 == 0;

        # get a random IPv4 address. Not using Net::Works due to possible
        # speed issues
        my $ip = join '.', unpack 'C4', pack 'N', int(rand(2**32-1));

        # Catch exceptions from unknown addresses
        try {
           my $record = $reader->record_for_address($ip);
        };
    }
    my $end = [gettimeofday];
    my $duration = tv_interval( $start, $end );

    printf
      "Looked up %d records in %f seconds, a speed of %f lookups / second\n",
      $iterations, $duration, $iterations / $duration;
}

main();