-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_cache.py
More file actions
130 lines (108 loc) Β· 5.37 KB
/
Copy pathtest_cache.py
File metadata and controls
130 lines (108 loc) Β· 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""
Test script to verify PDF text extraction caching functionality
"""
import requests
import time
import json
BASE_URL = "http://localhost:8000/api/pdf"
def test_cache_functionality():
print("π§ͺ Testing PDF Text Extraction Caching")
print("=" * 50)
# Test 1: Check initial cache state
print("\n1. Checking initial cache state...")
response = requests.get(f"{BASE_URL}/cache/info")
if response.status_code == 200:
cache_info = response.json()
print(f" Cache directory: {cache_info['cache_directory']}")
print(f" Cached files: {cache_info['total_cached_files']}")
print(f" Cache size: {cache_info['total_cache_size_mb']} MB")
else:
print(f" β Failed to get cache info: {response.status_code}")
return
# Test 2: Select a PDF for the first time (should extract and cache)
print("\n2. Selecting PDF for the first time (cache miss)...")
pdf_filename = "Python_Basics.pdf" # Using a smaller PDF for faster testing
start_time = time.time()
response = requests.post(f"{BASE_URL}/select", json={"filename": pdf_filename})
first_extraction_time = time.time() - start_time
if response.status_code == 200:
result = response.json()
print(f" β
PDF selected successfully")
print(f" Filename: {result['filename']}")
print(f" Text length: {result['text_length']} characters")
print(f" Extraction time: {first_extraction_time:.3f} seconds")
else:
print(f" β Failed to select PDF: {response.status_code}")
print(f" Error: {response.text}")
return
# Test 3: Check cache state after first extraction
print("\n3. Checking cache state after first extraction...")
response = requests.get(f"{BASE_URL}/cache/info")
if response.status_code == 200:
cache_info = response.json()
print(f" Cached files: {cache_info['total_cached_files']}")
print(f" Cache size: {cache_info['total_cache_size_mb']} MB")
# Test 4: Select the same PDF again (should use cache)
print("\n4. Selecting the same PDF again (cache hit)...")
start_time = time.time()
response = requests.post(f"{BASE_URL}/select", json={"filename": pdf_filename})
second_extraction_time = time.time() - start_time
if response.status_code == 200:
result = response.json()
print(f" β
PDF selected successfully")
print(f" Filename: {result['filename']}")
print(f" Text length: {result['text_length']} characters")
print(f" Extraction time: {second_extraction_time:.3f} seconds")
# Calculate performance improvement
if first_extraction_time > 0:
improvement = ((first_extraction_time - second_extraction_time) / first_extraction_time) * 100
print(f" π Performance improvement: {improvement:.1f}%")
print(f" β‘ Speed up: {first_extraction_time / second_extraction_time:.1f}x faster")
else:
print(f" β Failed to select PDF: {response.status_code}")
print(f" Error: {response.text}")
# Test 5: Test with a larger PDF
print("\n5. Testing with a larger PDF...")
large_pdf_filename = "Ikigai - The Japanese secret to a long and happy life.pdf"
start_time = time.time()
response = requests.post(f"{BASE_URL}/select", json={"filename": large_pdf_filename})
large_pdf_time = time.time() - start_time
if response.status_code == 200:
result = response.json()
print(f" β
Large PDF selected successfully")
print(f" Filename: {result['filename']}")
print(f" Text length: {result['text_length']} characters")
print(f" Extraction time: {large_pdf_time:.3f} seconds")
else:
print(f" β Failed to select large PDF: {response.status_code}")
# Test 6: Select large PDF again (should use cache)
print("\n6. Selecting large PDF again (cache hit)...")
start_time = time.time()
response = requests.post(f"{BASE_URL}/select", json={"filename": large_pdf_filename})
large_pdf_cached_time = time.time() - start_time
if response.status_code == 200:
result = response.json()
print(f" β
Large PDF selected successfully from cache")
print(f" Extraction time: {large_pdf_cached_time:.3f} seconds")
if large_pdf_time > 0:
improvement = ((large_pdf_time - large_pdf_cached_time) / large_pdf_time) * 100
print(f" π Performance improvement: {improvement:.1f}%")
print(f" β‘ Speed up: {large_pdf_time / large_pdf_cached_time:.1f}x faster")
# Test 7: Final cache state
print("\n7. Final cache state...")
response = requests.get(f"{BASE_URL}/cache/info")
if response.status_code == 200:
cache_info = response.json()
print(f" Cached files: {cache_info['total_cached_files']}")
print(f" Cache size: {cache_info['total_cache_size_mb']} MB")
print("\n" + "=" * 50)
print("β
Cache testing completed!")
if __name__ == "__main__":
try:
test_cache_functionality()
except requests.exceptions.ConnectionError:
print("β Could not connect to the backend server.")
print(" Make sure the backend is running on http://localhost:8000")
except Exception as e:
print(f"β Test failed with error: {e}")