opticparse-python / benchmark_temp.py
Nanny7's picture
initial deploy
bcf46c3
Raw
History Blame Contribute Delete
6.33 kB
import urllib.request, json, time, uuid
PYTHON = 'https://opticparse-python-sg.onrender.com'
NODE = 'https://opticparse-1opticparse-node-sg.onrender.com'
SUPABASE_URL = 'https://xxmvhvxeglsjbewlouqg.supabase.co'
with open('.env') as f:
for line in f:
if line.startswith('SUPABASE_SERVICE_KEY='):
SERVICE_KEY = line.strip().split('=',1)[1]
# Create test user and get key
req = urllib.request.Request(
f'{SUPABASE_URL}/auth/v1/admin/users',
method='POST',
headers={'Content-Type':'application/json','apikey':SERVICE_KEY,'Authorization':f'Bearer {SERVICE_KEY}'},
data=json.dumps({'email':f'bench_{uuid.uuid4().hex[:6]}@test.com','password':'Test123!','email_confirm':True}).encode()
)
user_id = json.loads(urllib.request.urlopen(req).read())['id']
req2 = urllib.request.Request(
f'{PYTHON}/gateway/keys/generate',
method='POST',
headers={'Content-Type':'application/json'},
data=json.dumps({'user_id':user_id}).encode()
)
api_key = json.loads(urllib.request.urlopen(req2,timeout=30).read())['api_key']
print(f'API key: {api_key[:20]}...')
def scrape(url, query, schema, name):
start=time.time()
try:
req=urllib.request.Request(
f'{PYTHON}/api/vision-scrape',
method='POST',
headers={'Content-Type':'application/json','X-API-Key':api_key},
data=json.dumps({'target_url':url,'extraction_query':query,'response_schema':schema}).encode()
)
res=urllib.request.urlopen(req,timeout=90)
body=json.loads(res.read())
elapsed=time.time()-start
print(f'βœ… {name}: PASS ({elapsed:.1f}s)')
print(f' {json.dumps(body)[:150]}')
return elapsed, True
except Exception as e:
elapsed=time.time()-start
print(f'❌ {name}: FAIL ({elapsed:.1f}s) {str(e)[:80]}')
return elapsed, False
def phish(url, name, expected='safe'):
start=time.time()
try:
req=urllib.request.Request(
f'{NODE}/api/phish-detect',
method='POST',
headers={'Content-Type':'application/json'},
data=json.dumps({'url':url}).encode()
)
res=urllib.request.urlopen(req,timeout=90)
body=json.loads(res.read())
elapsed=time.time()-start
verdict=body.get('verdict','?')
confidence=body.get('confidence_score_percentage',0)
cached=body.get('cached',False)
correct=verdict==expected
print(f'{"βœ…" if correct else "❌"} {name}: {verdict} ({confidence}%) {"[CACHED]" if cached else "[FRESH]"} ({elapsed:.1f}s)')
return elapsed, correct
except Exception as e:
elapsed=time.time()-start
print(f'❌ {name}: FAIL ({elapsed:.1f}s) {str(e)[:60]}')
return elapsed, False
print()
print('=== OPTICPARSE BENCHMARK ===')
times, results = [], []
t,r = scrape('https://news.ycombinator.com','Top 5 posts with titles and points',{'posts':[{'title':'string','points':'number'}]},'HackerNews')
times.append(t); results.append(r)
t,r = scrape('https://github.com/trending','Top 3 trending repos with name and stars',{'repos':[{'name':'string','stars':'string'}]},'GitHub Trending')
times.append(t); results.append(r)
t,r = scrape('https://vercel.com/pricing','All plan names and prices',{'plans':[{'name':'string','price':'string'}]},'Vercel Pricing')
times.append(t); results.append(r)
t,r = scrape('https://www.cloudflare.com/plans/','Plan names and prices',{'plans':[{'name':'string','price':'string'}]},'Cloudflare (anti-bot test)')
times.append(t); results.append(r)
t,r = scrape('https://stripe.com/pricing','Product names and prices',{'products':[{'name':'string','price':'string'}]},'Stripe (heavy JS test)')
times.append(t); results.append(r)
# Cache test - same URL
t,r = scrape('https://news.ycombinator.com','Top 3 post titles',{'posts':[{'title':'string'}]},'HN Cache Test (should be fast)')
times.append(t); results.append(r)
passed = sum(results)
avg = sum(times)/len(times) if times else 0
print(f'OpticParse: {passed}/{len(results)} passed | Avg: {avg:.1f}s')
print(f'vs Apify: 8-15s avg | Ours: {avg:.1f}s')
print()
print('=== PHISHVISION BENCHMARK ===')
ptimes, presults = [], []
t,r = phish('https://google.com','Google (safe)','safe')
ptimes.append(t); presults.append(r)
t,r = phish('https://microsoft.com','Microsoft (safe)','safe')
ptimes.append(t); presults.append(r)
t,r = phish('https://accounts.google.com','Google Login (critical false-positive test)','safe')
ptimes.append(t); presults.append(r)
t,r = phish('https://github.com/login','GitHub Login (false-positive test)','safe')
ptimes.append(t); presults.append(r)
t,r = phish('https://apple.com','Apple (safe)','safe')
ptimes.append(t); presults.append(r)
t,r = phish('https://google.com','Google Cache Test (should be instant)','safe')
ptimes.append(t); presults.append(r)
ppassed = sum(presults)
pavg = sum(ptimes)/len(ptimes) if ptimes else 0
cached_times = [ptimes[i] for i in range(len(ptimes)) if ptimes[i] < 2]
fresh_times = [ptimes[i] for i in range(len(ptimes)) if ptimes[i] >= 2]
print(f'PhishVision: {ppassed}/{len(presults)} correct')
print(f'Fresh avg: {sum(fresh_times)/len(fresh_times):.1f}s' if fresh_times else 'No fresh scans')
print(f'Cached avg: {sum(cached_times)/len(cached_times):.2f}s' if cached_times else 'No cached hits')
print(f'vs Bolster.ai: 30-60s | Ours fresh: {sum(fresh_times)/len(fresh_times):.1f}s' if fresh_times else '')
print()
print('=== SECURITY TESTS ===')
# Fake key
try:
urllib.request.urlopen(urllib.request.Request(
f'{PYTHON}/api/vision-scrape',method='POST',
headers={'Content-Type':'application/json','X-API-Key':'fake-key-123'},
data=json.dumps({'target_url':'https://example.com','extraction_query':'test'}).encode()
),timeout=10)
print('❌ Fake key not rejected')
except urllib.error.HTTPError as e:
print(f'βœ… Fake key rejected ({e.code})')
# Internal URL
try:
urllib.request.urlopen(urllib.request.Request(
f'{NODE}/api/phish-detect',method='POST',
headers={'Content-Type':'application/json'},
data=json.dumps({'url':'http://127.0.0.1/secret'}).encode()
),timeout=10)
print('❌ Internal URL not blocked')
except urllib.error.HTTPError as e:
print(f'βœ… Internal URL blocked ({e.code})')