~panda-roux/MoonGem

a4ed647ebba6359cf5082866996636ceb78f0fa3 — panda-roux 5 months ago 9573435
adding benchmarks
3 files changed, 39 insertions(+), 2 deletions(-)

A test/root/has_script.gmi
M test/test.py
M test/test_definitions.py
A test/root/has_script.gmi => test/root/has_script.gmi +5 -0
@@ 0,0 1,5 @@
This page has a script.

{{ return "hello world" }}

Here's the end of the page.

M test/test.py => test/test.py +33 -1
@@ 1,13 1,39 @@
from statistics import mean
import test_definitions as td
import ignition as ig
import sys
import time

tests = {
    'empty.gmi':      td.exists,
    'does_not_exist.gmi': td.does_not_exist
}

def main():
def bench_route(route):
    bench_times = list()
    last_time = time.perf_counter_ns()
    for i in range(0, 100):
        response = ig.request(f'//localhost:1966/{route}')
        current_time = time.perf_counter_ns()
        bench_times.append(current_time - last_time)
        last_time = current_time
    return bench_times

def print_benchmarks(name, times: list[int]):
    first = times[0] / 1000000.0
    minimum = min(times) / 1000000.0
    maximum = max(times) / 1000000.0
    average = mean(times) / 1000000.0
    print(f'{name} - first: {first}ms | min: {minimum}ms | max: {maximum}ms | mean: {average}ms')

def run_benchmarks():
    print('Running benchmarks...')
    print_benchmarks('Script', bench_route('has_script.gmi'))
    print_benchmarks('Empty', bench_route('empty.gmi'))


def run_integration():
    print('Running integration tests...')
    for route, func in tests.items():
        response = ig.request(f'//localhost:1966/{route}')
        passed, msg = func(response)


@@ 17,5 43,11 @@ def main():
        else:
            print(f'{func.__name__} passed ({route})')


def main():
    run_integration()
    run_benchmarks()

        
if __name__ == '__main__':
    main()

M test/test_definitions.py => test/test_definitions.py +1 -1
@@ 14,6 14,6 @@ def exists(response: BaseResponse):

def does_not_exist(response: BaseResponse):
    if response.status != ig.RESPONSE_STATUSDETAIL_PERM_FAILURE_NOT_FOUND:
        return fail('Resource was found')
        return fail(f'Response was {response.data()}')
    return ok()