[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
SlideShare a Scribd company logo
最近リリースした
N個のCPANモジュール
Yokohama.pm #10
2014/02/21
kazeburo
Time::Crontab
parser for crontab date and time field
use Time::Crontab;
my $time_cron = Time::Crontab->new('0 0 1 * *');
if ( $time_cron->match(time()) ) {
do_cron_job();
}
Proclet@0.32
supports cron like job
use Proclet;
my $proclet = Proclet->new();
$proclet->service(...);
$proclet->service(...);
$proclet->service(
code => sub {
scheduled_work();
},
tag => 'cron',
every => '0 12 * * *', #everyday at 12:00am
);
$proclet->run;
Apache::LogFormat::Compiler
@0.14 bugfix around DST
@0.30 improvement strftime(2)
Apache::LogFormat::Compiler has a
problem. Twice a year the timezone is
changed because of daylight saving.
A::LF::C ignores this fact and still
shows old timezone after change.
https://github.com/kazeburo/Apache-LogFormat-Compiler/pull/3

dex4er

ALFC、DST(サマータイム)をサポートしてないよ!
version < 0.14
local $ENV{TZ} = 'America/New_York';
POSIX::tzset;
my $time = timelocal(0, 0, 1, 3, 11 - 1, 2013);
my $log_handler = Apache::LogFormat::Compiler->new();
my $req = req_to_psgi(GET "/");
my $res = [200,[],[q!OK!]];
print $log_handler->log_line($req,$res,0,2,$time);
print $log_handler->log_line($req,$res,0,2,$time+3599);
print $log_handler->log_line($req,$res,0,2,$time+3600);
時間が戻ってる!
__DATA__
127.0.0.1 - - [03/Nov/2013:01:00:00 -0400] "GET / HTTP/1.1" 200 0 "-" "-"
127.0.0.1 - - [03/Nov/2013:01:59:59 -0400] "GET / HTTP/1.1" 200 0 "-" "-"
127.0.0.1 - - [03/Nov/2013:01:00:00 -0400] "GET / HTTP/1.1" 200 0 "-" "-"
version >= 0.14
local $ENV{TZ} = 'America/New_York';
POSIX::tzset;
my $time = timelocal(0, 0, 1, 3, 11 - 1, 2013);
my $log_handler = Apache::LogFormat::Compiler->new();
my $req = req_to_psgi(GET "/");
my $res = [200,[],[q!OK!]];
print $log_handler->log_line($req,$res,0,2,$time);
print $log_handler->log_line($req,$res,0,2,$time+3599);
オフセットが
print $log_handler->log_line($req,$res,0,2,$time+3600);
変わった
__DATA__
127.0.0.1 - - [03/Nov/2013:01:00:00 -0400] "GET / HTTP/1.1" 200 0 "-" "-"
127.0.0.1 - - [03/Nov/2013:01:59:59 -0400] "GET / HTTP/1.1" 200 0 "-" "-"
127.0.0.1 - - [03/Nov/2013:01:00:00 -0500] "GET / HTTP/1.1" 200 0 "-" "-"
I'm trying to install
Apache::LogFormat::Compiler on
Android. This system has poor
support for locales, so the Perl is
usually compiled with -Ui_locale flag.
https://github.com/kazeburo/Apache-LogFormat-Compiler/pull/6

dex4er

Andoroidがsetlocaleをサポートしてない!
$ export LC_ALL=ja_JP.UTF-8
$ perl -MPOSIX=strftime 
-E 'say strftime(q!%d/%b/%Y%T!,localtime());'

21/ 2月/2014:00:17:35

strftime(2) はlocaleによって出力が変わる
my $old_locale = POSIX::setlocale(&POSIX::LC_ALL);
POSIX::setlocale(&POSIX::LC_ALL, 'C');
$time = POSIX::strftime(‘%d/%b/%Y:%T’,localtime());
POSIX::setlocale(&POSIX::LC_ALL, $old_locale);

%{format}t がstrftime(2) を使っていて、
ALFCはstrftime(2)の前にsetlocale(LC_ALL,C)してた
       |
   \  __  /
   _ (m) _ピコーン
      |ミ|
    /  `´  \
     ('A`)
     ノヽノヽ
       くく
そうだlocaleに影響を受けない
strftime(2)を作ろう
やめておけばよかった。。
POSIX::strftime::Compiler
$ export LC_ALL=ja_JP.UTF-8
$ perl -MPOSIX::strftime::Compiler=strftime 
-E 'say strftime(q!%d/%b/%Y:%T!,localtime());'

