File size: 6,330 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
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})')