How to create multiple synchronous connections to the web server?

#!/usr/bin/perl

###########################################################
# Filename: loadgenerator.php
# Author:   Paul Greenberg
# Website:  www.isrcomputing.com
# License:  GPLv3
# Purpose:  This script creates predefined number of threads,
# and each of the threads fetches specified URL.
# It is a tool for stress-testing applications.
###########################################################

use strict;
use warnings;
use LWP;
use threads;
use threads::shared;
use Thread::Queue;

my $cnt_pending = 0;
my $NumberOfThreads = 5;
my $q=Thread::Queue->new;
my $failure=Thread::Queue->new;
my $success=Thread::Queue->new;

my $url = 'http://www.google.com';

my $ua = LWP::UserAgent->new;

for my $i ( 1 .. 70 ) {
 $q->enqueue($i);
}

#Create threads
my @th;
for(1..$NumberOfThreads) {
    push @th, threads->create(\&fetchUrl);
}

sub fetchUrl {
 while(my $page=$q->dequeue_nb) {
  return unless $page;
  if(getUrl() == 0) {
   $success->enqueue($page);
  }    else {
   $failure->enqueue($page);
  }
 }
}

sub getUrl {
  my $req = HTTP::Request->new(POST => $url);
  my $content = $ua->request($req);
  #print $content->as_string;
  print $content->code."\n";
  if($content->code eq "200") {
   return 0;    
  } else {
   return 1;
  }
}


#Wait for threads to finish
while(my $cnt_pending=$q->pending) {
    print "Pages remaining to be fetched: $cnt_pending\n";
    sleep(1);
}
 
#Join all threads
$_->join for(@th);
 
#Display results:
my $cnt_success=$success->pending;
my $cnt_failure=$failure->pending;
while(my $f=$failure->dequeue_nb) {
    last unless $f;
    print "Down: $f\n";
}
print "Total: " . ($cnt_success+$cnt_failure) . "\n";
print "Success: $cnt_success\n";
print "Failure: $cnt_failure\n";
 
 

   

Moving your apps to Amazon or Miscrosoft Clouds?

We can help you analyze your existing infrastructure, identify the cost savings we can achieve by migrating to a cloud provider. We can then execute end-to-end migration plan of your infrastructure and bringing down your TCO.

Cloud Computing

Ready for IPv6 Migration?

The Internet is running out of the equivalent of phone numbers - familiar problem, non-trivial solution.

The world has to move to IPv6, with its 128-bit addresses. But that's easier said than done.

IPv6 Migration

Are you fluent in "Linux"?

Learn Linux from a leading expert and quickly master you Linux skills.

Learn how to simplify your workflow and increase your productivity using tips and techniques of the pros.

Ideal training for Corporate IT Beginners and Advanced IT Admins alike.

Corporate Linux Training

Who's Online

We have 6 guests and no members online