Welcome, guest | Sign In | My Account | Store | Cart

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, 14 lines
 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)) + "%"

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.

Created by Mark Nenadov on Fri, 27 Jul 2001 (PSF)
Python recipes (4591)
Mark Nenadov's recipes (12)

Required Modules

  • (none specified)

Other Information and Tasks