在Perl

时间:2015-07-20 12:45:34

标签: perl benchmarking www-mechanize

我正在使用Google网站解释我的问题。

我有一个带有表单的网页。我正在使用WWW::Mechanize来抓取并填写表单,并注明$s1中的开始时间。

我正在阅读下一页的标题,并注明$s2中的结束时间。

然后我按$s2 - $s1计算等待时间。

这是我的代码

use WWW::Mechanize;
use LWP::UserAgent;
use Time::HiRes qw/gettimeofday/;
use Time::Format qw/%time/;
use POSIX qw( strftime );
use Time::HiRes qw( gettimeofday );

$m = WWW::Mechanize->new();
$m->get( "http://www.google.com" );
$m->submit_form(
    form_number => 1,
    fields      => { q => 'abcd' },
);

$s1 = gettimeofday;
my ($secs, $microsecs) = gettimeofday();
print strftime("%H:%M:%S", localtime($secs)) . sprintf(".%04d", $microsecs/10);

print " ";
print $m->title;
print " ";

$s2 = gettimeofday;
my ($secs, $microsecs) = gettimeofday();
print strftime("%H:%M:%S", localtime($secs)) . sprintf(".%04d", $microsecs/10);

$s3 = $s2 - $s1;
$s3 = $s3 * 1000;
print "  $s3\n";

输出结果为:

18:02:42.71923 abcd - Google Search 18:02:42.71996  0.737905502319336

现在,我在Parallel Fork Manager中使用了相同的代码,将loopcount保持为1 我的代码:

use Parallel::ForkManager;
use WWW::Mechanize;
use LWP::UserAgent;
use Time::HiRes qw/gettimeofday/;
use Time::Format qw/%time/;
use POSIX qw( strftime );
use Time::HiRes qw( gettimeofday );

$count = 1;

$pm = new Parallel::ForkManager($count);

for ( 1 .. $count ) {

  $pm->start and next;

  $m = WWW::Mechanize->new();
  $m->get( "http://www.google.com" );
  $m->submit_form(
      form_number => 1,
      fields      => { q => 'abcd' },
  );

  $s1 = gettimeofday;
  my ($secs, $microsecs) = gettimeofday();
  print strftime("%H:%M:%S", localtime($secs)) . sprintf(".%04d", $microsecs/10);

  print " ";
  print $m->title;
  print " ";

  $s2 = gettimeofday;
  my ($secs, $microsecs) = gettimeofday();
  print strftime("%H:%M:%S", localtime($secs)) . sprintf(".%04d", $microsecs/10);

  $s3 = $s2 - $s1;
  $s3 = $s3 * 1000;
  print "  $s3\n";

  $pm->finish
}  

$pm->wait_all_children; ## wait for the child processes

我的输出:

18:02:48.55297 abcd - Google Search 18:02:48.55325  0.282049179077148

我无法理解使用fork时延迟时间有多大差异(减少到1/3)。应该是一样的。任何的想法。我的主要目的是记录1000-2000个请求的延迟时间并找到平均值。由于直接代码和fork下的相同代码之间存在很大差异,我不确定延迟时间是否正确。

1 个答案:

答案 0 :(得分:3)

您无法衡量互联网响应时间,您只是计算一些打印语句和函数调用。如果您的互联网连接在半毫秒内响应,那么我感到很惊讶!

尝试使用此代码,该代码执行类似的操作,但需要访问互联网请求/响应次数

use strict;
use warnings;

use WWW::Mechanize;
use Time::HiRes 'gettimeofday';

my $mech = WWW::Mechanize->new;

$mech->get( 'http://www.google.com/' );

my $t0 = gettimeofday;

$mech->submit_form(
    form_number => 1,
    fields      => { q => 'abcd' },
);

my $t1 = gettimeofday;

printf "Title: %s\n",  $mech->title;
printf "Delay: %5.3fs\n", $t1 - $t0;

输出

Title: abcd - Google Search
Delay: 1.784s