Skip to content

Commit 3d85077

Browse files
committed
add scenario 9
1 parent 0e12ce4 commit 3d85077

2 files changed

Lines changed: 382 additions & 0 deletions

File tree

Lines changed: 373 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,373 @@
1+
import encoding from 'k6/encoding';
2+
import { check } from 'k6';
3+
import http from 'k6/http';
4+
5+
const baseUrl = __ENV.OPERATOR_URL;
6+
const clientSecret = __ENV.CLIENT_SECRET;
7+
const clientKey = __ENV.CLIENT_KEY;
8+
9+
const generateRPS = 25000;
10+
const refreshRPS = 25000;
11+
12+
// Low RPS for identity map to mimic real prod traffic.
13+
// DIIs are generated fresh on every request (no 45s caching) so each call
14+
// exercises a unique set of identifiers.
15+
const identityMapRPS = 3;
16+
17+
const warmUpTime = '10m'
18+
const testDuration = '20m'
19+
20+
export const options = {
21+
insecureSkipTLSVerify: true,
22+
noConnectionReuse: false,
23+
scenarios: {
24+
// Warmup scenarios
25+
tokenGenerateWarmup: {
26+
executor: 'ramping-arrival-rate',
27+
exec: 'tokenGenerate',
28+
timeUnit: '1s',
29+
preAllocatedVUs: 200,
30+
maxVUs: 400,
31+
stages: [
32+
{ duration: warmUpTime, target: generateRPS}
33+
],
34+
},
35+
tokenRefreshWarmup: {
36+
executor: 'ramping-arrival-rate',
37+
exec: 'tokenRefresh',
38+
timeUnit: '1s',
39+
preAllocatedVUs: 200,
40+
maxVUs: 400,
41+
stages: [
42+
{ duration: warmUpTime, target: refreshRPS}
43+
],
44+
},
45+
identityMapWarmup: {
46+
executor: 'ramping-arrival-rate',
47+
exec: 'identityMap',
48+
timeUnit: '1s',
49+
preAllocatedVUs: 5,
50+
maxVUs: 10,
51+
stages: [
52+
{ duration: warmUpTime, target: identityMapRPS}
53+
],
54+
},
55+
// Actual testing scenarios
56+
tokenGenerate: {
57+
executor: 'constant-arrival-rate',
58+
exec: 'tokenGenerate',
59+
rate: generateRPS,
60+
timeUnit: '1s',
61+
preAllocatedVUs: 200,
62+
maxVUs: 400,
63+
duration: testDuration,
64+
gracefulStop: '0s',
65+
startTime: warmUpTime,
66+
},
67+
tokenRefresh: {
68+
executor: 'constant-arrival-rate',
69+
exec: 'tokenRefresh',
70+
rate: refreshRPS,
71+
timeUnit: '1s',
72+
preAllocatedVUs: 200,
73+
maxVUs: 400,
74+
duration: testDuration,
75+
gracefulStop: '0s',
76+
startTime: warmUpTime,
77+
},
78+
identityMap: {
79+
executor: 'constant-arrival-rate',
80+
exec: 'identityMap',
81+
rate: identityMapRPS,
82+
timeUnit: '1s',
83+
preAllocatedVUs: 5,
84+
maxVUs: 10,
85+
duration: testDuration,
86+
gracefulStop: '0s',
87+
startTime: warmUpTime,
88+
},
89+
},
90+
// So we get count in the summary, to demonstrate different metrics are different
91+
summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'],
92+
thresholds: {
93+
// Intentionally empty. We'll programatically define our bogus
94+
// thresholds (to generate the sub-metrics) below. In your real-world
95+
// load test, you can add any real threshoulds you want here.
96+
}
97+
};
98+
99+
// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3
100+
for (let key in options.scenarios) {
101+
// Each scenario automaticall tags the metrics it generates with its own name
102+
let thresholdName = `http_req_duration{scenario:${key}}`;
103+
// Check to prevent us from overwriting a threshold that already exists
104+
if (!options.thresholds[thresholdName]) {
105+
options.thresholds[thresholdName] = [];
106+
}
107+
// 'max>=0' is a bogus condition that will always be fulfilled
108+
options.thresholds[thresholdName].push('max>=0');
109+
}
110+
111+
export async function setup() {
112+
var token = await generateRefreshRequest();
113+
return {
114+
tokenGenerate: null,
115+
refreshToken: token
116+
};
117+
118+
async function generateRefreshRequest() {
119+
let randomSuffix = Math.floor(Math.random() * 1_000_000_001);
120+
let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`});
121+
var requestData = {
122+
endpoint: '/v2/token/generate',
123+
requestBody: request,
124+
}
125+
let response = await send(requestData, clientKey);
126+
let decrypt = await decryptEnvelope(response.body, clientSecret)
127+
return decrypt.body.refresh_token;
128+
};
129+
}
130+
131+
export function handleSummary(data) {
132+
return {
133+
'summary.json': JSON.stringify(data),
134+
}
135+
}
136+
137+
// Scenarios
138+
export async function tokenGenerate(data) {
139+
const endpoint = '/v2/token/generate';
140+
if (data.tokenGenerate == null) {
141+
var newData = await generateTokenGenerateRequestWithTime();
142+
data.tokenGenerate = newData;
143+
} else if (data.tokenGenerate.time < (Date.now() - 45000)) {
144+
data.tokenGenerate = await generateTokenGenerateRequestWithTime();
145+
}
146+
147+
var requestBody = data.tokenGenerate.requestBody;
148+
var tokenGenerateData = {
149+
endpoint: endpoint,
150+
requestBody: requestBody,
151+
}
152+
153+
execute(tokenGenerateData, true);
154+
}
155+
156+
export function tokenRefresh(data) {
157+
var requestBody = data.refreshToken;
158+
var refreshData = {
159+
endpoint: '/v2/token/refresh',
160+
requestBody: requestBody
161+
}
162+
163+
execute(refreshData, false);
164+
}
165+
166+
export async function identityMap() {
167+
// No caching: generate a fresh encrypted request with unique DIIs on every call.
168+
const requestData = await generateIdentityMapRequestWithTime(5000);
169+
170+
var identityMapData = {
171+
endpoint: '/v3/identity/map',
172+
requestBody: requestData.requestBody,
173+
}
174+
175+
execute(identityMapData, true);
176+
}
177+
178+
// Helpers
179+
async function createReqWithTimestamp(timestampArr, obj) {
180+
var envelope = getEnvelopeWithTimestamp(timestampArr, obj);
181+
return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer);
182+
}
183+
184+
function generateIdentityMapRequest(emailCount) {
185+
var data = {
186+
'email': []
187+
};
188+
189+
let randomSuffix = Math.floor(Math.random() * 1_000_000_001);
190+
for (var i = 0; i < emailCount; ++i) {
191+
data.email.push(`test${randomSuffix}${i}@example.com`);
192+
}
193+
194+
return data;
195+
}
196+
197+
function send(data, auth) {
198+
var options = {};
199+
if (auth) {
200+
options.headers = {
201+
'Authorization': `Bearer ${clientKey}`
202+
};
203+
}
204+
205+
return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options);
206+
}
207+
208+
function execute(data, auth) {
209+
var response = send(data, auth);
210+
211+
check(response, {
212+
'status is 200': r => r.status === 200,
213+
});
214+
}
215+
216+
async function encryptEnvelope(envelope, clientSecret) {
217+
const rawKey = encoding.b64decode(clientSecret);
218+
const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [
219+
"encrypt",
220+
"decrypt",
221+
]);
222+
223+
const iv = crypto.getRandomValues(new Uint8Array(12));
224+
225+
const ciphertext = new Uint8Array(await crypto.subtle.encrypt(
226+
{
227+
name: "AES-GCM",
228+
iv: iv,
229+
},
230+
key,
231+
envelope
232+
));
233+
234+
const result = new Uint8Array(+(1 + iv.length + ciphertext.length));
235+
236+
// The version of the envelope format.
237+
result[0] = 1;
238+
239+
result.set(iv, 1);
240+
241+
// The tag is at the end of ciphertext.
242+
result.set(ciphertext, 1 + iv.length);
243+
244+
return result;
245+
}
246+
247+
async function decryptEnvelope(envelope, clientSecret) {
248+
const rawKey = encoding.b64decode(clientSecret);
249+
const rawData = encoding.b64decode(envelope);
250+
const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [
251+
"encrypt",
252+
"decrypt",
253+
]);
254+
const length = rawData.byteLength;
255+
const iv = rawData.slice(0, 12);
256+
257+
const decrypted = await crypto.subtle.decrypt(
258+
{
259+
name: "AES-GCM",
260+
iv: iv,
261+
tagLength: 128
262+
},
263+
key,
264+
rawData.slice(12)
265+
);
266+
267+
268+
const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16)));
269+
const response = JSON.parse(decryptedResponse);
270+
271+
return response;
272+
}
273+
274+
function getEnvelopeWithTimestamp(timestampArray, obj) {
275+
var randomBytes = new Uint8Array(8);
276+
crypto.getRandomValues(randomBytes);
277+
278+
var payload = stringToUint8Array(JSON.stringify(obj));
279+
280+
var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length);
281+
envelope.set(timestampArray);
282+
envelope.set(randomBytes, timestampArray.length);
283+
envelope.set(payload, timestampArray.length + randomBytes.length);
284+
285+
return envelope;
286+
287+
}
288+
function getEnvelope(obj) {
289+
var timestampArr = new Uint8Array(getTimestamp());
290+
return getEnvelopeWithTimestamp(timestampArr, obj);
291+
}
292+
293+
function getTimestamp() {
294+
const now = Date.now();
295+
return getTimestampFromTime(now);
296+
}
297+
298+
function getTimestampFromTime(time) {
299+
const res = new ArrayBuffer(8);
300+
const { hi, lo } = Get32BitPartsBE(time);
301+
const view = new DataView(res);
302+
view.setUint32(0, hi, false);
303+
view.setUint32(4, lo, false);
304+
return res;
305+
}
306+
307+
// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html
308+
function Get32BitPartsBE(bigNumber) {
309+
if (bigNumber > 9007199254740991) {
310+
// Max int that JavaScript can represent is 2^53.
311+
throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber);
312+
}
313+
314+
var bigNumberAsBinaryStr = bigNumber.toString(2);
315+
// Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left
316+
var bigNumberAsBinaryStr2 = '';
317+
for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) {
318+
bigNumberAsBinaryStr2 += '0';
319+
};
320+
321+
bigNumberAsBinaryStr2 += bigNumberAsBinaryStr;
322+
323+
return {
324+
hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2),
325+
lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2),
326+
};
327+
}
328+
329+
function stringToUint8Array(str) {
330+
const buffer = new ArrayBuffer(str.length);
331+
const view = new Uint8Array(buffer);
332+
for (var i = 0; i < str.length; i++) {
333+
view[i] = str.charCodeAt(i);
334+
}
335+
return view;
336+
}
337+
338+
async function createReq(obj) {
339+
var envelope = getEnvelope(obj);
340+
return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer);
341+
};
342+
343+
async function generateRequestWithTime(obj) {
344+
var time = Date.now();
345+
var timestampArr = new Uint8Array(getTimestampFromTime(time));
346+
var requestBody = await createReqWithTimestamp(timestampArr, obj);
347+
var element = {
348+
time: time,
349+
requestBody: requestBody
350+
};
351+
352+
return element;
353+
}
354+
355+
async function generateTokenGenerateRequestWithTime() {
356+
let randomSuffix = Math.floor(Math.random() * 1_000_000_001);
357+
let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` };
358+
return await generateRequestWithTime(requestData);
359+
}
360+
361+
async function generateIdentityMapRequestWithTime(emailCount) {
362+
let data = generateIdentityMapRequest(emailCount);
363+
return await generateRequestWithTime(data);
364+
}
365+
366+
const generateSinceTimestampStr = () => {
367+
var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */);
368+
var year = date.getFullYear();
369+
var month = (date.getMonth() + 1).toString().padStart(2, '0');
370+
var day = date.getDate().toString().padStart(2, '0');
371+
372+
return `${year}-${month}-${day}T00:00:00`;
373+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
COMMENT=$1
4+
5+
if [ "$#" -ne 1 ]; then
6+
COMMENT=$( date '+%F_%H:%M:%S' )
7+
fi
8+
9+
./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-9-v3.js $COMMENT

0 commit comments

Comments
 (0)