Created
August 31, 2018 04:54
-
-
Save xtetsuji/3b80cc103ab549390ba672dd3b754083 to your computer and use it in GitHub Desktop.
measure script for SMTP connection establish times
This file contains 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
#!/usr/bin/perl | |
# smtp-connect-bench.pl [HOSTNAME|IPADDR] | |
# SMTP 接続の速度を確認する | |
use strict; | |
use warnings; | |
use Net::SMTP; | |
use Time::HiRes qw(time sleep); # ミリ秒化 | |
use constant WAIT_SECONDS => 15; | |
use constant SMTP_TIMEOUT_SECONDS => 120; | |
$| = 1; # flush output buffer: 改行無し print 迅速化 | |
my $hostname = shift || '127.0.0.1'; | |
while (1) { | |
print "connection start... "; | |
local $@; | |
my $take_seconds = measure_time( sub { | |
my $smtp = Net::SMTP->new($hostname, Timeout => SMTP_TIMEOUT_SECONDS); | |
# $smtp->hello("localhost"); | |
$smtp->quit(); | |
}); | |
printf "take %.2f seconds\n", $take_seconds; | |
if ( $@ ) { print " Exception: $@\n"; } | |
sleep WAIT_SECONDS; # 過度な連続接続防止 | |
} | |
# measure_time( sub { ... } ) | |
# 第1引数のコードリファレンスを実行した秒数を返す | |
# 例外変数 $@ は上流で初期化する | |
sub measure_time { | |
my $code = shift; | |
my $start_time = time; | |
eval { $code->(); }; | |
my $end_time = time; | |
return $end_time - $start_time; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment