|
| 1 | +import { check, sleep } from "k6"; |
| 2 | +import http from "k6/http"; |
| 3 | +import { Counter, Trend } from "k6/metrics"; |
| 4 | + |
| 5 | +const completionRequests = new Counter("completion_requests"); |
| 6 | +const completionSuccess = new Counter("completion_success"); |
| 7 | +const completionLatency = new Trend("completion_latency"); |
| 8 | +const streamingTTFB = new Trend("streaming_ttfb"); |
| 9 | + |
| 10 | +export const options = { |
| 11 | + vus: 5, |
| 12 | + duration: "30s", |
| 13 | + thresholds: { |
| 14 | + completion_success: ["count > 0"], |
| 15 | + checks: ["rate > 0.9"], |
| 16 | + completion_latency: ["p(95) < 30000"], |
| 17 | + }, |
| 18 | +}; |
| 19 | + |
| 20 | +const API_URL = __ENV.API_URL || "http://localhost:4000"; |
| 21 | +const AUTH_TOKEN = __ENV.AUTH_TOKEN || ""; |
| 22 | +const STREAMING = __ENV.STREAMING === "true"; |
| 23 | + |
| 24 | +const PROMPTS = { |
| 25 | + short: "What is 2+2?", |
| 26 | + medium: |
| 27 | + "Explain the concept of recursion in programming. Include a simple example and mention common use cases.", |
| 28 | + long: `You are a helpful assistant analyzing a meeting transcript. Here is the context: |
| 29 | +
|
| 30 | +The team discussed the following topics: |
| 31 | +1. Q3 roadmap planning - focusing on improving user onboarding experience |
| 32 | +2. Technical debt reduction - specifically around the authentication system |
| 33 | +3. New feature requests from enterprise customers |
| 34 | +4. Performance optimization for the real-time collaboration features |
| 35 | +5. Integration with third-party calendar services |
| 36 | +
|
| 37 | +Based on this context, please provide: |
| 38 | +- A summary of the key discussion points |
| 39 | +- Action items that should be tracked |
| 40 | +- Any decisions that were made |
| 41 | +- Recommended follow-up topics for the next meeting |
| 42 | +
|
| 43 | +Keep your response concise but comprehensive.`, |
| 44 | +}; |
| 45 | + |
| 46 | +const PROMPT_KEYS = Object.keys(PROMPTS); |
| 47 | + |
| 48 | +function getRandomPrompt() { |
| 49 | + const key = PROMPT_KEYS[Math.floor(Math.random() * PROMPT_KEYS.length)]; |
| 50 | + return { type: key, content: PROMPTS[key] }; |
| 51 | +} |
| 52 | + |
| 53 | +function makeRequest(prompt, stream) { |
| 54 | + const url = `${API_URL}/chat/completions`; |
| 55 | + const payload = JSON.stringify({ |
| 56 | + messages: [ |
| 57 | + { role: "system", content: "You are a helpful assistant." }, |
| 58 | + { role: "user", content: prompt.content }, |
| 59 | + ], |
| 60 | + stream, |
| 61 | + max_tokens: 256, |
| 62 | + }); |
| 63 | + |
| 64 | + const params = { |
| 65 | + headers: { |
| 66 | + "Content-Type": "application/json", |
| 67 | + ...(AUTH_TOKEN ? { Authorization: `Bearer ${AUTH_TOKEN}` } : {}), |
| 68 | + }, |
| 69 | + timeout: "60s", |
| 70 | + }; |
| 71 | + |
| 72 | + const startTime = Date.now(); |
| 73 | + const res = http.post(url, payload, params); |
| 74 | + const latency = Date.now() - startTime; |
| 75 | + |
| 76 | + return { res, latency, promptType: prompt.type }; |
| 77 | +} |
| 78 | + |
| 79 | +export default function () { |
| 80 | + const prompt = getRandomPrompt(); |
| 81 | + completionRequests.add(1); |
| 82 | + |
| 83 | + const { res, latency, promptType } = makeRequest(prompt, STREAMING); |
| 84 | + |
| 85 | + const isSuccess = check(res, { |
| 86 | + "status is 200": (r) => r.status === 200, |
| 87 | + "has response body": (r) => r.body && r.body.length > 0, |
| 88 | + }); |
| 89 | + |
| 90 | + if (isSuccess) { |
| 91 | + completionSuccess.add(1); |
| 92 | + completionLatency.add(latency); |
| 93 | + |
| 94 | + if (STREAMING) { |
| 95 | + streamingTTFB.add(res.timings.waiting); |
| 96 | + } |
| 97 | + |
| 98 | + console.log( |
| 99 | + `[${promptType}] ${STREAMING ? "stream" : "sync"} completed in ${latency}ms`, |
| 100 | + ); |
| 101 | + } else { |
| 102 | + console.log( |
| 103 | + `[${promptType}] failed with status ${res.status}: ${res.body}`, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + sleep(1); |
| 108 | +} |
0 commit comments