Last active
December 15, 2015 07:49
-
-
Save bokutin/5226003 to your computer and use it in GitHub Desktop.
Validation::Class is slower than HTML::FormHandler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Simple::HFH { | |
use HTML::FormHandler::Moose; | |
extends 'HTML::FormHandler'; | |
has_field name => ( type => 'Text', required => 1 ); | |
__PACKAGE__->meta->make_immutable; | |
} | |
package main { | |
use Mojo::Base -strict; | |
use Benchmark ':all'; | |
use Data::Domain ':all'; | |
use Data::Validator; | |
use Validation::Class::Simple; | |
my $vc = Validation::Class::Simple->new( | |
fields => { name => { required => 1 } }, | |
); | |
my $hfh = Simple::HFH->new; | |
my $domain = Struct( name => String(-min_length=>1) ); | |
my $dv = Data::Validator->new( | |
name => { isa => 'Str' }, | |
)->with('NoThrow'); | |
my $check_vc = sub { | |
$vc->params->add($_[0]); | |
$vc->validate; | |
}; | |
my $check_hfh = sub { | |
$hfh->process( params => $_[0], no_update => 1 ); | |
}; | |
my $check_domain = sub { | |
!$domain->inspect($_[0]); | |
}; | |
my $check_dv = sub { | |
$dv->validate($_[0]); | |
$dv->has_errors ? 0 : 1; | |
}; | |
my $params_ok = { name => 'name' }; | |
my $params_bad = { name => undef }; | |
$check_vc ->($params_ok) or die; | |
!$check_vc ->($params_bad) or die; | |
$check_hfh ->($params_ok) or die; | |
!$check_hfh ->($params_bad) or die; | |
$check_domain->($params_ok) or die; | |
!$check_domain->($params_bad) or die; | |
$check_dv->($params_ok) or die; | |
!$check_dv->($params_bad) or die; | |
my $count = 10000; | |
cmpthese($count, { | |
vc => sub { $check_vc->($params_ok) }, | |
hfh => sub { $check_hfh->($params_ok) }, | |
domain => sub { $check_domain->($params_ok) }, | |
dv => sub { $check_dv->($params_ok) }, | |
}); | |
} | |
#% perl benchmark/validator.pl | |
# (warning: too few iterations for a reliable count) | |
# (warning: too few iterations for a reliable count) | |
# Rate vc hfh domain dv | |
#vc 345/s -- -71% -99% -100% | |
#hfh 1211/s 250% -- -97% -99% | |
#domain 40000/s 11480% 3204% -- -72% | |
#dv 142857/s 41257% 11700% 257% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment