Help-Site Computer Manuals
Software
Hardware
Programming
Networking
  Algorithms & Data Structures   Programming Languages   Revision Control
  Protocols
  Cameras   Computers   Displays   Keyboards & Mice   Motherboards   Networking   Printers & Scanners   Storage
  Windows   Linux & Unix   Mac

Benchmark::Forking
Run benchmarks in separate processes

Benchmark::Forking - Run benchmarks in separate processes


NAME

Benchmark::Forking - Run benchmarks in separate processes


SYNOPSIS


  use Benchmark::Forking qw( timethis timethese cmpthese );

  timethis ($count, "code");

  timethese($count, {

      'Name1' => sub { ...code1... },

      'Name2' => sub { ...code2... },

  });

  

  cmpthese($count, {

      'Name1' => sub { ...code1... },

      'Name2' => sub { ...code2... },

  });

  Benchmark::Forking->enabled(0);  # Stop using forking feature

  ...

  Benchmark::Forking->enabled(1);  # Begin using forking again


DESCRIPTION

The Benchmark::Forking module changes the behavior of the standard Benchmark module, running each piece of code to be timed in a separate forked process. Because each child exits after running its timing loop, the computations it performs can't propogate back to affect subsequent test cases.

This can make benchmark comparisons more accurate, because the separate test cases are mostly isolated from side-effects caused by the others. Benchmark scripts typically don't depend on those side-effects, so in most cases you can simply use or require this module at the top of your existing code without having to change anything else. (A few key exceptions are noted in BUGS.)

Implementation

Benchmark::Forking replaces the private runloop() function in the Benchmark module with a wrapper that forks before calling the original function. Forking is accomplished by the special open(F,"-|") call described in perlfunc/open, and the results are passed back as text from the child to the parent through an interprocess filehandle.

When comparing several test cases with the timethese or cmpthese functions, the main process will fork off a child and wait for it to complete its timing of all of the repetitions of one piece of code, then fork off a new child to handle the next case and wait again.

Exports

This module re-exports the same functions provided by Benchmark: countit, timeit, timethis, timethese, and cmpthese.

For a description of these functions, see Benchmark.

Methods

The benchmark forking functionality is automatically enabled once you load this module, but you can also disable and re-enable it at run-time using the following class methods.

enabled
If called without arguments, reports the current status:

    my $boolean = Benchmark::Forking->enabled;

If passed an additional argument, enables or disable forking:


    Benchmark::Forking->enabled( 1 );

    $t = timeit(10, '$Global = 5 * $Global');

    Benchmark::Forking->enabled( 0 );

enable
Enables benchmark forking.

    Benchmark::Forking->enable();

disable
Disables benchmark forking.

    Benchmark::Forking->disable();


BUGS

Because this depends on Perl's implementation of fork, it may not work as expected on non-Unix platforms such as Microsoft Windows.

Some external resources may not work when opened in the parent process and then accessed from multiple forked instances. If using this module causes your file, network, or database code to fail with an unusual error, this issue may be the culprit.

Some Benchmark scripts either accidentally or deliberately rely on the side-effects that this module avoids. If using this module causes your Perl code to behave differently than expected, you may be relying on this behavior; you can either revise your code to remove the dependency or continue to use the non-forking Benchmark.

If the standard Benchmark module were more fully object-oriented, this functionality could be added via subclassing, rather than by fiddling with Benchmark's internals, but the current implemenation doesn't seem to allow for this.


SEE ALSO

For documentation of the timing functions, see Benchmark.

For distribution, installation, support, copyright and license information, see the Benchmark::Forking::ReadMe manpage.

Programminig
Wy
Wy
yW
Wy
Programming
Wy
Wy
Wy
Wy