Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created August 31, 2018 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/3b80cc103ab549390ba672dd3b754083 to your computer and use it in GitHub Desktop.
Save xtetsuji/3b80cc103ab549390ba672dd3b754083 to your computer and use it in GitHub Desktop.
measure script for SMTP connection establish times
#!/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