Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw(timeit cmpthese timesum :hireswallclock);
use lib 'lib', '../p5-http-tiny/lib';
use Elasticsearch;
use v5.16;
my $Times = 3;
my $Total = 10000;
my $Doc = '{"foo":"' . ( 'x' x 300000 ) . '"}';
my $es;
cxn('NetCurl');
reset_es();
my ( %reads, %writes );
for ( 1 .. $Times ) {
say "\n\nRun $_:";
for my $cxn ( 'LWP', 'Hijk', 'NetCurl', 'HTTPTiny' ) {
say "\n * $cxn";
cxn($cxn);
my $t = timeit( 1, sub { index_docs() } );
$writes{$cxn} = $writes{$cxn} ? timesum( $writes{$cxn}, $t ) : $t;
$t = timeit( 1, sub { get_docs() } );
$reads{$cxn} = $reads{$cxn} ? timesum( $reads{$cxn}, $t ) : $t;
reset_es();
}
}
say "\nWrite:";
cmpthese( \%writes );
say "\nRead:";
cmpthese( \%reads );
#===================================
sub cxn {
#===================================
$es = Elasticsearch->new( cxn => shift(), request_timeout => 15 );
}
#===================================
sub reset_es {
#===================================
$es->indices->delete( index => 'test', ignore => 404 );
$es->indices->create(
index => 'test',
body => { mappings => { test => { enabled => 0 } } }
);
$es->cluster->health( wait_for_status => 'yellow' );
}
#===================================
sub index_docs {
#===================================
local $| = 1;
for ( 1 .. $Total ) {
$es->index( index => 'test', type => 'test', id => $_, body => $Doc );
$_ % 100 || print ".";
}
}
#===================================
sub get_docs {
#===================================
local $| = 1;
for ( 1 .. $Total ) {
my $res = $es->get( index => 'test', type => 'test', id => $_ );
$res->{_source} = $Doc or die "Baah: " . $res->{_source};
$_ % 100 || print ".";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment