typester (owner)

Revisions

  • 157663 Daisuke... Thu Sep 03 01:31:02 -0700 2009
  • cdaa5c typester Thu Sep 03 01:28:40 -0700 2009
  • 9b3758 Daisuke... Thu Sep 03 01:28:34 -0700 2009
  • 0f09e7 Daisuke... Thu Sep 03 01:28:25 -0700 2009
gist: 180187 Download_button fork
public
Public Clone URL: git://gist.github.com/180187.git
Embed All Files: show embed
0result.txt #
1
2
3
4
                            Rate AnyEvent::Gearman::Client Gearman::Client::Async
AnyEvent::Gearman::Client 1435/s -- -60%
Gearman::Client::Async 3571/s 149% --
 
bench.pl #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use Test::Base;
use Test::TCP;
use FindBin;
 
use Benchmark 'cmpthese';
 
use AnyEvent::Gearman::Client;
use Gearman::Client::Async;
 
eval q{
use Gearman::Worker;
use Gearman::Server;
};
if ($@) {
    plan skip_all
        => "Gearman::Worker and Gearman::Server are required to run this test";
}
 
my $port = empty_port;
 
my $child = fork;
if (!defined $child) {
    die "fork failed: $!";
}
elsif ($child == 0) {
    my $server = Gearman::Server->new( port => $port );
    $server->start_worker("$FindBin::Bin/worker.pl -s 127.0.0.1:$port");
    Danga::Socket->EventLoop;
}
else {
    END { kill 9, $child if $child }
}
 
my $danga = Gearman::Client::Async->new(
    job_servers => ["127.0.0.1:$port"],
);
 
my $ae = AnyEvent::Gearman::Client->new(
    job_servers => ["127.0.0.1:$port"],
);
 
cmpthese( 10000, {
    'Gearman::Client::Async' => sub {
        $danga->add_task(
            Gearman::Task->new( echo => \'Hello! Hello!', {
                on_complete => sub {
                    my $res = $_[0];
                    die unless $$res eq 'Hello! Hello!';
                    Danga::Socket->SetPostLoopCallback(sub { 0 });
                },
            }),
        );
 
        Danga::Socket->EventLoop;
    },
 
    'AnyEvent::Gearman::Client' => sub {
        my $cv = AnyEvent->condvar;
 
        $ae->add_task(
            echo => 'Hello! Hello!',
            on_complete => sub {
                my $res = $_[1];
                die unless $res eq 'Hello! Hello!';
                $cv->send;
            },
            on_fail => sub { die },
        );
        $cv->recv;
    },
});
 
 
worker.pl #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/perl
 
use strict;
use Gearman::Worker;
use Getopt::Long;
my $opt_js;
GetOptions('s=s' => \$opt_js);
 
my $worker = Gearman::Worker->new;
$worker->job_servers(split(/,/, $opt_js));
 
$worker->register_function( echo => sub {
    my $job = shift;
    my $arg = $job->arg;
 
    return $arg;
});
 
$worker->work while 1;