| 1 | package App::Benchmark::WAF; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use base qw(Exporter); |
|---|
| 5 | use Apache::TestConfig; |
|---|
| 6 | use Apache::TestUtil qw(t_debug); |
|---|
| 7 | use File::Spec; |
|---|
| 8 | use IPC::Open3; |
|---|
| 9 | use Test::More; |
|---|
| 10 | use URI; |
|---|
| 11 | our $VERSION = '0.00001'; |
|---|
| 12 | |
|---|
| 13 | our @EXPORT = qw(waf_benchmark_diag); |
|---|
| 14 | our $AB; |
|---|
| 15 | BEGIN { |
|---|
| 16 | require File::Spec->catfile('t', 'conf', 'apache_test_config.pm'); |
|---|
| 17 | my $config = apache_test_config->new(); |
|---|
| 18 | $AB = $ENV{APACHE_BENCH} || |
|---|
| 19 | File::Spec->catfile($config->{vars}->{bindir}, "ab"); |
|---|
| 20 | |
|---|
| 21 | if (! -x $AB) { |
|---|
| 22 | plan(skip_all => "could not find a working $AB"); |
|---|
| 23 | } else { |
|---|
| 24 | plan('no_plan'); |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | sub waf_benchmark_diag { |
|---|
| 29 | my %args = @_; |
|---|
| 30 | |
|---|
| 31 | my $mode = $args{mode}; |
|---|
| 32 | my $type = $args{type}; |
|---|
| 33 | |
|---|
| 34 | my $config = apache_test_config->new(); |
|---|
| 35 | my $uri = URI->new(); |
|---|
| 36 | $uri->scheme('http'); |
|---|
| 37 | $uri->host( $config->our_remote_addr() ); |
|---|
| 38 | $uri->port( $config->port ); |
|---|
| 39 | $uri->path( $args{path} || join('/', $mode, $type, 'index.cgi' ) ); |
|---|
| 40 | |
|---|
| 41 | my $post = $args{post} |
|---|
| 42 | ? "-p $args{post} -T \"application/x-www-form-urlencoded\"" : ''; |
|---|
| 43 | my @cmd = ($AB, "-c", $args{concurrency} || 100, "-n", $args{requests} || 1000, $post, '-v 2', $uri); |
|---|
| 44 | |
|---|
| 45 | t_debug("running $type in $mode ($uri)"); |
|---|
| 46 | t_debug("command: @cmd"); |
|---|
| 47 | |
|---|
| 48 | # capture output... |
|---|
| 49 | { |
|---|
| 50 | my ($stdout, $stdin, $stderr); |
|---|
| 51 | open3( $stdin, $stdout, $stderr, "@cmd" ); |
|---|
| 52 | while (<$stdout>) { |
|---|
| 53 | t_debug($_); |
|---|
| 54 | warn $_; |
|---|
| 55 | next unless /Requests per second:\s*(.*)$/; |
|---|
| 56 | diag("[$type ($mode)]: $1"); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | ok(1); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | 1; |
|---|