Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

What's causing the busy wait when using AnyEvent::HTTP

by Anonymous Monk
on Apr 10, 2014 at 22:52 UTC ( #1081889=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a simple load tester to test my web app using AnyEvent::HTTP. For the most part it works fine, but if the site becomes non-responsive, eg. it holds open the request connections but doesn't return a response, the load tester goes into a busy wait using 100% cpu, until all the requests time out. I tried different AE loops, and disabled persistent connections, but nothing I tried resolves the issue. And since it's intermittent, I haven't been able to catch it with Devel::NYTProf either. I've included the basic skeleton of the script below. Any pointers to the cause would be much appreciated.
#!/usr/bin/env perl use strict; use warnings; use AnyEvent::Loop; # use the perl loop, not EV use AnyEvent::HTTP; use AnyEvent::Util 'guard'; use BSD::Resource; my $rps = 50; my $timeout = 30; my $duration = 300; my $delay = 60 / $rps / 60; setrlimit RLIMIT_OFILE, 2048, 2048; $AnyEvent::HTTP::MAX_PER_HOST = 1024; my $cv = AE::cv; my $timer = AE::timer 0, $delay, \&do_request; my $wait = AE::timer $duration, 0, sub { undef $timer }; my $halt = AE::timer $duration + $timeout, 0, sub { $cv->send }; $cv->recv; sub do_request { $cv->begin; http_request( GET => '', timeout => $timeout, # I also tried limiting the concurrent requests per connection + with # the following: # persistant => 0, sub { my $guard = guard { $cv->end }; # ... } ); }

Replies are listed 'Best First'.
Re: What's causing the busy wait when using AnyEvent::HTTP
by Corion (Pope) on Apr 11, 2014 at 06:44 UTC

    I haven't looked into AnyEvent::HTTP, but if your HTTP request callback never gets called, your code fragment looks as if $cv will never be decremented. This could then turn into an infinite loop.

      That would present differently. The program would never end, but would not use 100% cpu.
Re: What's causing the busy wait when using AnyEvent::HTTP
by basiliscos (Pilgrim) on Apr 11, 2014 at 08:41 UTC

    If you plan to cancel http requests, don't call http_request in void context, as documentation says.

    Also documentation recommends to set AnyEvent::HTTP::MAX_PER_HOST = 4, while you set it to 1024. I'm not sure, but I recommend you try to decrease that value.

      It sounds like you've never used AnyEvent::HTTP before and just scanned the docs to try to answer the question. When http_request is called in void context, it's still easy to cancel a request from within the callback. And you don't understand what load testing is if you suggest limiting concurrent requests to 4.

        Thank you for warm tone of your reply and flattering assumptions about my knowledge.

        Actually, I have written some kind of proxy, which uses AE::HTTP for backend querying. There I've met the limitation HTTP::MAX_PER_HOST = 4

        You request can't be cancelled inside callback, because it is invoked only after http call (successful or unsuccessful).

        I think, I do understand well what is load testing and how AE works. It works in single process CPU thread, that means, that it gets some performance boost only during I/O wait operations via processing or enqueueing other requests. So, might reach the limit of parallel requests much earlier, then you'll get 1000 simultaneously running http requests, due to limitations of the single core CPU. So, perl / AE scheduler might be just overloaded in doing http requests, processing results, and switching between contexts.

        It may be better to gradually rise the number of simultaneous http requests. According to that metrics: AE::HTTPD (written in similar manner as AE::HTTP), serves only about 560 rps (on single CPU core, as I understand). That explains well, that doing 1000 dummy requests, will load your CPU to 100%.

        I can advice you to use ab from apache-tools package for simple load testing, because it is written in C, and aimed for loading.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081889]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2017-06-26 22:59 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (595 votes). Check out past polls.