| 
 | 1 | +import http from 'k6/http';  | 
 | 2 | +import { Trend } from 'k6/metrics';  | 
 | 3 | +import { group, sleep } from 'k6';  | 
 | 4 | + | 
 | 5 | +const chatStreamLatency = new Trend('chat_stream_duration');  | 
 | 6 | +const chatNoStreamLatency = new Trend('chat_nostream_duration');  | 
 | 7 | + | 
 | 8 | +function between(min, max) {  | 
 | 9 | + min = Math.ceil(min);  | 
 | 10 | + max = Math.floor(max);  | 
 | 11 | + return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive  | 
 | 12 | +}  | 
 | 13 | + | 
 | 14 | +function choose(list) {  | 
 | 15 | + return list[between(0, list.length)];  | 
 | 16 | +}  | 
 | 17 | + | 
 | 18 | +export function chat(baseUrl, stream = true) {  | 
 | 19 | + group('Chat flow', function () {  | 
 | 20 | + const defaultPrompts = [  | 
 | 21 | + 'How to search and book rentals?',  | 
 | 22 | + 'What is the refund policy?',  | 
 | 23 | + 'How to contact a representative?',  | 
 | 24 | + ];  | 
 | 25 | + | 
 | 26 | + const payload = JSON.stringify({  | 
 | 27 | + messages: [{ content: choose(defaultPrompts), role: 'user' }],  | 
 | 28 | + context: {  | 
 | 29 | + retrieval_mode: 'hybrid',  | 
 | 30 | + semantic_ranker: true,  | 
 | 31 | + semantic_captions: false,  | 
 | 32 | + suggest_followup_questions: true,  | 
 | 33 | + retrievalMode: 'hybrid',  | 
 | 34 | + top: 3,  | 
 | 35 | + useSemanticRanker: true,  | 
 | 36 | + useSemanticCaptions: false,  | 
 | 37 | + excludeCategory: '',  | 
 | 38 | + promptTemplate: '',  | 
 | 39 | + promptTemplatePrefix: '',  | 
 | 40 | + promptTemplateSuffix: '',  | 
 | 41 | + suggestFollowupQuestions: true,  | 
 | 42 | + approach: 'rrr',  | 
 | 43 | + },  | 
 | 44 | + stream,  | 
 | 45 | + });  | 
 | 46 | + | 
 | 47 | + const parameters = {  | 
 | 48 | + headers: {  | 
 | 49 | + 'Content-Type': 'application/json',  | 
 | 50 | + },  | 
 | 51 | + tags: { type: 'API' },  | 
 | 52 | + };  | 
 | 53 | + | 
 | 54 | + const response = http.post(`${baseUrl}/chat`, payload, parameters);  | 
 | 55 | + | 
 | 56 | + if (response.status !== 200) {  | 
 | 57 | + console.log(`Response: ${response.status} ${response.body}`);  | 
 | 58 | + }  | 
 | 59 | + | 
 | 60 | + // add duration property to metric  | 
 | 61 | + const latencyMetric = stream ? chatStreamLatency : chatNoStreamLatency;  | 
 | 62 | + latencyMetric.add(response.timings.duration, { type: 'API' });  | 
 | 63 | + | 
 | 64 | + sleep(between(5, 20)); // wait between 5 and 20 seconds between each user iteration  | 
 | 65 | + });  | 
 | 66 | +}  | 
0 commit comments