Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Help me beat NodeJS

by marioroy (Prior)
on Feb 13, 2016 at 17:23 UTC ( [id://1155161]=note: print w/replies, xml ) Need Help??


in reply to Help me beat NodeJS

Update: added pattern matching to the demonstration.

Greetings,

At first glance, am not seeing NodeJS performing regex for fields containing spaces. That's not really fair, IMHO :) The regex in Perl is likely the main reason for taking longer. Having 24 workers reading IO simultaneously from the nfsv3 mounted SAN may be another reason even though NodeJS seems not affected by it. Maybe, the regex statement in Perl is the reason.

The following is an alternative solution. IO is faster for the upcoming 1.700 release versus MCE 1.608. Thus, the note on using 1.699_010. Also, inlined bits for the Windows platform.

#!/usr/local/bin/perl use strict; use warnings; # IO performance in 1.699_010 is faster than MCE 1.608. # https://metacpan.org/release/MARIOROY/MCE-1.699_010 use MCE::Loop; use MCE::Candy; my $dir = 'logs/*.log.gz'; my @files = sort(glob "$dir"); my $pattern = "some_string"; MCE::Loop::init { gather => MCE::Candy::out_iter_fh(\*STDOUT), chunk_size => '240k', max_workers => 24, use_slurpio => 1, }; open( my $fh, "-|", "zcat", @files ) or die "open error: $!\n"; mce_loop { my ( $mce, $slurp_ref, $chunk_id ) = @_; my $buf = ''; # Quickly determine if a match is found... # ...and process slurped chunk only if true. if ( $$slurp_ref =~ /$pattern/m ) { # The following is fast on Unix, but performance degrades # drastically on Windows beyond 4 workers. open my $MEM_FH, '<', $slurp_ref; while ( my $line = <$MEM_FH> ) { if ( $line =~ /$pattern/ ) { my @matches = $line =~ /".*?"|\S+/g; $buf .= "$matches[0],$matches[1],$matches[3],$matches +[4]\n"; } } close $MEM_FH; # Therefore, use the following construction on Windows. # while ( $$slurp_ref =~ /([^\n]+\n)/mg ) { # # my $line = $1; # possibly save $1 to not lose the value # # not necessary for this demonstration # my @matches = $1 =~ /".*?"|\S+/g; # $buf .= "$matches[0],$matches[1],$matches[3],$matches[4]\ +n"; # } } # Send output to the manager process for orderly output to STDOUT $mce->gather($chunk_id, $buf); } $fh; close $fh;

There is one reader across NFS by the manager process only, not workers. The effect is sequential IO which is typically faster than random IO.

Regards, Mario

Replies are listed 'Best First'.
Re^2: Help me beat NodeJS
by marioroy (Prior) on Feb 13, 2016 at 18:23 UTC

    Update: added pattern matching to the demonstration

    If parallel reads is desired, the following demonstration does the same thing. Basically, specifying chunk_size => 1 is all one needs to do for getting MCE to run like Parallel::ForkManager.

    #!/usr/local/bin/perl use strict; use warnings; use MCE::Loop; my $dir = 'logs/*.log.gz'; my @files = sort(glob "$dir"); my $pattern = "some_string"; MCE::Loop::init { max_workers => 24, chunk_size => 1, }; mce_loop { my ( $mce, $chunk_ref, $chunk_id ) = @_; open( my $fh, "-|", "zcat", $chunk_ref->[0] ) or die "open error: +$!\n"; while ( my $line = <$fh> ) { if ( $line =~ /$pattern/ ) { my @matches = $line =~ /".*?"|\S+/g; print "$matches[0],$matches[1],$matches[3],$matches[4]\n"; } } close $fh; } @files;

    Regards, Mario

      Wow thanks, I'll give this a shot. I will have to read some more on MCE, it looks very useful. I should have clarified, in the "parser" function in nodejs, I'm applying the same regex as perl to be fair. I've done the tests looking for a specific string (/some_string/) and I've done the regex in the above code (/".*?"|\S+/g), which captures everything in an array, since the lines are in this format: ' 1970-01-01 00:00:00 1.1.1.1 "A multi-word field" 2.2.2.2 '

        Got it. I went ahead and updated both MCE demonstrations to account for pattern matching. The more expensive regex (/".*?"|\S+/g) pattern is processed only if given line matches the initial string pattern. That will likely run faster.

        Likewise, for Parallel::ForkManager.

        #!/usr/local/bin/perl use strict; use warnings; use Parallel::ForkManager; my $pm = new Parallel::ForkManager(24); my $dir = '/data/logs/*.log.gz'; my @files = sort(glob "$dir"); my $pattern = "some_string"; $pm->set_waitpid_blocking_sleep(0); for my $file( @files ) { $pm->start and next; open( my $fh, "-|", "/bin/zcat", $file ) or die "open error: $!\n" +; while ( my $line = <$fh> ) { if ( $line =~ /$pattern/ ) { my @matches = $line =~ /".*?"|\S+/g; print "$matches[0],$matches[1],$matches[3],$matches[4]\n"; } } $pm->finish; } $pm->wait_all_children;

        Regards, Mario

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1155161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-28 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found