21/Feb/2014:00:17:35

localeによって出力が変わらない! YATTA!!
Time::TZOffset
Show timezone offset strings like “+0900”
more portable than POSIX::strftime('%z') and fast

XSにてtm構造体のtm_gmtoffを取得するモジュール
gmtoffをサポートしていないOSでは
localtimeとgmtimeの差分を計算
HTTP::Entity::Parser
PSGI compliant HTTP Entity Parser
yet another HTTP::Body
based on tokuhirom's code. https://github.com/plack/Plack/pull/434

別のモジュールしてだれか進めて
use HTTP::Entity::Parser;
my $parser = HTTP::Entity::Parser->new;
$parser->register('application/x-www-form-urlencoded',
'HTTP::Entity::Parser::UrlEncoded');
$parser->register('multipart/form-data',
'HTTP::Entity::Parser::MultiPart');
$parser->register('application/json',
'HTTP::Entity::Parser::JSON');
sub app {
my $env = shift;
my ( $params, $uploads) = $parser->parse($env);
}
WWW::Form::UrlEncoded
parser and builder for application/x-www-form-urlencoded
s_id=1&type=foo&message1=foo+bar+baz+hoge
+hoge+hoge+hoge+hogehogemessage2=
%E6%97%A5%E6%9C%AC%E8%AA%9E
%E3%81%A7%E3%81%99%E3%82%88%E3%83%BC

parse

build

