Module analysis

Analyses the url log file generated by the tests.

The tests produce a log of requested urls, with the information who requested the url, see Client.

This script is called by build.sh in the project's top-level directory, and produces a grand total of requests/urls fired by the client to the server.

Expand source code
"""Analyses the url log file generated by the tests.

The tests produce a log of requested urls, with the information
who requested the url, see `client.Client`.

This script is called by `build.sh` in the project's top-level directory,
and produces a grand total of requests/urls fired by the client to the server.
"""

import sys
import collections

URL_LOG = "tests/urllog.txt"

collected = collections.defaultdict(collections.Counter)

with open(URL_LOG) as fh:
    requests = 0
    for line in fh:
        requests += 1
        (user, url) = line.rstrip().split("\t")
        collected[url][user] += 1

    if requests == 0:
        msg = ""
    else:
        urls = len(collected)
        usersPerUrl = sum(len(x) for x in collected.values()) / urls
        requestsPerUrl = requests / urls
        msg = (
            f"""{"REQUESTS":>10} {"URLS":>10} {"USERS/URL":>10} {"REQ/URL":>10}\n"""
            f"""{requests:>10d} {urls:>10d} """
            f"""{usersPerUrl:>10.01f} {requestsPerUrl:>10.01f}\n"""
        )

sys.stdout.write(msg)
sys.stderr.write(msg)