File size: 7,080 Bytes
edcd2ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
252
253
254
#!/bin/bash
# Performance Verification Script
# Verifies SLA compliance: API latency <500ms, real-time updates <2s, throughput >100 req/sec
#

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Configuration
API_URL="${API_URL:-http://localhost:8000}"
DURATION="${DURATION:-60}" # seconds
CONCURRENCY="${CONCURRENCY:-10}"
THROUGHPUT_TARGET="${THROUGHPUT_TARGET:-100}"
LATENCY_P95_TARGET="${LATENCY_P95_TARGET:-500}"
REALTIME_TARGET="${REALTIME_TARGET:-2000}"

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Performance Verification${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}API URL: ${API_URL}${NC}"
echo -e "${YELLOW}Duration: ${DURATION}s${NC}"
echo -e "${YELLOW}Concurrency: ${CONCURRENCY}${NC}"
echo ""

FAILURES=0

# 1. API Latency Test (P95 < 500ms)
echo -e "${YELLOW}1. Testing API latency (target: P95 < ${LATENCY_P95_TARGET}ms)...${NC}"

if command -v wrk &> /dev/null; then
  RESULT=$(wrk -t${CONCURRENCY} -c${CONCURRENCY} -d${DURATION}s ${API_URL}/health 2>&1)
  LATENCY=$(echo "$RESULT" | grep "Latency" | awk '{print $2}')

  echo "$RESULT"

  if [ -n "$LATENCY" ]; then
    echo -e "${GREEN}βœ“ P95 Latency: ${LATENCY}${NC}"

    # Extract numeric value for comparison (assuming format like "123.45ms")
    LATENCY_VALUE=$(echo $LATENCY | sed 's/ms//')
    LATENCY_INT=${LATENCY_VALUE%.*}

    if [ "$LATENCY_INT" -lt "$LATENCY_P95_TARGET" ]; then
      echo -e "${GREEN}βœ“ Latency SLA met${NC}"
    else
      echo -e "${RED}βœ— Latency SLA not met${NC}"
      FAILURES=$((FAILURES + 1))
    fi
  else
    echo -e "${YELLOW}⚠ Could not parse latency${NC}"
  fi
else
  echo -e "${YELLOW}⚠ 'wrk' not installed - run: apt-get install wrk${NC}"
fi

# 2. Throughput Test (> 100 req/sec)
echo ""
echo -e "${YELLOW}2. Testing throughput (target: > ${THROUGHPUT_TARGET} req/sec)...${NC}"

if command -v wrk &> /dev/null; then
  RESULT=$(wrk -t${CONCURRENCY} -c${CONCURRENCY} -d${DURATION}s ${API_URL}/health 2>&1)
  REQ_PER_SEC=$(echo "$RESULT" | grep "Req/Sec" | awk '{print $1}')

  echo "$RESULT"

  if [ -n "$REQ_PER_SEC" ]; then
    echo -e "${GREEN}βœ“ Throughput: ${REQ_PER_SEC} req/sec${NC}"

    REQ_VALUE=${REQ_PER_SEC%.*}

    if [ "$REQ_VALUE" -gt "$THROUGHPUT_TARGET" ]; then
      echo -e "${GREEN}βœ“ Throughput SLA met${NC}"
    else
      echo -e "${RED}βœ— Throughput SLA not met${NC}"
      FAILURES=$((FAILURES + 1))
    fi
  else
    echo -e "${YELLOW}⚠ Could not parse throughput${NC}"
  fi
else
  echo -e "${YELLOW}⚠ 'wrk' not installed${NC}"
fi

# 3. Real-Time Updates Test (< 2 seconds)
echo ""
echo -e "${YELLOW}3. Testing real-time updates (target: < ${REALTIME_TARGET}ms)...${NC}"

if command -v python3 &> /dev/null; then
  # Create a Python script to test WebSocket latency
  cat > /tmp/test_websocket.py << 'EOF'
import asyncio
import websockets
import json
import time
from datetime import datetime

async def test_websocket_latency():
    uri = "ws://localhost:8000/ws?user_id=test-user-123"

    try:
        async with websockets.connect(uri) as websocket:
            # Send ping
            start_time = time.time()
            await websocket.send(json.dumps({
                "type": "ping",
                "timestamp": datetime.utcnow().isoformat()
            }))

            # Wait for pong
            response = await asyncio.wait_for(websocket.recv(), timeout=5)
            end_time = time.time()

            latency_ms = (end_time - start_time) * 1000
            print(f"WebSocket latency: {latency_ms:.2f}ms")

            if latency_ms < 2000:
                return 0  # Success
            else:
                return 1  # Failure
    except Exception as e:
        print(f"WebSocket connection failed: {e}")
        return 2  # Error

if __name__ == "__main__":
    exit(asyncio.run(test_websocket_latency()))
EOF

  if python3 -c "import websockets" 2>/dev/null; then
    python3 /tmp/test_websocket.py
    RESULT=$?

    if [ $RESULT -eq 0 ]; then
      echo -e "${GREEN}βœ“ Real-time updates SLA met${NC}"
    elif [ $RESULT -eq 1 ]; then
      echo -e "${RED}βœ— Real-time updates SLA not met${NC}"
      FAILURES=$((FAILURES + 1))
    else
      echo -e "${YELLOW}⚠ Could not connect to WebSocket${NC}"
    fi
  else
    echo -e "${YELLOW}⚠ 'websockets' not installed - run: pip install websockets${NC}"
  fi

  rm -f /tmp/test_websocket.py
else
  echo -e "${YELLOW}⚠ Python3 not found${NC}"
fi

# 4. Database Query Performance (P95 < 50ms)
echo ""
echo -e "${YELLOW}4. Testing database query performance...${NC}"

cd backend
if python3 -c "import sqlalchemy" 2>/dev/null; then
  RESULT=$(python3 -c "
from datetime import datetime
from time import perf_counter
from src.db.session import SessionLocal
from src.models.task import Task

db = SessionLocal()
start = perf_counter()
tasks = db.query(Task).limit(100).all()
end = perf_counter()

latency_ms = (end - start) * 1000
print(f'Database query latency: {latency_ms:.2f}ms')

if latency_ms < 50:
    exit(0)
else:
    exit(1)
" 2>&1)

  echo "$RESULT"

  if [ $? -eq 0 ]; then
    echo -e "${GREEN}βœ“ Database query performance SLA met${NC}"
  else
    echo -e "${RED}βœ— Database query performance SLA not met${NC}"
    FAILURES=$((FAILURES + 1))
  fi
else
  echo -e "${YELLOW}⚠ SQLAlchemy not installed${NC}"
fi
cd ..

# 5. Intent Detection Performance (< 500ms)
echo ""
echo -e "${YELLOW}5. Testing AI intent detection performance...${NC}"

cd backend
if python3 -c "from src.orchestrator.intent_detector import IntentDetector" 2>/dev/null; then
  RESULT=$(python3 -c "
from time import perf_counter
from src.orchestrator.intent_detector import IntentDetector

detector = IntentDetector()
test_input = 'Create a task to buy milk tomorrow at 5pm'

start = perf_counter()
intent, confidence = detector.detect(test_input)
end = perf_counter()

latency_ms = (end - start) * 1000
print(f'Intent detection latency: {latency_ms:.2f}ms')
print(f'Detected intent: {intent.value} (confidence: {confidence:.2f})')

if latency_ms < 500:
    exit(0)
else:
    exit(1)
" 2>&1)

  echo "$RESULT"

  if [ $? -eq 0 ]; then
    echo -e "${GREEN}βœ“ Intent detection SLA met${NC}"
  else
    echo -e "${RED}βœ— Intent detection SLA not met${NC}"
    FAILURES=$((FAILURES + 1))
  fi
else
  echo -e "${YELLOW}⚠ Intent detector not available${NC}"
fi
cd ..

# Summary
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Performance Test Summary${NC}"
echo -e "${GREEN}========================================${NC}"

if [ $FAILURES -eq 0 ]; then
  echo -e "${GREEN}βœ“ All performance SLAs met!${NC}"
  echo ""
  echo "Performance Results:"
  echo "  - API P95 Latency: < ${LATENCY_P95_TARGET}ms βœ“"
  echo "  - Throughput: > ${THROUGHPUT_TARGET} req/sec βœ“"
  echo "  - Real-time updates: < ${REALTIME_TARGET}ms βœ“"
  echo "  - Database queries: < 50ms βœ“"
  echo "  - Intent detection: < 500ms βœ“"
  exit 0
else
  echo -e "${RED}βœ— ${FAILURES} performance SLAs not met${NC}"
  exit 1
fi