Installing Varnish cache on Ubuntu 14.04 and measuring performance with ApacheBench

Today I’m doing an assignment for my school’s linux servers class held by Tero Karvinen. The assignment is to install Varnish cache and edit the settings of it, to measure site speed with ‘ab’ (ApacheBench) included in ‘apache2-utils’ package and to analyze and speed up a site with Firebug.

I will do this assignment with my virtual private server I made in an earlier post running an installation of Ubuntu 14.04 LTS that has Apache already installed. Using a late 2013 MacBook Pro as the terminal running OS X 10.11.

Installing Varnish

To install Varnish, I needed to add their own repository to my apt sources. [1]

curl https://repo.varnish-cache.org/GPG-key.txt | sudo apt-key add -
sudoedit /etc/apt/sources.list.d/varnish-cache.list

Pasted the following line to the new apt source file for varnish:

deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1

Then I could install the package:

sudo apt-get update
sudo apt-get -y install varnish

Configuring Varnish and Apache

For varnish to work, I need it to be visible to the internet. This means I need Varnish to listen on port 80, while Apache is running back on another port discussing with Varnish. [2]

sudoedit /etc/default/varnish

Here I scrolled to the bottom of the file and changed the ‘DAEMON_OPTS’ setting so it listens on port 80.

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Next up is Apache.

sudoedit /etc/apache2/ports.conf

I changed the first setting in file and changed it to listen on the port ‘8080’.

Listen 8080

Also, I need to change the VirtualHost configuration on the site.

sudoedit /etc/apache2/sites-available/metsanheimo.tk.conf

Right at the top, I changed the tag to also point to port ‘8080’.

<VirtualHost *:8080>

Now I needed to restart both daemons to read the new configuration.

sudo service apache2 restart
sudo service varnish restart

Leveraging browser cache for media files using VCL configuration

One extra setting I added for varnish to improve speed and SEO ranking is browser caching.

sudoedit /etc/varnish/default.vcl

I searched the ‘vcl_backend_response’ function from the file and added this content. [3]

sub vcl_backend_response {
    if (bereq.url ~ "\.(png|gif|jpg|swf|css|js)$") {
    unset beresp.http.set-cookie;
    set beresp.http.cache-control = "max-age = 2592000";
  }
}

And of course restarting the daemon afterwards.

sudo service varnish restart

Measuring performance

I will measure the performance of my WordPress test site on the virtual server with ApacheBench. To use the tool, I need apache utils installed:

sudo apt-get install apache2-utils

Before

This test I made before the installation of Varnish, this command sends out 500×100 (100 requests at a time for 500 times) requests to the web server located at metsanheimo.tk.

ab -n 500 -c 100 http://metsanheimo.tk/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking metsanheimo.tk (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests

Server Software:        Apache/2.4.7
Server Hostname:        metsanheimo.tk
Server Port:            80
Document Path:          /
Document Length:        14746 bytes
Concurrency Level:      100
Time taken for tests:   19.600 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      7499000 bytes
HTML transferred:       7373000 bytes
Requests per second:    25.51 [#/sec] (mean)
Time per request:       3919.957 [ms] (mean)
Time per request:       39.200 [ms] (mean, across all concurrent requests)
Transfer rate:          373.64 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   10  77.1      1     998
Processing:   635 3798 1345.9   3661    6433
Waiting:      544 2671 1038.4   2259    4823
Total:        643 3808 1344.4   3678    6433
Percentage of the requests served within a certain time (ms)
  50%   3678
  66%   4203
  75%   4516
  80%   5145
  90%   5879
  95%   6047
  98%   6229
  99%   6336
 100%   6433 (longest request)

Telling from the results above, the server took almost 20 seconds to respond to all the requests. Time per one request was almost 40ms.

After

ab -n 500 -c 100 http://metsanheimo.tk/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking metsanheimo.tk (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests
Server Software:        Apache/2.4.7
Server Hostname:        metsanheimo.tk
Server Port:            80
Document Path:          /
Document Length:        14746 bytes
Concurrency Level:      100
Time taken for tests:   0.768 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      7549104 bytes
HTML transferred:       7373000 bytes
Requests per second:    651.32 [#/sec] (mean)
Time per request:       153.534 [ms] (mean)
Time per request:       1.535 [ms] (mean, across all concurrent requests)
Transfer rate:          9603.32 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        8   36  15.1     37      77
Processing:    32   82  37.2     72     371
Waiting:       32   76  21.4     72     138
Total:         72  118  33.6    110     412
Percentage of the requests served within a certain time (ms)
  50%    110
  66%    115
  75%    123
  80%    127
  90%    138
  95%    146
  98%    268
  99%    318
 100%    412 (longest request)

With our glorious reverse proxy Varnish the response times crashed down. The first test took almost 20 seconds to complete, the test with Varnish took only 0,768 seconds and time per request was just 1.535ms compared to the 40ms before. Such grand results with just so simple installation. 

After the VCL browser cache leverage

After the small vcl browser cache leverage setting the same command gave these results.

ab -n 500 -c 100 http://metsanheimo.tk/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking metsanheimo.tk (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests

Server Software:        Apache/2.4.7
Server Hostname:        metsanheimo.tk
Server Port:            80
Document Path:          /
Document Length:        14746 bytes
Concurrency Level:      100
Time taken for tests:   0.629 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      7549138 bytes
HTML transferred:       7373000 bytes
Requests per second:    794.78 [#/sec] (mean)
Time per request:       125.821 [ms] (mean)
Time per request:       1.258 [ms] (mean, across all concurrent requests
Transfer rate:          11718.56 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        8   36  14.0     37      77
Processing:    34   75  15.6     74     131
Waiting:       34   74  15.5     73     120
Total:         65  111  10.6    112     142

Percentage of the requests served within a certain time (ms)
  50%    112
  66%    115
  75%    117
  80%    118
  90%    122
  95%    127
  98%    132
  99%    135
 100%    142 (longest request)

Again, we have a bit better results here. Time taken for the tests reduced by 0,139 seconds and time per request reduced by 0,277ms.

Examining site with Firebug

Here is a shot of what firebug shows about the caching of the site.

Firebug Net tab cache information

Firebug Net tab cache information

Here we can see that 843,5kb of the total page size of 848.4kb is coming from a cache. That is pretty a pretty good result. Only things that aren’t coming from the cache are two google fonts. They are also slowing down the load times a bit, because they need to be downloaded from another server. To fix this I could download the fonts locally.

Sources

Class by Tero Karvinen

[1] Installing Varnish

[2][3] Configuring Varnish and Apache

 

Miro Metsänheimo

A software developer from Finland born in -92. I'm passionate about computers and technology. Feel free to message me about anything!

 

Leave a Reply

Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin