Skip to content

Instantly share code, notes, and snippets.

@alanboy
Created July 27, 2024 21:12
Show Gist options
  • Select an option

  • Save alanboy/2c8d91160c35c122628ec74506ca6437 to your computer and use it in GitHub Desktop.

Select an option

Save alanboy/2c8d91160c35c122628ec74506ca6437 to your computer and use it in GitHub Desktop.
import pytest
import json
from fastapi.testclient import TestClient
import sys
from app import app
import time
client = TestClient(app)
def run_image_description(filename):
with open(filename, 'rb') as f:
img_data = f.read()
response = client.post('/description', files={'image': ('test.jpg', img_data)})
json = response.json()
assert response.status_code == 200
return json
def run_image_description_with_options(n_times, filename):
# load the file only once
with open(filename, 'rb') as f:
img_data = f.read()
start = time.time()
for i in range(0, n_times):
response = client.post('/description', files={'image': ('test.jpg', img_data)})
json = response.json()
assert response.status_code == 200
end = time.time()
return end - start
#
# Test cases
#
def test_same_image():
json = run_image_description('.devmatch/tree.jpeg')
assert json['oak_tree'] >= 50
def test_diff_images():
json = run_image_description('.devmatch/lenna.jpg')
assert json['woman'] >= 40
assert json['girl'] >= 10
json = run_image_description('.devmatch/cat.jpg')
assert json['tiger'] >= 30
json = run_image_description('.devmatch/tree.jpeg')
assert json['oak_tree'] >= 50
json = run_image_description('.devmatch/truck.jpg')
assert json['pickup_truck'] >= 50
# The cache test images are the same in size:
#
# Times with NO cache implemented
# 1 request : 1.94730520248413
# 5 requests : 9.22530078887939
# 10 requests : 19.94834017753601
#
# Times with cache implemented:
# 1 requests : 1.86988091468811
# 5 requests : 1.84185409545898
# 30 requests : 1.91821122169494
#
# our verification will be that both 5 and 10
# requests fall within 50% of the 1 request.
#
def test_cache():
# this means that running it more than once will be
# the same as 1x this percent tolerance
accepted_range = 0.5
# run it once
test_image = '.devmatch/cache_test_4.1.03.tiff'
one_time = run_image_description_with_options(1, test_image)
# run it 5 times should be roughly the same as 1x
test_image = '.devmatch/cache_test_4.1.02.tiff'
five_times = run_image_description_with_options(5, test_image)
five_times_within_range = abs(five_times - one_time) <= accepted_range * one_time;
assert five_times_within_range
# run it 10 times should be roughly the same as 1x
test_image = '.devmatch/cache_test_4.1.01.tiff'
ten_times = run_image_description_with_options(10, test_image)
ten_times_within_range = abs(ten_times - one_time) <= accepted_range * one_time;
assert ten_times_within_range
def test_cache_stats_api_exists():
response = client.get('/cache-stats')
json = response.json()
assert response.status_code == 200
return json
def test_cache_stats_api_works():
# get the baseline stats
baseline = client.get('/cache-stats').json()
print (baseline)
# run a new image
json = run_image_description('.devmatch/cache_api_test_4.1.05.tiff')
first_run = client.get('/cache-stats').json()
assert baseline['hits'] == first_run['hits']
assert baseline['images_in_cache'] + 1 == first_run['images_in_cache']
# run a new image
# json = run_image_description('.devmatch/cache_api_test_4.1.04.tiff')
# first_run = client.get('/cache-stats').json()
return json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment