| 1 |
#!/usr/bin/env perl |
| 2 |
use strictures 2; |
| 3 |
|
| 4 |
use GIS::Distance; |
| 5 |
use Benchmark qw( cmpthese ); |
| 6 |
use List::Util qw( any ); |
| 7 |
use List::MoreUtils qw( zip uniq ); |
| 8 |
use Module::Find qw( findallmod ); |
| 9 |
use Getopt::Long; |
| 10 |
|
| 11 |
Getopt::Long::Configure(qw( |
| 12 |
gnu_getopt no_ignore_case |
| 13 |
)); |
| 14 |
|
| 15 |
GetOptions( |
| 16 |
'formula|f=s' => \my @formulas, |
| 17 |
|
| 18 |
'pp!' => \my $do_pp, |
| 19 |
'xs!' => \my $do_xs, |
| 20 |
'geo' => \my $do_geo, |
| 21 |
|
| 22 |
'iters|i=i' => \my $iters, |
| 23 |
'dry-run|n' => \my $dry_run, |
| 24 |
|
| 25 |
'h|help' => \my $help, |
| 26 |
) or die "Unable to process options!\n"; |
| 27 |
|
| 28 |
if ($help) { |
| 29 |
require Pod::Usage; |
| 30 |
Pod::Usage::pod2usage( -verbose => 2 ); |
| 31 |
exit 0; |
| 32 |
} |
| 33 |
|
| 34 |
@formulas = split(/,/, join(',', @formulas)); |
| 35 |
|
| 36 |
$iters ||= 5_000_000; |
| 37 |
|
| 38 |
$do_pp = 1 if !defined $do_pp; |
| 39 |
$do_xs = 1 if !defined $do_xs; |
| 40 |
|
| 41 |
my %gis_to_geo; |
| 42 |
|
| 43 |
if ($do_geo) { |
| 44 |
require Geo::Distance; |
| 45 |
Geo::Distance->import(); |
| 46 |
*Geo::Distance::new_distance = \&Geo::Distance::distance; |
| 47 |
require Geo::Distance::XS; |
| 48 |
Geo::Distance::XS->import(); |
| 49 |
|
| 50 |
# Geo::Distance::new_distance() proxies to GIS::Distance. |
| 51 |
# Geo::Distance::distance() is installed by Geo::Distance::XS. |
| 52 |
# Geo::Distance::old_distance() is pure-perl, pre GIS::Distance gutting. |
| 53 |
|
| 54 |
%gis_to_geo = (reverse %Geo::Distance::GEO_TO_GIS_FORMULA_MAP); |
| 55 |
} |
| 56 |
|
| 57 |
my @installed_modules = ( |
| 58 |
grep { $_ !~ m{::Formula} } |
| 59 |
grep { $_ ne 'GIS::Distance::Constants' } |
| 60 |
grep { $_ ne 'GIS::Distance::Fast' } |
| 61 |
findallmod('GIS::Distance') |
| 62 |
); |
| 63 |
|
| 64 |
my %installed_modules_lookup = map { $_=>1 } @installed_modules; |
| 65 |
|
| 66 |
my @all_formulas = ( |
| 67 |
uniq |
| 68 |
map { s{^GIS::Distance::}{}r } |
| 69 |
map { s{^GIS::Distance::Fast::}{}r } |
| 70 |
@installed_modules |
| 71 |
); |
| 72 |
|
| 73 |
@formulas = @all_formulas if !@formulas; |
| 74 |
|
| 75 |
my @coords = ( 34.202361, -118.601875, 37.752258, -122.441254 ); |
| 76 |
my @geo_coords = @coords[1,0,3,2]; |
| 77 |
|
| 78 |
my @tests; |
| 79 |
|
| 80 |
foreach my $formula (sort @formulas) { |
| 81 |
my $pp_module = "GIS::Distance::$formula"; |
| 82 |
if ($do_pp and $installed_modules_lookup{$pp_module}) { |
| 83 |
my $gis = GIS::Distance->new( $pp_module ); |
| 84 |
my $code = $pp_module->can('_distance'); |
| 85 |
|
| 86 |
push @tests, ( |
| 87 |
["PP $formula - GIS::Distance->distance" => sub{ |
| 88 |
return $gis->distance( @coords )->km(); |
| 89 |
}], |
| 90 |
["PP $formula - GIS::Distance->distance_metal" => sub{ |
| 91 |
return $gis->distance_metal( @coords ); |
| 92 |
}], |
| 93 |
["PP $formula - $pp_module\::_distance" => sub{ |
| 94 |
return $code->( @coords ); |
| 95 |
}], |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
my $xs_module = "GIS::Distance::Fast::$formula"; |
| 100 |
if ($do_xs and $installed_modules_lookup{$xs_module}) { |
| 101 |
my $gis = GIS::Distance->new( $xs_module ); |
| 102 |
my $code = $xs_module->can('_distance'); |
| 103 |
|
| 104 |
push @tests, ( |
| 105 |
["XS $formula - GIS::Distance->distance" => sub{ |
| 106 |
return $gis->distance( @coords )->km(); |
| 107 |
}], |
| 108 |
["XS $formula - GIS::Distance->distance_metal" => sub{ |
| 109 |
return $gis->distance_metal( @coords ); |
| 110 |
}], |
| 111 |
["XS $formula - $xs_module\::_distance" => sub{ |
| 112 |
return $code->( @coords ); |
| 113 |
}], |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
my $geo_formula = $gis_to_geo{ $formula }; |
| 118 |
if ($do_geo and $geo_formula) { |
| 119 |
my $geo = Geo::Distance->new(); |
| 120 |
$geo->formula( $geo_formula ); |
| 121 |
|
| 122 |
push @tests, ( |
| 123 |
["PP $geo_formula - Geo::Distance->old_distance" => sub{ |
| 124 |
return $geo->old_distance( 'kilometer', @geo_coords ); |
| 125 |
}], |
| 126 |
) if $do_pp; |
| 127 |
|
| 128 |
push @tests, ( |
| 129 |
["XS $geo_formula - Geo::Distance->new_distance" => sub{ |
| 130 |
return $geo->new_distance( 'kilometer', @geo_coords ); |
| 131 |
}], |
| 132 |
["XS $geo_formula - Geo::Distance::XS->distance" => sub{ |
| 133 |
return $geo->distance( 'kilometer', @geo_coords ); |
| 134 |
}], |
| 135 |
["XS $geo_formula - Geo::Distance::XS::distance" => sub{ |
| 136 |
return Geo::Distance::XS::distance( |
| 137 |
$geo, 'kilometer', @geo_coords, |
| 138 |
); |
| 139 |
}], |
| 140 |
) if $do_xs; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if ($dry_run) { |
| 145 |
print "Tests to run:\n"; |
| 146 |
print( |
| 147 |
map { " - $_\n" } |
| 148 |
sort |
| 149 |
map { $_->[0] } |
| 150 |
@tests |
| 151 |
); |
| 152 |
exit; |
| 153 |
} |
| 154 |
|
| 155 |
# Run them all once in case there are runtime errors. |
| 156 |
foreach my $test (@tests) { $test->[1]->() } |
| 157 |
|
| 158 |
my @time = gmtime( time() ); |
| 159 |
my $stamp = sprintf( |
| 160 |
'%04d-%02d-%02dT%02d:%02d:%02dZ', |
| 161 |
$time[5] + 1900, |
| 162 |
$time[4] + 1, |
| 163 |
$time[3], |
| 164 |
$time[2], $time[1], $time[0], |
| 165 |
); |
| 166 |
print "$stamp\n"; |
| 167 |
|
| 168 |
my %versions; |
| 169 |
|
| 170 |
foreach my $module (keys %INC) { |
| 171 |
next if $module !~ m{\.pm$}; |
| 172 |
|
| 173 |
$module =~ s{\.pm$}{}; |
| 174 |
$module =~ s{/}{::}g; |
| 175 |
|
| 176 |
next unless $module->can('distance') |
| 177 |
or $module eq 'GIS::Distance::Fast'; |
| 178 |
|
| 179 |
my $version = eval("\$$module\::VERSION"); |
| 180 |
$versions{$module} = $version if $version; |
| 181 |
} |
| 182 |
|
| 183 |
foreach my $module (sort keys %versions) { |
| 184 |
print "$module $versions{ $module }\n"; |
| 185 |
} |
| 186 |
|
| 187 |
cmpthese( |
| 188 |
$iters, |
| 189 |
{ map { @$_ } @tests }, |
| 190 |
); |
| 191 |
|
| 192 |
__END__ |
| 193 |
|
| 194 |
=head1 NAME |
| 195 |
|
| 196 |
author/bench - Benchmark formulas and the various interfaces to them. |
| 197 |
|
| 198 |
=head1 SYNOPSIS |
| 199 |
|
| 200 |
# Benchmark every GIS::Distance and GIS::Distance::Fast formula. |
| 201 |
author/bench |
| 202 |
|
| 203 |
# Show which benchmarks would be run for every GIS::Distance::Fast |
| 204 |
# and Geo::Distance::XS formula, but don't run them. |
| 205 |
author/bench --geo --no-pp --dry-run |
| 206 |
|
| 207 |
# Displays this handy documentation! |
| 208 |
author/bench --help |
| 209 |
|
| 210 |
=head1 OPTIONS |
| 211 |
|
| 212 |
=head2 formula |
| 213 |
|
| 214 |
--formula=<formula-short-name> |
| 215 |
--formaul=Null |
| 216 |
-f Haversine -f Cosine |
| 217 |
|
| 218 |
By default all formulas will be considered for benchmarking. |
| 219 |
If you'd like to limit this then you can specify one or more |
| 220 |
formulas with this option. |
| 221 |
|
| 222 |
=head2 pp |
| 223 |
|
| 224 |
--no-pp |
| 225 |
|
| 226 |
Disable the C<pp> option, meaning no pure-perl formulas will |
| 227 |
be included. |
| 228 |
|
| 229 |
=head2 xs |
| 230 |
|
| 231 |
--no-xs |
| 232 |
|
| 233 |
Disable the C<xs> option, meaning no XS formulas will |
| 234 |
be included. |
| 235 |
|
| 236 |
=head2 geo |
| 237 |
|
| 238 |
--geo |
| 239 |
|
| 240 |
L<Geo::Distance> and/or L<Geo::Distance::XS> formulas will be |
| 241 |
included. |
| 242 |
|
| 243 |
=head2 iters |
| 244 |
|
| 245 |
--iters=100 |
| 246 |
-i 10000000 |
| 247 |
|
| 248 |
The number of iterations to benchmark each formula against. |
| 249 |
|
| 250 |
The default is C<5,000,000>. |
| 251 |
|
| 252 |
=head2 dry-run |
| 253 |
|
| 254 |
--dry-run |
| 255 |
-n |
| 256 |
|
| 257 |
Lists all the benchmarks which would be run, but does not run them. |
| 258 |
|
| 259 |
=head2 help |
| 260 |
|
| 261 |
--help |
| 262 |
-h |
| 263 |
|
| 264 |
Displays this handy documentation! |
| 265 |
|
| 266 |
=head1 AUTHORS AND LICENSE |
| 267 |
|
| 268 |
See L<GIS::Distance/AUTHORS> and L<GIS::Distance/LICENSE>. |
| 269 |
|
| 270 |
=cut |
| 271 |
|