使用perl AnyEvent测量个人时间

时间:2015-12-28 12:58:44

标签: perl libev anyevent

我需要获取许多http网址,我使用AnyEvent :: HTTP来执行此操作 对于每个URL我需要测量时间,我该怎么做?

我的代码(已剥离)在这里

    #!/usr/bin/perl                                                                                                                                                          

use strict;
use AnyEvent::HTTP;
use AnyEvent::Socket;
use Data::Dumper;


my $internal_ip=v192.168.2.103;   #Use this ip to bind instead of default ip. Harcoding necessary :-( using v$ip                                                         

sub prep_cb {
    my ($socket)=@_;
    my $bind = AnyEvent::Socket::pack_sockaddr undef, $internal_ip;
    # I need to start the time here                                                                                                                                      
    bind $socket, $bind
         or die "bind: $!";
}

my $url="http://192.168.2.105/echo.php";

my $anyevent = AnyEvent->condvar;
$anyevent->begin;
http_request(
    "GET" => $url,
    on_prepare =>\&prep_cb,
    sub {
        my ($data, $hdr) = @_;
        $anyevent->end;
        # I need to measure the time taken                                                                                                                               
        print Dumper([$data,$hdr]);
    }
    );
$anyevent->recv;

2 个答案:

答案 0 :(得分:0)

如果用以下内容替换http_request(),该怎么办:

my $timer;
http_request(
  "GET" => $url,
  on_prepare => sub {$timer = time; prep_cb},
  sub {
      my ($data, $hdr) = @_;
      $anyevent->end;
      print "Took " . (time - $timer) . " seconds.\n";               
      print Dumper([$data,$hdr]);
  }
);

答案 1 :(得分:0)

更简单的方法是拥有一个变量并在ng-repeat上更新它并按照TheAmigo

的提法在on_prepare之后记录它

分析/计算任何功能的一般方法: 假设你的函数是$anyevent->end, 你可以像这样称呼它

fetchHttpUrl($url)