(s_id => 1, type => 'foo', message1 =>
'foo bar baz hoge hoge hoge hoge
hogehoge', message2 => '日本語ですよー')
'a=b&c=d'
'a=b;c=d'
'a=1&b=2;c=3'
'a==b&c==d'
'a=b& c=d'
'a=b; c=d'
'a=b; c =d'
'a=b;c= d '
'a=b&+c=d'
'a=b&+c+=d'
'a=b&c=+d+'
'a=b&%20c=d'
'a=b&%20c%20=d'
'a=b&c=%20d%20'
'a&c=d'
'a=b&=d'
'a=b&='
'&'
'='
''

=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>

["a","b","c","d"]
["a","b","c","d"]
["a","1","b","2","c","3"]
["a","=b","c","=d"]
["a","b","c","d"]
["a","b","c","d"]
["a","b","c ","d"]
["a","b","c"," d "]
["a","b"," c","d"]
["a","b"," c ","d"]
["a","b","c"," d "]
["a","b"," c","d"]
["a","b"," c ","d"]
["a","b","c"," d "]
["a","","c","d"]
["a","b","","d"]
["a","b","",""]
["","","",""]
["",""]
Plack::RequestやHTTP::Bodyとの
[]

互換性を維持する
WWW::Form::UrlEncoded::XS
XS implementation of parser and builder for application/x-www-form-urlencoded
Benchmark: running parse_pp, parse_xs
for at least 3 CPU seconds...
parse_pp: 3 wallclock secs
( 3.00 usr + 0.00 sys = 3.00 CPU)
@ 27353.00/s (n=82059)
parse_xs: 3 wallclock secs
( 3.02 usr + 0.00 sys = 3.02 CPU)
@ 258802.32/s (n=781583)
Rate
parse_pp 27353/s
parse_xs 258802/s

parse_p
-846%

parseのベンチマークで

parse_xs
-89%
--

9.5倍高速
Benchmark: running build_pp, build_xs
for at least 3 CPU seconds...
build_pp: 4 wallclock secs
( 3.40 usr + 0.01 sys = 3.41 CPU)
@ 15048.68/s (n=51316)
build_xs: 4 wallclock secs
( 3.08 usr + 0.01 sys = 3.09 CPU)
@ 651364.08/s (n=2012715)
Rate build_pp build_xs
build_pp 15049/s
--98%
build_xs 651364/s
4228%
--

43倍高速

buildは
## content length => 38 ##
Rate
http_body http_entity
http_body
35870/s
--41%
http_entity 60703/s
69%
-## content length => 177 ##
Rate
http_body http_entity
http_body
14355/s
--74%
http_entity 54371/s
279%
-## content length => 1997 ##
Rate
http_body http_entity
http_body
2054/s
--92%
http_entity 27049/s
1217%
--

WWW::Form::UrlEncoded::XSをいれた
HTTP::Entity::Parserのベンチマーク
これからの課題
•

Plackに取り込んでもらう
*Plackの依存モジュールは基本Pure Perl

•

Plack::(?:Request|Response)の高速版を作る
*互換性を保ちつつXS利用
いじょう。

More Related Content

最近作ったN個のCPANモジュール Yokohama.pm #10

  • 2. Time::Crontab parser for crontab date and time field
  • 3. use Time::Crontab; my $time_cron = Time::Crontab->new('0 0 1 * *'); if ( $time_cron->match(time()) ) { do_cron_job(); }
  • 5. use Proclet; my $proclet = Proclet->new(); $proclet->service(...); $proclet->service(...); $proclet->service( code => sub { scheduled_work(); }, tag => 'cron', every => '0 12 * * *', #everyday at 12:00am ); $proclet->run;
  • 6. Apache::LogFormat::Compiler @0.14 bugfix around DST @0.30 improvement strftime(2)
  • 7. Apache::LogFormat::Compiler has a problem. Twice a year the timezone is changed because of daylight saving. A::LF::C ignores this fact and still shows old timezone after change. https://github.com/kazeburo/Apache-LogFormat-Compiler/pull/3 dex4er ALFC、DST(サマータイム)をサポートしてないよ!
  • 8. version < 0.14 local $ENV{TZ} = 'America/New_York'; POSIX::tzset; my $time = timelocal(0, 0, 1, 3, 11 - 1, 2013); my $log_handler = Apache::LogFormat::Compiler->new(); my $req = req_to_psgi(GET "/"); my $res = [200,[],[q!OK!]]; print $log_handler->log_line($req,$res,0,2,$time); print $log_handler->log_line($req,$res,0,2,$time+3599); print $log_handler->log_line($req,$res,0,2,$time+3600); 時間が戻ってる! __DATA__ 127.0.0.1 - - [03/Nov/2013:01:00:00 -0400] "GET / HTTP/1.1" 200 0 "-" "-" 127.0.0.1 - - [03/Nov/2013:01:59:59 -0400] "GET / HTTP/1.1" 200 0 "-" "-" 127.0.0.1 - - [03/Nov/2013:01:00:00 -0400] "GET / HTTP/1.1" 200 0 "-" "-"
  • 9. version >= 0.14 local $ENV{TZ} = 'America/New_York'; POSIX::tzset; my $time = timelocal(0, 0, 1, 3, 11 - 1, 2013); my $log_handler = Apache::LogFormat::Compiler->new(); my $req = req_to_psgi(GET "/"); my $res = [200,[],[q!OK!]]; print $log_handler->log_line($req,$res,0,2,$time); print $log_handler->log_line($req,$res,0,2,$time+3599); オフセットが print $log_handler->log_line($req,$res,0,2,$time+3600); 変わった __DATA__ 127.0.0.1 - - [03/Nov/2013:01:00:00 -0400] "GET / HTTP/1.1" 200 0 "-" "-" 127.0.0.1 - - [03/Nov/2013:01:59:59 -0400] "GET / HTTP/1.1" 200 0 "-" "-" 127.0.0.1 - - [03/Nov/2013:01:00:00 -0500] "GET / HTTP/1.1" 200 0 "-" "-"
  • 10. I'm trying to install Apache::LogFormat::Compiler on Android. This system has poor support for locales, so the Perl is usually compiled with -Ui_locale flag. https://github.com/kazeburo/Apache-LogFormat-Compiler/pull/6 dex4er Andoroidがsetlocaleをサポートしてない!
  • 11. $ export LC_ALL=ja_JP.UTF-8 $ perl -MPOSIX=strftime -E 'say strftime(q!%d/%b/%Y%T!,localtime());' 21/ 2月/2014:00:17:35 strftime(2) はlocaleによって出力が変わる
  • 12. my $old_locale = POSIX::setlocale(&POSIX::LC_ALL); POSIX::setlocale(&POSIX::LC_ALL, 'C'); $time = POSIX::strftime(‘%d/%b/%Y:%T’,localtime()); POSIX::setlocale(&POSIX::LC_ALL, $old_locale); %{format}t がstrftime(2) を使っていて、 ALFCはstrftime(2)の前にsetlocale(LC_ALL,C)してた
  • 13.        |    \  __  /    _ (m) _ピコーン       |ミ|     /  `´  \      ('A`)      ノヽノヽ        くく そうだlocaleに影響を受けない strftime(2)を作ろう やめておけばよかった。。
  • 15. $ export LC_ALL=ja_JP.UTF-8 $ perl -MPOSIX::strftime::Compiler=strftime -E 'say strftime(q!%d/%b/%Y:%T!,localtime());' 21/Feb/2014:00:17:35 localeによって出力が変わらない! YATTA!!
  • 16. Time::TZOffset Show timezone offset strings like “+0900” more portable than POSIX::strftime('%z') and fast XSにてtm構造体のtm_gmtoffを取得するモジュール gmtoffをサポートしていないOSでは localtimeとgmtimeの差分を計算
  • 17. HTTP::Entity::Parser PSGI compliant HTTP Entity Parser yet another HTTP::Body based on tokuhirom's code. https://github.com/plack/Plack/pull/434 別のモジュールしてだれか進めて
  • 18. use HTTP::Entity::Parser; my $parser = HTTP::Entity::Parser->new; $parser->register('application/x-www-form-urlencoded', 'HTTP::Entity::Parser::UrlEncoded'); $parser->register('multipart/form-data', 'HTTP::Entity::Parser::MultiPart'); $parser->register('application/json', 'HTTP::Entity::Parser::JSON'); sub app { my $env = shift; my ( $params, $uploads) = $parser->parse($env); }
  • 19. WWW::Form::UrlEncoded parser and builder for application/x-www-form-urlencoded
  • 20. s_id=1&type=foo&message1=foo+bar+baz+hoge +hoge+hoge+hoge+hogehogemessage2= %E6%97%A5%E6%9C%AC%E8%AA%9E %E3%81%A7%E3%81%99%E3%82%88%E3%83%BC parse build (s_id => 1, type => 'foo', message1 => 'foo bar baz hoge hoge hoge hoge hogehoge', message2 => '日本語ですよー')
  • 21. 'a=b&c=d' 'a=b;c=d' 'a=1&b=2;c=3' 'a==b&c==d' 'a=b& c=d' 'a=b; c=d' 'a=b; c =d' 'a=b;c= d ' 'a=b&+c=d' 'a=b&+c+=d' 'a=b&c=+d+' 'a=b&%20c=d' 'a=b&%20c%20=d' 'a=b&c=%20d%20' 'a&c=d' 'a=b&=d' 'a=b&=' '&' '=' '' => => => => => => => => => => => => => => => => => => => => ["a","b","c","d"] ["a","b","c","d"] ["a","1","b","2","c","3"] ["a","=b","c","=d"] ["a","b","c","d"] ["a","b","c","d"] ["a","b","c ","d"] ["a","b","c"," d "] ["a","b"," c","d"] ["a","b"," c ","d"] ["a","b","c"," d "] ["a","b"," c","d"] ["a","b"," c ","d"] ["a","b","c"," d "] ["a","","c","d"] ["a","b","","d"] ["a","b","",""] ["","","",""] ["",""] Plack::RequestやHTTP::Bodyとの [] 互換性を維持する
  • 22. WWW::Form::UrlEncoded::XS XS implementation of parser and builder for application/x-www-form-urlencoded
  • 23. Benchmark: running parse_pp, parse_xs for at least 3 CPU seconds... parse_pp: 3 wallclock secs ( 3.00 usr + 0.00 sys = 3.00 CPU) @ 27353.00/s (n=82059) parse_xs: 3 wallclock secs ( 3.02 usr + 0.00 sys = 3.02 CPU) @ 258802.32/s (n=781583) Rate parse_pp 27353/s parse_xs 258802/s parse_p -846% parseのベンチマークで parse_xs -89% -- 9.5倍高速
  • 24. Benchmark: running build_pp, build_xs for at least 3 CPU seconds... build_pp: 4 wallclock secs ( 3.40 usr + 0.01 sys = 3.41 CPU) @ 15048.68/s (n=51316) build_xs: 4 wallclock secs ( 3.08 usr + 0.01 sys = 3.09 CPU) @ 651364.08/s (n=2012715) Rate build_pp build_xs build_pp 15049/s --98% build_xs 651364/s 4228% -- 43倍高速 buildは
  • 25. ## content length => 38 ## Rate http_body http_entity http_body 35870/s --41% http_entity 60703/s 69% -## content length => 177 ## Rate http_body http_entity http_body 14355/s --74% http_entity 54371/s 279% -## content length => 1997 ## Rate http_body http_entity http_body 2054/s --92% http_entity 27049/s 1217% -- WWW::Form::UrlEncoded::XSをいれた HTTP::Entity::Parserのベンチマーク