#!/usr/bin/env -S perl # parallel demonstration # based on example in documentation # https://metacpan.org/pod/MCE::Shared::Cache#PERFORMANCE-TESTING use strict; use warnings; use feature qw( say ); use Digest::MD5 qw( md5_base64 ); use Time::HiRes qw( time ); use MCE 1.814; use MCE::Shared; use Redis; use Sereal qw/encode_sereal decode_sereal/; $| = 1; srand(0); # construct shared variables # serialization is handled automatically my $c = Redis->new; my $found = MCE::Shared->scalar( 0 ); # construct and spawn MCE workers # workers increment a local variable $f my $mce = MCE->new( chunk_size => 4000, max_workers => 4, user_func => sub { my ($mce, $chunk_ref, $chunk_id) = @_; if ( $mce->user_args()->[0] eq 'setter' ) { for ( @{ $chunk_ref } ) { $c->setex($_, 600, encode_sereal({md5 => $_})) } } else { my $f = 0; for ( @{ $chunk_ref } ) { my $srl = $c->get($_); $srl = decode_sereal($srl) if defined $srl; $f++ if ref $srl eq 'HASH'; } $found->incrby($f); } } )->spawn(); say "Mapping"; my @todo = map { md5_base64($_) } ( 1 .. 600_000 ); say "Starting"; my ( $read, $write ); { my $s = time; $mce->process({ progress => sub { print "Write: $_[0]\r" }, user_args => [ 'setter' ], }, \@todo); $write = time - $s; } say "Write: ", sprintf("%0.3f", scalar(@todo) / $write); { my $s = time; $found->set(0); $mce->process({ progress => sub { print "Read $_[0]\r" }, user_args => [ 'getter' ], }, \@todo); $read = time - $s; } $mce->shutdown(); say "Read : ", sprintf("%0.3f", scalar(@todo) / $read); say "Found: ", $found->get();