Beekeeper

 view release on metacpan or  search on metacpan

examples/flood/flood.pl  view on Meta::CPAN

    }
    elsif ($type =~ m/^A(sync)?/i) {
        $type = 'async calls';
        $code = sub {
            # Measure response time
            MyApp::Service::Flood->async_echo( $payload, sub {} );
        };
    }
    elsif ($type =~ m/^F(ire)?/i) {
        $type = 'fire & forget';
        $code = sub {
            # Measure send time only
            MyApp::Service::Flood->fire_echo( $payload );
        };
    }
    else {
        die "type must be one of (N)otification, (S)ync, (A)sync or (F)ire and forget\n";
    }

    my $count = $args{'count'};
    my $rate  = $args{'rate'};
    my $batch = $rate || $count || 1000;
    my $total = 0;

    local $| = 1;

    BENCH: {

        printf( "%s %-15s of %3s Kb  ", $batch, $type, $size );

        my $start = time();

        for (1..$batch) {
            &$code();
        }

        $total += $batch;

        if ($type eq 'async calls') {

            # Run the event loop in order to receive responses
            my $cv = AnyEvent->condvar;
            AnyEvent::postpone { $cv->send };
            $cv->recv;
        }

        my $took = time() - $start;
        my $clk = sprintf("%.3f", $took);
        my $cps = sprintf("%.0f", $batch / $took);
        my $avg = sprintf("%.2f", $took / $batch * 1000);

        if ($rate) {

            printf( "in %6s sec  %6s /sec %6s ms each", $clk, $cps, $avg );

            if ($took <= 1) {

                my $sleep = 1 - $took;
                my $cv = AnyEvent->condvar;
                AnyEvent->now_update;
                my $tmr = AnyEvent->timer( after => $sleep, cb => $cv);
                $cv->recv;
            }
            else {
                my $ovl = int(abs(($took - 1) * 100)); 
                print "   $ovl\% overload";
            }

            print "\n";

            last if $count && $total >= $count;

            my $key = ReadKey(-1);
            last if $key;

            redo BENCH;
        }
        else {

            printf( "in %6s sec  %6s /sec %6s ms each\n", $clk, $cps, $avg );
        }
    }

    if ($type eq 'async calls' && !$rate) {
        # Ensure that all responses are received before running the next benchmark
        $client->wait_async_calls;
    }
}

sub run_benchmarks {

    my $count = $opt_count || $opt_number || 1000;

    my @sizes = ( 0, 1, 5, 10 );

    # Notifications
    foreach (@sizes) {
        time_this( type => 'N', count => $count, size => $_ );
        sleep 1;
    }

    print "\n";

    # Sync calls
    foreach (@sizes) {
        time_this( type => 'S', count => $count, size => $_ );
        sleep 1;
    }

    print "\n";

    # Async calls
    foreach (@sizes) {
        time_this( type => 'A', count => $count, size => $_ );
        sleep 1;
    }

    print "\n";

    # Fire calls
    foreach (@sizes) {



( run in 1.128 second using v1.01-cache-2.11-cpan-6aa56a78535 )