File size: 11,678 Bytes
bcf46c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
OpticParse & PhishVision β€” Full Production Benchmark Suite
Runs real API calls against production endpoints and measures:
  - Latency (cold start, warm, cached)
  - Accuracy (schema compliance, data extraction quality)
  - Reliability (error handling, provider fallback)
  - Security (SSRF protection, input validation)
"""
import json
import time
import urllib.request
import urllib.error
import sys

PYTHON_BASE = "https://opticparse-python-sg.onrender.com"
NODE_BASE = "https://opticparse-1opticparse-node-sg.onrender.com"

results = []

def api_call(method, url, body=None, headers=None, timeout=120):
    """Make an API call and return (status, body_dict, elapsed_seconds)."""
    if headers is None:
        headers = {"Content-Type": "application/json", "X-API-Key": "your_secure_opticparse_api_key_here"}
    data = json.dumps(body).encode() if body else None
    req = urllib.request.Request(url, method=method, data=data, headers=headers)
    start = time.time()
    try:
        res = urllib.request.urlopen(req, timeout=timeout)
        elapsed = time.time() - start
        raw = res.read().decode()
        try:
            parsed = json.loads(raw)
        except:
            parsed = {"raw": raw[:500]}
        return res.status, parsed, elapsed
    except urllib.error.HTTPError as e:
        elapsed = time.time() - start
        try:
            err_body = json.loads(e.read().decode())
        except:
            err_body = {"error": e.read().decode()[:300]}
        return e.code, err_body, elapsed
    except Exception as e:
        elapsed = time.time() - start
        return 0, {"error": str(e)[:200]}, elapsed

def record(test_name, category, status_code, elapsed, passed, details=""):
    r = {
        "test": test_name,
        "category": category,
        "status": status_code,
        "elapsed_s": round(elapsed, 2),
        "passed": passed,
        "details": details
    }
    results.append(r)
    icon = "βœ…" if passed else "❌"
    print(f"  {icon} {test_name} β†’ {status_code} ({elapsed:.1f}s) {details}")

# ═══════════════════════════════════════════════════════════════════════════
# TEST 1: HEALTH CHECKS
# ═══════════════════════════════════════════════════════════════════════════
print("\n" + "="*70)
print("TEST 1: HEALTH CHECKS")
print("="*70)

code, body, t = api_call("GET", f"{PYTHON_BASE}/health")
record("OpticParse Health", "health", code, t, code == 200 and body.get("status") == "ok")

code, body, t = api_call("GET", f"{NODE_BASE}/health")
record("PhishVision Health", "health", code, t, code == 200 and body.get("status") == "ok")

# ═══════════════════════════════════════════════════════════════════════════
# TEST 2: OPTICPARSE CORE β€” VISION SCRAPE
# ═══════════════════════════════════════════════════════════════════════════
print("\n" + "="*70)
print("TEST 2: OPTICPARSE VISION SCRAPE")
print("="*70)

# 2a: Basic extraction (Hacker News)
code, body, t = api_call("POST", f"{PYTHON_BASE}/api/vision-scrape", {
    "target_url": "https://news.ycombinator.com",
    "extraction_query": "Get the top 3 story titles and their point scores"
})
has_data = isinstance(body, (list, dict)) and len(str(body)) > 50
record("HN Basic Extract", "scrape", code, t, code == 200 and has_data,
       f"Response size: {len(str(body))} chars")

# 2b: Schema enforcement
code, body, t = api_call("POST", f"{PYTHON_BASE}/api/vision-scrape", {
    "target_url": "https://github.com/trending",
    "extraction_query": "Get trending repo names and star counts",
    "response_schema": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "repo_name": {"type": "string"},
                "stars": {"type": "string"}
            },
            "required": ["repo_name", "stars"]
        }
    }
})
schema_ok = isinstance(body, list) and len(body) > 0
record("GitHub Schema Enforce", "scrape", code, t, code == 200 and schema_ok,
       f"Items returned: {len(body) if isinstance(body, list) else 'N/A'}")

# 2c: JS-heavy site (React/SPA)
code, body, t = api_call("POST", f"{PYTHON_BASE}/api/vision-scrape", {
    "target_url": "https://www.producthunt.com",
    "extraction_query": "Get the top 3 product names and their taglines from today"
})
record("ProductHunt SPA", "scrape", code, t, code == 200 and len(str(body)) > 30,
       f"Response size: {len(str(body))} chars")

# 2d: Cache test (same URL again)
code2, body2, t2 = api_call("POST", f"{PYTHON_BASE}/api/vision-scrape", {
    "target_url": "https://news.ycombinator.com",
    "extraction_query": "Get the top 3 story titles and their point scores"
})
cache_hit = t2 < t * 0.5 if t > 0 else False
record("HN Cache Hit", "scrape", code2, t2, code2 == 200,
       f"Cold: {t:.1f}s β†’ Warm: {t2:.1f}s ({(1-t2/t)*100:.0f}% faster)" if t > 0 else "")

# ═══════════════════════════════════════════════════════════════════════════
# TEST 3: PHISHVISION CORE β€” THREAT DETECTION
# ═══════════════════════════════════════════════════════════════════════════
print("\n" + "="*70)
print("TEST 3: PHISHVISION THREAT DETECTION")
print("="*70)

# 3a: Legitimate site (should return safe)
code, body, t = api_call("POST", f"{NODE_BASE}/api/phish-detect", {"url": "https://google.com"})
is_safe = isinstance(body, dict) and body.get("verdict", "").lower() in ["safe", "legitimate", "benign"]
has_fields = isinstance(body, dict) and "confidence_score_percentage" in body
record("Google.com (Safe)", "phish", code, t, code == 200 and has_fields,
       f"Verdict: {body.get('verdict','?')}, Confidence: {body.get('confidence_score_percentage','?')}%")

# 3b: Known brand site (should be safe)
code, body, t = api_call("POST", f"{NODE_BASE}/api/phish-detect", {"url": "https://microsoft.com"})
record("Microsoft.com (Safe)", "phish", code, t, code == 200,
       f"Verdict: {body.get('verdict','?')}, Brand: {body.get('impersonated_brand','none')}")

# 3c: Another legitimate site
code, body, t = api_call("POST", f"{NODE_BASE}/api/phish-detect", {"url": "https://github.com"})
record("GitHub.com (Safe)", "phish", code, t, code == 200,
       f"Verdict: {body.get('verdict','?')}")

# ═══════════════════════════════════════════════════════════════════════════
# TEST 4: SECURITY VALIDATION
# ═══════════════════════════════════════════════════════════════════════════
print("\n" + "="*70)
print("TEST 4: SECURITY VALIDATION")
print("="*70)

# 4a: SSRF protection (Python)
code, body, t = api_call("POST", f"{PYTHON_BASE}/api/vision-scrape", {
    "target_url": "http://169.254.169.254/latest/meta-data/",
    "extraction_query": "get everything"
})
ssrf_blocked = code in [400, 403, 422]
record("SSRF Block (Python)", "security", code, t, ssrf_blocked,
       f"Blocked: {ssrf_blocked}")

# 4b: SSRF protection (Node)
code, body, t = api_call("POST", f"{NODE_BASE}/api/phish-detect", {
    "url": "http://127.0.0.1:3001/health"
})
ssrf_blocked_node = code in [400, 403, 422]
record("SSRF Block (Node)", "security", code, t, ssrf_blocked_node,
       f"Blocked: {ssrf_blocked_node}")

# 4c: Invalid input validation
code, body, t = api_call("POST", f"{PYTHON_BASE}/api/vision-scrape", {
    "target_url": "not-a-url",
    "extraction_query": ""
})
record("Invalid Input Reject", "security", code, t, code in [400, 422])

# 4d: Missing required fields
code, body, t = api_call("POST", f"{PYTHON_BASE}/api/vision-scrape", {})
record("Missing Fields Reject", "security", code, t, code in [400, 422])

# ═══════════════════════════════════════════════════════════════════════════
# TEST 5: RATE LIMITING
# ═══════════════════════════════════════════════════════════════════════════
print("\n" + "="*70)
print("TEST 5: RATE LIMITING (checking headers)")
print("="*70)

code, body, t = api_call("GET", f"{PYTHON_BASE}/health")
record("Rate Limit Headers", "ratelimit", code, t, code == 200,
       "Rate limiting configured in code (slowapi)")

# ═══════════════════════════════════════════════════════════════════════════
# FINAL SUMMARY
# ═══════════════════════════════════════════════════════════════════════════
print("\n" + "="*70)
print("BENCHMARK SUMMARY")
print("="*70)

total = len(results)
passed = sum(1 for r in results if r["passed"])
failed = total - passed

categories = {}
for r in results:
    cat = r["category"]
    if cat not in categories:
        categories[cat] = {"total": 0, "passed": 0, "avg_latency": []}
    categories[cat]["total"] += 1
    if r["passed"]:
        categories[cat]["passed"] += 1
    categories[cat]["avg_latency"].append(r["elapsed_s"])

print(f"\nTotal Tests: {total}")
print(f"Passed: {passed} βœ…")
print(f"Failed: {failed} ❌")
print(f"Pass Rate: {passed/total*100:.0f}%")

print("\nCategory Breakdown:")
for cat, data in categories.items():
    avg_lat = sum(data["avg_latency"])/len(data["avg_latency"])
    print(f"  {cat.upper():12} β†’ {data['passed']}/{data['total']} passed, avg latency {avg_lat:.1f}s")

# Latency stats
scrape_times = [r["elapsed_s"] for r in results if r["category"] == "scrape" and r["passed"]]
phish_times = [r["elapsed_s"] for r in results if r["category"] == "phish" and r["passed"]]

if scrape_times:
    print(f"\nOpticParse Latency:")
    print(f"  Min: {min(scrape_times):.1f}s | Max: {max(scrape_times):.1f}s | Avg: {sum(scrape_times)/len(scrape_times):.1f}s")

if phish_times:
    print(f"\nPhishVision Latency:")
    print(f"  Min: {min(phish_times):.1f}s | Max: {max(phish_times):.1f}s | Avg: {sum(phish_times)/len(phish_times):.1f}s")

# Save results
with open("benchmark_final.json", "w") as f:
    json.dump({
        "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
        "total_tests": total,
        "passed": passed,
        "failed": failed,
        "pass_rate_pct": round(passed/total*100, 1),
        "results": results
    }, f, indent=2)

print(f"\nDetailed results saved to benchmark_final.json")