Simon Heimlicher

Simon Heimlicher's Homepage

Fix Apache Bench ('AB') on OS X Lion

The popular web server benchmark tool Apache Bench, better known as ab, comes built in with the Apache httpd web server of OS X, but it has serious issues with the latest relaese, OS X Lion. To add insult to injury, the official release has another bug that manifests itself fatally on OS X Lion. This latter bug is related to a feature that was added recently and it breaks ab on IPv6-capable hosts. Even if you have never heard of IPv6, OS X Lion has IPv6 built in.

The latest development version of Apache (Subversion trunk) has a fix for this bug, however this fix is not yet available in a regular release.

This is a short tutorial on how to compile ab yourself.

Stock ab in OS X Lion

The stock ab of OS X Lion has a bug that manifests itself by socket errors from the Apache Portable Runtime APR, such as apr_socket_recv: Connection reset by peer (54):

1
2
3
4
5
6
7
8
9
$ /usr/sbin/ab -n 10000 -c 1000 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)Send request failed!
Send request failed!
apr_socket_recv: Connection reset by peer (54)
Total of 2763 requests completed

Apparently, the same issue has also been observed on FreeBSD. The patch mentioned in this post has been merged into ab after the revision that ships with OS X Lion.

Latest Stable Release of ab

The latest stable version has a different bug. Ironically, you can read about the patch that introduces the bug in the official bug tracker.

Compiling the Subversion Trunk

Since the Subversion trunk does not come with a configure script, it is easier to download the latest stable release, httpd-2.4.2, and then patch the source code of ab, source/ab.c.

If you use Homebrew, you might use this Formula, as follows:

1
2
brew install 'https://raw.github.com/simonair/homebrew-dupes/e5177ef4fc82ae5246842e5a544124722c9e975b/ab.rb'
brew test ab

This formula also should appear in the homebrew-dupes tap soon.

While the httpd web server requires the PCRE library for regular expression parsing, ab does not use this. In other guides on how to compile ab, it is assumed that you have Homebrew and you are asked to install PCRE. However, this is simply a waste of time and disk space because PCRE will only be considered during the configure script.

It makes much more sense to simply remove the requirement for APR from the configure script. This is what is done in the above Homebrew formula.

How this can be accomplished without Homebrew is given in the following zsh shell script:

Apache Bench installation script (ab-2.4.2-install.zsh) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env zsh
set -ue

PKG=ab
VERSION=2.4.2
SRC_DIR=httpd-$VERSION

# Install prefix
PREFIX=/usr/local

# Package prefix (files will be symlinked to install prefix)
PKG_PREFIX=$PREFIX/pkg/$PKG-$VERSION

SUB_BIN=bin
SUB_MAN1=share/man/man1

# Create directories
sudo install -d $PKG_PREFIX/{$SUB_BIN,$SUB_MAN1}

WORK_DIR="$(mktemp -dt `basename $0`)" && cd $WORK_DIR

echo "\n### Compiling \`ab' in $WORK_DIR\n\n" >&2

curl http://www.apache.org/dist/httpd/$SRC_DIR.tar.bz2 | tar x
cd $SRC_DIR

# Patch ab.c to match latest SVN trunk version
PATCH='conditionally-bind-socket.diff'
curl 'http://svn.apache.org/viewvc/httpd/httpd/trunk/support/ab.c?r1=1351737&r2=1351736&pathrev=1351737&view=patch' -o $PATCH
patch -f -p3 -i $PATCH

# Patch `configure' script to do away with dependency on PCRE (`ab' does not use PCRE)
patch -p1 << 'EOP'
diff --git a/configure b/configure
index 5f4c09f..84d3de2 100755
--- a/configure
+++ b/configure
@@ -6037,8 +6037,6 @@ $as_echo "$as_me: Using external PCRE library from $PCRE_CONFIG" >&6;}
     done
   fi
 
-else
-  as_fn_error $? "pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/" "$LINENO" 5
 fi
 
   APACHE_VAR_SUBST="$APACHE_VAR_SUBST PCRE_LIBS"
EOP

# Avoid an error of the APR libtool on Mountain Lion
export LTFLAGS='--tag CC'
# Configure
./configure --silent --prefix="$PREFIX" --disable-debug --disable-dependency-tracking

# Change to support directory, where `ab' is located
cd support

# Compile ab
make -s ab

sudo install ab $PKG_PREFIX/$SUB_BIN
sudo install ../docs/man/ab.1 $PKG_PREFIX/$SUB_MAN1

cd $PKG_PREFIX
for d ($SUB_BIN $SUB_MAN1); do
    for f ($d/*); do
        sudo ln -sf $PKG_PREFIX/$f $PREFIX/$d
    done
done

echo "\n\n### Successfully installed \`ab' to $PKG_PREFIX and linked it into $PREFIX\n\n" >&2

Download the script, make it executable, and run it. It will ask you for your password in order to be able to install to /usr/local. If you would like to change the target directory, simply edit the definition of the PREFIX variable.

After the installation, you might have to rehash the path of your shell:

1
2
3
4
5
$  which ab
/usr/sbin/ab
$  hash -r
$  which ab
/usr/local/bin/ab

Run ab -V to check that the new version works:

1
2
3
4
$  ab -V
This is ApacheBench, Version 2.3 <$Revision: 1178079 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Comments