File size: 5,798 Bytes
ed537ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Sentio API load test — Locust

Usage:
    cd sentio-api
    export SENTIO_TOKEN="<supabase-jwt-for-test-user>"
    locust -f scripts/locustfile.py --host https://<your-hf-space>.hf.space \
           --users 25 --spawn-rate 5 --run-time 60s --headless \
           --html scripts/locust_report_25.html
    # Repeat with --users 50 for the 50-user run.

Required env vars:
    SENTIO_TOKEN   Supabase JWT for the test user (create a dedicated load-test account)

Metrics captured:
    P50 and P95 response time for:
      - POST /journal          (entry creation, returns 201 immediately)
      - GET  /journal          (list entries, paginated)
      - POST /ai/chat          (SSE stream, consumed to completion)
      - GET  /journal/themes   (aggregate query)

The SSE chat task is lower-weight (1 vs 3) because each stream holds a connection
for ~3–10 s.  Adjust weights if the HF Space concurrency limit is < 10.
"""
import os
import uuid
from locust import HttpUser, task, between, events

_TOKEN = os.environ.get("SENTIO_TOKEN", "")
_HEADERS = {"Authorization": f"Bearer {_TOKEN}", "Content-Type": "application/json"}

# Sample journal texts for POST /journal — varied enough to exercise the classifier
_JOURNAL_TEXTS = [
    "I knew the project would fail from the start. Everyone disagreed but I was right.",
    "My team keeps making the same mistakes. I never do things that poorly.",
    "I only remember the times things went wrong this week. Everything feels hopeless.",
    "The first estimate I heard for the deadline stuck with me even though it was wrong.",
    "Everyone at the meetup seemed to agree with the new framework, so I adopted it too.",
    "I spent three months on this feature. Even though requirements changed, we should ship it.",
    "My colleague seemed confident so I assumed his numbers were correct without checking.",
    "I feel overwhelmed but I think I handled today's presentation pretty well overall.",
    "Reflecting on the week: I learned that slowing down often speeds up the outcome.",
    "I caught myself assuming the worst before the meeting even started. Trying to reframe.",
]

_CHAT_MESSAGES = [
    "What is confirmation bias and how can I overcome it?",
    "I've been feeling anxious about decisions lately. Any advice?",
    "How does journaling help with self-awareness?",
    "Can you explain cognitive distortions in simple terms?",
    "What strategies help with anchoring bias in negotiations?",
]


class SentioUser(HttpUser):
    """Simulates a typical Sentio user: journal + chat mix."""

    wait_time = between(1, 3)

    def on_start(self) -> None:
        self._entry_ids: list[str] = []
        self._session_id = str(uuid.uuid4())

    @task(3)
    def create_journal_entry(self) -> None:
        text = _JOURNAL_TEXTS[hash(str(uuid.uuid4())) % len(_JOURNAL_TEXTS)]
        with self.client.post(
            "/journal/",
            json={"content": text},
            headers=_HEADERS,
            catch_response=True,
            name="POST /journal",
        ) as resp:
            if resp.status_code == 201:
                data = resp.json()
                if data.get("id"):
                    self._entry_ids.append(data["id"])
                resp.success()
            elif resp.status_code == 200 and resp.json().get("crisis_resources"):
                resp.success()  # safety gate fired — not an error
            else:
                resp.failure(f"Unexpected status {resp.status_code}")

    @task(3)
    def list_journal_entries(self) -> None:
        with self.client.get(
            "/journal/?limit=20&offset=0",
            headers=_HEADERS,
            catch_response=True,
            name="GET /journal",
        ) as resp:
            if resp.status_code == 200:
                resp.success()
            else:
                resp.failure(f"Unexpected status {resp.status_code}")

    @task(2)
    def get_journal_themes(self) -> None:
        with self.client.get(
            "/journal/themes",
            headers=_HEADERS,
            catch_response=True,
            name="GET /journal/themes",
        ) as resp:
            if resp.status_code == 200:
                resp.success()
            else:
                resp.failure(f"Unexpected status {resp.status_code}")

    @task(1)
    def chat_stream(self) -> None:
        """POST /ai/chat — SSE stream.  Consume all chunks, then close."""
        msg = _CHAT_MESSAGES[hash(str(uuid.uuid4())) % len(_CHAT_MESSAGES)]
        # Use a stable session UUID for the whole user session (Option B)
        with self.client.post(
            "/ai/chat",
            json={"message": msg, "conversation_id": self._session_id},
            headers=_HEADERS,
            stream=True,
            catch_response=True,
            name="POST /ai/chat (SSE)",
            timeout=30,
        ) as resp:
            if resp.status_code not in (200, 201):
                resp.failure(f"Unexpected status {resp.status_code}")
                return
            consumed = 0
            for chunk in resp.iter_content(chunk_size=512):
                consumed += len(chunk)
                if consumed > 32_768:  # 32 KB cap per stream
                    break
            resp.success()


@events.quitting.add_listener
def on_quit(environment, **_kwargs) -> None:
    """Print P50/P95 summary to stdout when the test ends."""
    stats = environment.stats
    print("\n=== Sentio Load Test Results ===")
    for name, entry in stats.entries.items():
        p50 = entry.get_response_time_percentile(0.50)
        p95 = entry.get_response_time_percentile(0.95)
        print(f"  {name[1]:40s}  P50={p50:6.0f}ms  P95={p95:6.0f}ms  RPS={entry.current_rps:.1f}  err={entry.num_failures}")
    print("================================\n")