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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
use Mojo::Base -strict;
use Minion;
use Time::HiRes 'time';
my $ENQUEUE = 10000;
my $DEQUEUE = 1000;
my $REPETITIONS = 2;
my $WORKERS = 4;
my $INFO = 100;
my $STATS = 100;
my $REPAIR = 100;
my $LOCK = 1000;
my $UNLOCK = 1000;
# A benchmark script for comparing backends and evaluating the performance
# impact of proposed changes
my $minion = Minion->new(Pg => 'postgresql://postgres@127.0.0.1:5432/postgres');
$minion->add_task(foo => sub { });
$minion->add_task(bar => sub { });
$minion->reset({all => 1});
# Enqueue
say "Clean start with $ENQUEUE jobs";
my @parents = map { $minion->enqueue('foo') } 1 .. 5;
my $before = time;
$minion->enqueue($_ % 2 ? 'foo' : 'bar' => [] => {parents => \@parents}) for 1 .. $ENQUEUE;
my $elapsed = time - $before;
my $avg = sprintf '%.3f', $ENQUEUE / $elapsed;
say "Enqueued $ENQUEUE jobs in $elapsed seconds ($avg/s)";
$minion->backend->pg->db->query('ANALYZE minion_jobs');
# Dequeue
sub dequeue {
my @pids;
for (1 .. $WORKERS) {
die "Couldn't fork: $!" unless defined(my $pid = fork);
unless ($pid) {
my $worker = $minion->repair->worker->register;
say "$$ will finish $DEQUEUE jobs";
my $before = time;
$worker->dequeue(0.5)->finish for 1 .. $DEQUEUE;
my $elapsed = time - $before;
my $avg = sprintf '%.3f', $DEQUEUE / $elapsed;
say "$$ finished $DEQUEUE jobs in $elapsed seconds ($avg/s)";
$worker->unregister;
exit;
}
push @pids, $pid;
}
say "$$ has started $WORKERS workers";
my $before = time;
waitpid $_, 0 for @pids;
my $elapsed = time - $before;
my $avg = sprintf '%.3f', ($DEQUEUE * $WORKERS) / $elapsed;
say "$WORKERS workers finished $DEQUEUE jobs each in $elapsed seconds ($avg/s)";
}
dequeue() for 1 .. $REPETITIONS;
# Job info
say "Requesting job info $INFO times";
$before = time;
my $backend = $minion->backend;
$backend->list_jobs(0, 1, {ids => [$_]}) for 1 .. $INFO;
$elapsed = time - $before;
$avg = sprintf '%.3f', $INFO / $elapsed;
say "Received job info $INFO times in $elapsed seconds ($avg/s)";
# Stats
say "Requesting stats $STATS times";
$before = time;
$minion->stats for 1 .. $STATS;
$elapsed = time - $before;
$avg = sprintf '%.3f', $STATS / $elapsed;
say "Received stats $STATS times in $elapsed seconds ($avg/s)";
# Repair
say "Repairing $REPAIR times";
$before = time;
$minion->repair for 1 .. $REPAIR;
$elapsed = time - $before;
$avg = sprintf '%.3f', $REPAIR / $elapsed;
say "Repaired $REPAIR times in $elapsed seconds ($avg/s)";
# Lock
say "Acquiring locks $LOCK times";
$before = time;
$minion->lock($_ % 2 ? 'foo' : 'bar', 3600, {limit => int($LOCK / 2)}) for 1 .. $LOCK;
$elapsed = time - $before;
$avg = sprintf '%.3f', $LOCK / $elapsed;
say "Acquired locks $LOCK times in $elapsed seconds ($avg/s)";
# Unlock
say "Releasing locks $UNLOCK times";
$before = time;
$minion->unlock($_ % 2 ? 'foo' : 'bar') for 1 .. $UNLOCK;
$elapsed = time - $before;
$avg = sprintf '%.3f', $UNLOCK / $elapsed;
say "Releasing locks $UNLOCK times in $elapsed seconds ($avg/s)";
|