edimusrex has asked for the wisdom of the Perl Monks concerning the following question:

I am using Parallel::ForkManager to test a webservice. The most important piece of information I need is how long each fork took to process the request. Here is a sample of my code

#!/usr/bin/perl use warnings; use strict; use File::Basename; use diagnostics; use Parallel::ForkManager qw( ); use Time::HiRes qw( time ); my $max_processes = 5; my $home = '/vagrant'; my $url = ''; my @files = `ls $home/*.zip`; my $pm = Parallel::ForkManager->new($max_processes); foreach my $zip (@files){ chomp $zip; my $start = time(); my $pid = $pm->start and next; my $filename = `unzip -l $zip | awk '{print \$4}' | grep -P '.*-\\ +d+\\.xml'`; chomp $filename; my $header = "curl -s --header 'Content-Type: application/zip' --d +ata-binary '@" . $zip . "' '". $url . $filename . '&media' . "=xml'"; my @response = `$header`; foreach my $line (@response){ chomp $line; if ($line =~ m/\<ref href\=\"(.*-\d+\.xml)\"\/\>\<\/entry\>/){ print "XBRL Name -- $1\nExpected Filename -- $filename\n"; last; } } my $end = time(); print "Runtime --- "; printf("%.2f\n", $end - $start); print "\n\n"; $pm->finish; } $pm->wait_all_children;

The output of the runtime is incorrect as it seems like the start time is not processed for each item in my array. There must be a better way to do it but damned if I know. Your help would be greatly appreciated


Replies are listed 'Best First'.
Re: Finding the run time of a Fork process
by BrowserUk (Pope) on Jan 07, 2016 at 13:37 UTC

    Swap the order of the two statements to be as below:

    my $pid = $pm->start and next; my $start = time();

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      even when I do that it seems to just append the time.
Re: Finding the run time of a Fork process
by Anonymous Monk on Jan 07, 2016 at 14:19 UTC
    Why do you keep telling 'it seems'. Tell us what actually happened. Surely you realize no one is going to run that stuff.

    As for better way, see times, POSIX::times, maybe even getrusage.

      What I mean is that the first one that runs print out an expected 4 seconds or so but the next one appears to be adding an additional 4 seconds to the it meaning that if I run 5 processes they (for example) go 4, 8, 12, 16, 20 etc instead of an expected 4 seconds per thread (on average). As I am typing this I just realized that it may be correct as my webservice may only be allowing 1 process to interact with it at a time in which case my output is correct. I'll investigate that aspect of it more.

      Thanks again