ActiveState Code

Recipe 66440: Apache Client Cache-rate Calculator


A function to calculate the percent of requests that were "refused" by Apache server due to the clients ability to read the file from their cache instead.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def ClientCachePercentage(logfile_pathname):
        Contents = open(logfile_pathname, "r").readlines()
        TotalRequests = len(Contents)
        CachedRequests = 0

        for line in Contents:
                if line.split(" ")[8] == "304":  # if server returned "not modified"
                        CachedRequests += 1

        return TotalRequests / CachedRequests

# example usage
log_path = "/usr/local/nusphere/apache/logs/access_log"
print "Percentage of requests that are client-cached: " + str(ClientCachePercentage(log_path)) + "%"

Discussion

This can be useful in performance analysis. The percentage of requests for cached files versus non-cached files can have a great bearing on the perceived performance of your server.

Sign in to comment