Skip to content

Commit e7a7ffc

Browse files
committed
make k6 simulate actual back to back conversation. thus increasing context
1 parent 7ed830e commit e7a7ffc

File tree

1 file changed

+48
-53
lines changed

1 file changed

+48
-53
lines changed

scripts/k6_load_test.js

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* This script requires the use of k6 docker image version
2+
* This script requires the use of k6 docker image and it needs to be run inside the docker container. Check the command below
3+
* The k6 modules are imported from within the docker container. So it is not a dependency in Currechat
34
*
4-
* run using: docker run --rm -i grafana/k6 run - <script.js
5+
* run using: docker run --rm -i grafana/k6 run - <scripts/k6_load_test.js
56
*/
67

78
import http from 'k6/http'
@@ -10,39 +11,32 @@ import { sleep, check } from 'k6'
1011
const staging = 'http://gptwrapper.toska.svc.cluster.local:8000/api/ai/stream'
1112
const local = 'http://172.17.0.1:3000/api//ai/stream'
1213

13-
const vusit = 50
14+
const vusit = 1
1415

1516
export const options = {
1617
vus: vusit,
1718
iterations: vusit,
1819
// duration: "1s",
1920
}
2021

21-
const messages = [
22-
'listaa viisi numeroa',
23-
'listaa viisi väriä',
24-
'listaa viisi esinettä',
25-
'listaa viisi makua',
26-
'listaa viisi huonekalua',
27-
'listaa viisi presidenttiä',
28-
'listaa viisi maata',
29-
'listaa viisi ohjelmointikieltä',
30-
'listaa viisi kaupunkia',
31-
'listaa viisi vaatemerkkiä',
32-
'listaa viisi urheilulajia',
33-
]
22+
const headerParams = {
23+
headers: {
24+
'Content-Type': 'application/json',
25+
uid: 'testUser',
26+
27+
preferredlanguage: 'fi',
28+
hypersonsisuid: 'hy-hlo-123',
29+
hygroupcn: 'grp-toska;hy-employees;grp-currechat-demostudents;grp-currechat-demoteachers',
30+
},
31+
}
32+
33+
const messages = ['listaa viisi esinettä', 'kerro niistä jotain', 'anna esimerkki jokaisesta esineestä', 'kerro yksityiskohtaisemmin', 'tiivistä kertomasi']
3434

3535
const data = {
3636
options: {
37-
messages: [
38-
{ role: 'system', content: 'Olet avulias avustaja' },
39-
{
40-
role: 'user',
41-
content: undefined, // will be substituted from messages list
42-
},
43-
],
37+
messages: [],
4438
assistantInstructions: 'Olet avulias avustaja',
45-
model: 'mock',
39+
model: 'gpt-4o-mini',
4640
modelTemperature: 0.5,
4741
saveConsent: false,
4842
prevResponseId: '',
@@ -51,50 +45,51 @@ const data = {
5145
courseId: 'sandbox',
5246
}
5347

54-
function tokensToText(tokenizedStr) {
55-
return tokenizedStr
48+
function handleTokens(tokenizedStr) {
49+
// Collect all .text fields into an array
50+
let texts = []
51+
let responseId = ''
52+
53+
tokenizedStr
5654
.split('\n')
5755
.filter(Boolean)
58-
.map((line) => {
56+
.forEach((line) => {
5957
try {
60-
return JSON.parse(line).text
58+
const parsedLine = JSON.parse(line)
59+
if (parsedLine.text) texts.push(parsedLine.text)
60+
if (parsedLine.prevResponseId) responseId = parsedLine.prevResponseId
6161
} catch {
62-
return ''
62+
// ignore parse errors
6363
}
6464
})
65-
.join('')
65+
66+
const response = texts
67+
.join('') // join all text fragments
68+
.replace(/\n+/g, ' ')
69+
.replace(/\s{2,}/g, ' ')
70+
.trim()
71+
72+
return { response, responseId }
6673
}
6774

6875
export default function () {
6976
const url = `${staging}/v2`
77+
let prevResponseId = ''
7078

7179
for (const message of messages) {
72-
const dataCopy = { ...data }
73-
dataCopy.options.messages = [
74-
{ role: 'system', content: 'Olet load testissä' },
75-
{
76-
role: 'user',
77-
content: message,
78-
},
79-
]
80-
const updatedData = dataCopy
80+
const newData = { ...data }
81+
newData.options.messages = [{ role: 'user', content: message }]
82+
newData.options.prevResponseId = prevResponseId
8183

8284
const payload = JSON.stringify({
83-
data: JSON.stringify(updatedData),
85+
data: JSON.stringify(newData),
8486
})
85-
const params = {
86-
headers: {
87-
'Content-Type': 'application/json',
88-
uid: 'testUser',
89-
90-
preferredlanguage: 'fi',
91-
hypersonsisuid: 'hy-hlo-123',
92-
hygroupcn: 'grp-toska;hy-employees;grp-currechat-demostudents;grp-currechat-demoteachers',
93-
},
94-
}
95-
96-
let res = http.post(url, payload, params)
97-
console.log('📌', tokensToText(res.body))
87+
88+
let res = http.post(url, payload, headerParams)
89+
const { response, responseId } = handleTokens(res.body)
90+
prevResponseId = responseId
91+
92+
console.log('📌 response:', response)
9893
check(res, { 'status is 200': (res) => res.status === 200 })
9994
sleep(5)
10095
}

0 commit comments

Comments
 (0)