Skip to content

Instantly share code, notes, and snippets.

@typester
Created September 3, 2009 08:28
Show Gist options
  • Save typester/180187 to your computer and use it in GitHub Desktop.
Save typester/180187 to your computer and use it in GitHub Desktop.
Rate AnyEvent::Gearman::Client Gearman::Client::Async
AnyEvent::Gearman::Client 1435/s -- -60%
Gearman::Client::Async 3571/s 149% --
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;
},
});
#!/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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment