diff options
author | Yorhel <git@yorhel.nl> | 2019-05-14 18:37:14 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-05-14 18:38:12 +0200 |
commit | 04719fb0dd62858f97996f791a2318e062b90544 (patch) | |
tree | 47f820a61ab0ee7757740e8cd6367f31e1e310ed /lmdb.pl |
Initial commit
Diffstat (limited to 'lmdb.pl')
-rwxr-xr-x | lmdb.pl | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +#!/usr/bin/perl + +# Usage: +# +# ./lmdb.pl create dbfile <dictionary +# ./lmdb.pl bench dbfile + +use strict; +use warnings; +use v5.10; +use LMDB_File ':flags'; + +my($cmd, $dbfile) = @ARGV; + +sub encode { + my $env = LMDB::Env->new($dbfile, { mapsize => 1<<36, maxdbs => 1, flags => MDB_NOSUBDIR }); + my $txn = $env->BeginTxn(); + my $dbi = $txn->open('db', MDB_CREATE); + while(<STDIN>) { + chomp; + $txn->put($dbi => $_, '') if length($_) && length($_) < 510; + } + $txn->commit; +} + + +sub bench { + my $env = LMDB::Env->new($dbfile, { mapsize => 1<<32, maxdbs => 1, flags => MDB_RDONLY|MDB_NOSUBDIR }); + my $txn = $env->BeginTxn(); + my $dbi = $txn->open('db'); + my $lookup = sub { eval { $txn->get($dbi => $_[0], my $d); 1 } }; + + use Benchmark 'timethis', ':hireswallclock'; + sub rstr { state $s=['a'..'z','A'..'Z','0'..'9','!','@','#']; join '', map $s->[rand @$s], 1..5 } + srand 0; + timethis 200000, sub { $lookup->(rstr()) }, 'lmdb'; +} + +encode if $cmd eq 'create'; +bench if $cmd eq 'bench'; |