diff --git a/performance-testing/uid2-operator/k6-identity-map.js b/performance-testing/uid2-operator/k6-identity-map.js index 3b03d2f..de404e2 100755 --- a/performance-testing/uid2-operator/k6-identity-map.js +++ b/performance-testing/uid2-operator/k6-identity-map.js @@ -8,10 +8,10 @@ const baseUrl = __ENV.OPERATOR_URL; const clientSecret = __ENV.CLIENT_SECRET; const clientKey = __ENV.CLIENT_KEY; const identityMapVUs = 300; -const identityMapLargeBatchVUs = 10; +const identityMapLargeBatchVUs = 30; const generateVUs = vus; -const testDuration = '5m' +const testDuration = '15m' export const options = { insecureSkipTLSVerify: true, @@ -47,9 +47,9 @@ export const options = { executor: 'constant-vus', exec: 'identityMapLargeBatch', vus: identityMapLargeBatchVUs, - duration: '300s', + duration: testDuration, gracefulStop: '0s', - startTime: '40s', + startTime: '30s', }, }, // So we get count in the summary, to demonstrate different metrics are different diff --git a/performance-testing/uid2-operator/k6-test-resource.yml b/performance-testing/uid2-operator/k6-test-resource.yml index d3b5679..c623bcf 100644 --- a/performance-testing/uid2-operator/k6-test-resource.yml +++ b/performance-testing/uid2-operator/k6-test-resource.yml @@ -3,7 +3,7 @@ kind: TestRun metadata: name: k6-uid2-load-test spec: - parallelism: 10 # original 4 + parallelism: 20 # original 4 arguments: --out experimental-prometheus-rw --tag "testid=replacecomment" script: configMap: diff --git a/performance-testing/uid2-operator/k6-token-generate-identitymaplarge.js b/performance-testing/uid2-operator/k6-token-generate-identitymaplarge.js new file mode 100644 index 0000000..42bed97 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-identitymaplarge.js @@ -0,0 +1,445 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateVUs = 300; +const refreshVUs = vus; +const identityMapVUs = 30; +const keySharingVUs = vus; +const testDuration = '15m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-vus', + exec: 'tokenGenerate', + stages: [ + { duration: '30s', target: generateVUs} + ], + gracefulRampDown: '0s', + }, + // tokenRefreshWarmup: { + // executor: 'ramping-vus', + // exec: 'tokenRefresh', + // stages: [ + // { duration: '30s', target: refreshVUs} + // ], + // gracefulRampDown: '0s', + // }, + identityMapWarmup: { + executor: 'ramping-vus', + exec: 'identityMap', + stages: [ + { duration: '30s', target: generateVUs} + ], + gracefulRampDown: '0s', + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: '30s', target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-vus', + exec: 'tokenGenerate', + vus: generateVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: '30s', + }, + // tokenRefresh: { + // executor: 'constant-vus', + // exec: 'tokenRefresh', + // vus: refreshVUs, + // duration: testDuration, + // gracefulStop: '0s', + // startTime: '30s', + // }, + // identityMap: { + // executor: 'constant-vus', + // exec: 'identityMapLargeBatch', + // vus: identityMapVUs, + // duration: testDuration, + // gracefulStop: '0s', + // startTime: '30s', + // }, + /* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: '30s', + },*/ + // identityMapLargeBatchSequential: { + // executor: 'constant-vus', + // exec: 'identityMapLargeBatch', + // vus: 1, + // duration: '300s', + // gracefulStop: '0s', + // startTime: '970s', + // }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: identityMapVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: '30s', + }, + // identityBuckets: { + // executor: 'constant-vus', + // exec: 'identityBuckets', + // vus: 2, + // duration: '300s', + // gracefulStop: '0s', + // startTime: '1590s', + // }, + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let request = await createReq( {'optout_check': 1, 'email': 'test5000@example.com'}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(2);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + 'optout_check': 1, + "email": [] + }; + + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let requestData = { 'optout_check': 1, 'email': 'test500@example.com' }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-1-v3.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-1-v3.js new file mode 100644 index 0000000..845f4b6 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-1-v3.js @@ -0,0 +1,464 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 450000; +const refreshRPS = 25000; +const identityMapRPS = 1500; + +const warmUpTime = '10m' +const testDuration = '20m' + + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 1500, + maxVUs: 2500, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 1800, + maxVUs: 2500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: '30s', + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + var dii = 100; + data.identityMap = await generateIdentityMapRequestWithTime(dii);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-1.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-1.js new file mode 100644 index 0000000..e2dd03e --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-1.js @@ -0,0 +1,465 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 450000; +const refreshRPS = 25000; +const identityMapRPS = 1500; + +const warmUpTime = '10m' +const testDuration = '20m' + + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 1500, + maxVUs: 2500, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 1800, + maxVUs: 2500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: '30s', + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + var dii = 100; + data.identityMap = await generateIdentityMapRequestWithTime(dii);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + 'optout_check': 1, + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-2-v3.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-2-v3.js new file mode 100644 index 0000000..c527aa3 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-2-v3.js @@ -0,0 +1,462 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 25000; +const refreshRPS = 25000; +const identityMapRPS = 1500; + +const warmUpTime = '10m' +const testDuration = '20m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-2.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-2.js new file mode 100644 index 0000000..8ff7b08 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-2.js @@ -0,0 +1,463 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 25000; +const refreshRPS = 25000; +const identityMapRPS = 1500; + +const warmUpTime = '10m' +const testDuration = '20m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + 'optout_check': 1, + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-3-v3.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-3-v3.js new file mode 100644 index 0000000..7c6a411 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-3-v3.js @@ -0,0 +1,468 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 450000; +const refreshRPS = 25000; +const identityMapRPS = 1500; +const largeBatchChance = 0.02; + +const warmUpTime = '10m' +const testDuration = '20m' + + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 1500, + maxVUs: 2500, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 1800, + maxVUs: 2500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: '30s', + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + var dii = 100; + if (Math.random() < largeBatchChance) { + dii = 5000; + } + data.identityMap = await generateIdentityMapRequestWithTime(dii);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-3.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-3.js new file mode 100644 index 0000000..1613f76 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-3.js @@ -0,0 +1,469 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 450000; +const refreshRPS = 25000; +const identityMapRPS = 1500; +const largeBatchChance = 0.02; + +const warmUpTime = '10m' +const testDuration = '20m' + + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 1500, + maxVUs: 2500, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 1800, + maxVUs: 2500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 75, + maxVUs: 150, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: '30s', + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + var dii = 100; + if (Math.random() < largeBatchChance) { + dii = 5000; + } + data.identityMap = await generateIdentityMapRequestWithTime(dii);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + 'optout_check': 1, + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-4-v3.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-4-v3.js new file mode 100644 index 0000000..3377894 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-4-v3.js @@ -0,0 +1,462 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 50000; +const refreshRPS = 50000; +const identityMapRPS = 8000; + +const warmUpTime = '20m' +const testDuration = '20m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 3000, + maxVUs: 8000, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 3000, + maxVUs: 8000, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-4.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-4.js new file mode 100644 index 0000000..44fbeb8 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-4.js @@ -0,0 +1,463 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 50000; +const refreshRPS = 50000; +const identityMapRPS = 8000; + +const warmUpTime = '20m' +const testDuration = '20m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 3000, + maxVUs: 8000, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 3000, + maxVUs: 8000, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(5000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + 'optout_check': 1, + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-5-v3.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-5-v3.js new file mode 100644 index 0000000..6bd2977 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-5-v3.js @@ -0,0 +1,462 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 25000; +const refreshRPS = 25000; +const identityMapRPS = 1500; + +const warmUpTime = '10m' +const testDuration = '20m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(10000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(10000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-5.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-5.js new file mode 100644 index 0000000..3a733c8 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-5.js @@ -0,0 +1,463 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 25000; +const refreshRPS = 25000; +const identityMapRPS = 1500; + +const warmUpTime = '10m' +const testDuration = '20m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(10000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v2/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(10000);; + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +function generateIdentityMapRequest(emailCount) { + var data = { + 'optout_check': 1, + "email": [] + }; + + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + for (var i = 0; i < emailCount; ++i) { + data.email.push(`test${randomSuffix}${i}@example.com`); + } + + return data; +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime(emailCount) { + let emails = generateIdentityMapRequest(emailCount); + return await generateRequestWithTime(emails); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-6-v3.js b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-6-v3.js new file mode 100644 index 0000000..7d12191 --- /dev/null +++ b/performance-testing/uid2-operator/k6-token-generate-refresh-identitymap-scenario-6-v3.js @@ -0,0 +1,484 @@ +import encoding from 'k6/encoding'; +import { check } from 'k6'; +import http from 'k6/http'; + +const vus = 50; +const baseUrl = __ENV.OPERATOR_URL; +const clientSecret = __ENV.CLIENT_SECRET; +const clientKey = __ENV.CLIENT_KEY; + +const generateRPS = 25000; +const refreshRPS = 25000; +const identityMapRPS = 1500; + +const warmUpTime = '10m' +const testDuration = '20m' + +export const options = { + insecureSkipTLSVerify: true, + noConnectionReuse: false, + scenarios: { + // Warmup scenarios + tokenGenerateWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenGenerate', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: generateRPS} + ], + }, + tokenRefreshWarmup: { + executor: 'ramping-arrival-rate', + exec: 'tokenRefresh', + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + stages: [ + { duration: warmUpTime, target: refreshRPS} + ], + }, + identityMapWarmup: { + executor: 'ramping-arrival-rate', + exec: 'identityMap', + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + stages: [ + { duration: warmUpTime, target: identityMapRPS} + ], + },/* + keySharingWarmup: { + executor: 'ramping-vus', + exec: 'keySharing', + stages: [ + { duration: warmUpTime, target: keySharingVUs} + ], + gracefulRampDown: '0s', + },*/ + // Actual testing scenarios + tokenGenerate: { + executor: 'constant-arrival-rate', + exec: 'tokenGenerate', + rate: generateRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + tokenRefresh: { + executor: 'constant-arrival-rate', + exec: 'tokenRefresh', + rate: refreshRPS, + timeUnit: '1s', + preAllocatedVUs: 200, + maxVUs: 400, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + }, + identityMap: { + executor: 'constant-arrival-rate', + exec: 'identityMap', + rate: identityMapRPS, + timeUnit: '1s', + preAllocatedVUs: 1000, + maxVUs: 1500, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },/* + keySharing:{ + executor: 'constant-vus', + exec: 'keySharing', + vus: keySharingVUs, + duration: testDuration, + gracefulStop: '0s', + startTime: warmUpTime, + },*/ + /*identityMapLargeBatchSequential: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 1, + duration: '300s', + gracefulStop: '0s', + startTime: '970s', + }, + identityMapLargeBatch: { + executor: 'constant-vus', + exec: 'identityMapLargeBatch', + vus: 16, + duration: '300s', + gracefulStop: '0s', + startTime: '1280s', + }, + identityBuckets: { + executor: 'constant-vus', + exec: 'identityBuckets', + vus: 2, + duration: '300s', + gracefulStop: '0s', + startTime: '1590s', + },*/ + }, + // So we get count in the summary, to demonstrate different metrics are different + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(90)', 'p(95)', 'p(99)', 'count'], + thresholds: { + // Intentionally empty. We'll programatically define our bogus + // thresholds (to generate the sub-metrics) below. In your real-world + // load test, you can add any real threshoulds you want here. + } +}; + +// https://community.k6.io/t/multiple-scenarios-metrics-per-each/1314/3 +for (let key in options.scenarios) { + // Each scenario automaticall tags the metrics it generates with its own name + let thresholdName = `http_req_duration{scenario:${key}}`; + // Check to prevent us from overwriting a threshold that already exists + if (!options.thresholds[thresholdName]) { + options.thresholds[thresholdName] = []; + } + // 'max>=0' is a bogus condition that will always be fulfilled + options.thresholds[thresholdName].push('max>=0'); +} + +export async function setup() { + var token = await generateRefreshRequest(); + return { + tokenGenerate: null, + identityMap: null, + refreshToken: token + }; + + async function generateRefreshRequest() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let request = await createReq( {'optout_check': 1, 'email': `test${randomSuffix}@example.com`}); + var requestData = { + endpoint: '/v2/token/generate', + requestBody: request, + } + let response = await send(requestData, clientKey); + let decrypt = await decryptEnvelope(response.body, clientSecret) + return decrypt.body.refresh_token; + }; +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), + } +} + +// Scenarios +export async function tokenGenerate(data) { + const endpoint = '/v2/token/generate'; + if (data.tokenGenerate == null) { + var newData = await generateTokenGenerateRequestWithTime(); + data.tokenGenerate = newData; + } else if (data.tokenGenerate.time < (Date.now() - 45000)) { + data.tokenGenerate = await generateTokenGenerateRequestWithTime(); + } + + var requestBody = data.tokenGenerate.requestBody; + var tokenGenerateData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(tokenGenerateData, true); +} + +export function tokenRefresh(data) { + var requestBody = data.refreshToken; + var refreshData = { + endpoint: '/v2/token/refresh', + requestBody: requestBody + } + + execute(refreshData, false); +} + +export async function identityMap(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(); + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export async function identityMapLargeBatch(data) { + const endpoint = '/v3/identity/map'; + if ((data.identityMap == null) || (data.identityMap.time < (Date.now() - 45000))) { + data.identityMap = await generateIdentityMapRequestWithTime(); + } + + var requestBody = data.identityMap.requestBody; + var identityData = { + endpoint: endpoint, + requestBody: requestBody, + } + execute(identityData, true); +} + +export function identityBuckets(data) { + var requestData = data.identityBuckets.requestData; + var elementToUse = selectRequestData(requestData); + + var bucketData = { + endpoint: data.identityBuckets.endpoint, + requestBody: elementToUse.requestBody, + } + execute(bucketData, true); +} + +export async function keySharing(data) { + const endpoint = '/v2/key/sharing'; + if (data.keySharing == null) { + var newData = await generateKeySharingRequestWithTime(); + data.keySharing = newData; + } else if (data.keySharing.time < (Date.now() - 45000)) { + data.keySharing = await generateKeySharingRequestWithTime(); + } + + var requestBody = data.keySharing.requestBody; + var keySharingData = { + endpoint: endpoint, + requestBody: requestBody, + } + + execute(keySharingData, true); +} + +// Helpers +async function createReqWithTimestamp(timestampArr, obj) { + var envelope = getEnvelopeWithTimestamp(timestampArr, obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +} + +// v3 supports mixed identity types in a single request. Each request contains +// 2,500 of each type: email, email_hash, phone, phone_hash (10,000 total). +async function generateIdentityMapRequest() { + const randomSuffix = Math.floor(Math.random() * 1_000_000_001); + const areaCode = ((randomSuffix % 800) + 200).toString(); + const countPerType = 2500; + + const emails = []; + const emailHashes = []; + const phones = []; + const phoneHashes = []; + + for (let i = 0; i < countPerType; i++) { + const email = `test${randomSuffix}${i}@example.com`; + emails.push(email); + emailHashes.push(await sha256Base64(email)); + + const phone = `+1${areaCode}555${i.toString().padStart(4, '0')}`; + phones.push(phone); + phoneHashes.push(await sha256Base64(phone)); + } + + return { + email: emails, + email_hash: emailHashes, + phone: phones, + phone_hash: phoneHashes, + }; +} + +async function sha256Base64(str) { + const data = stringToUint8Array(str); + const hashBuffer = await crypto.subtle.digest('SHA-256', data); + return encoding.b64encode(hashBuffer); +} + +function send(data, auth) { + var options = {}; + if (auth) { + options.headers = { + 'Authorization': `Bearer ${clientKey}` + }; + } + + return http.post(`${baseUrl}${data.endpoint}`, data.requestBody, options); +} + +function execute(data, auth) { + var response = send(data, auth); + + check(response, { + 'status is 200': r => r.status === 200, + }); +} + +async function encryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + + const iv = crypto.getRandomValues(new Uint8Array(12)); + + const ciphertext = new Uint8Array(await crypto.subtle.encrypt( + { + name: "AES-GCM", + iv: iv, + }, + key, + envelope + )); + + const result = new Uint8Array(+(1 + iv.length + ciphertext.length)); + + // The version of the envelope format. + result[0] = 1; + + result.set(iv, 1); + + // The tag is at the end of ciphertext. + result.set(ciphertext, 1 + iv.length); + + return result; +} + +async function decryptEnvelope(envelope, clientSecret) { + const rawKey = encoding.b64decode(clientSecret); + const rawData = encoding.b64decode(envelope); + const key = await crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [ + "encrypt", + "decrypt", + ]); + const length = rawData.byteLength; + const iv = rawData.slice(0, 12); + + const decrypted = await crypto.subtle.decrypt( + { + name: "AES-GCM", + iv: iv, + tagLength: 128 + }, + key, + rawData.slice(12) + ); + + + const decryptedResponse = String.fromCharCode.apply(String, new Uint8Array(decrypted.slice(16))); + const response = JSON.parse(decryptedResponse); + + return response; +} + +function getEnvelopeWithTimestamp(timestampArray, obj) { + var randomBytes = new Uint8Array(8); + crypto.getRandomValues(randomBytes); + + var payload = stringToUint8Array(JSON.stringify(obj)); + + var envelope = new Uint8Array(timestampArray.length + randomBytes.length + payload.length); + envelope.set(timestampArray); + envelope.set(randomBytes, timestampArray.length); + envelope.set(payload, timestampArray.length + randomBytes.length); + + return envelope; + +} +function getEnvelope(obj) { + var timestampArr = new Uint8Array(getTimestamp()); + return getEnvelopeWithTimestamp(timestampArr, obj); +} + +function getTimestamp() { + const now = Date.now(); + return getTimestampFromTime(now); +} + +function getTimestampFromTime(time) { + const res = new ArrayBuffer(8); + const { hi, lo } = Get32BitPartsBE(time); + const view = new DataView(res); + view.setUint32(0, hi, false); + view.setUint32(4, lo, false); + return res; +} + +// http://anuchandy.blogspot.com/2015/03/javascript-how-to-extract-lower-32-bit.html +function Get32BitPartsBE(bigNumber) { + if (bigNumber > 9007199254740991) { + // Max int that JavaScript can represent is 2^53. + throw new Error('The 64-bit value is too big to be represented in JS :' + bigNumber); + } + + var bigNumberAsBinaryStr = bigNumber.toString(2); + // Convert the above binary str to 64 bit (actually 52 bit will work) by padding zeros in the left + var bigNumberAsBinaryStr2 = ''; + for (var i = 0; i < 64 - bigNumberAsBinaryStr.length; i++) { + bigNumberAsBinaryStr2 += '0'; + }; + + bigNumberAsBinaryStr2 += bigNumberAsBinaryStr; + + return { + hi: parseInt(bigNumberAsBinaryStr2.substring(0, 32), 2), + lo: parseInt(bigNumberAsBinaryStr2.substring(32), 2), + }; +} + +function stringToUint8Array(str) { + const buffer = new ArrayBuffer(str.length); + const view = new Uint8Array(buffer); + for (var i = 0; i < str.length; i++) { + view[i] = str.charCodeAt(i); + } + return view; +} + +async function createReq(obj) { + var envelope = getEnvelope(obj); + return encoding.b64encode((await encryptEnvelope(envelope, clientSecret)).buffer); +}; + +async function generateRequestWithTime(obj) { + var time = Date.now(); + var timestampArr = new Uint8Array(getTimestampFromTime(time)); + var requestBody = await createReqWithTimestamp(timestampArr, obj); + var element = { + time: time, + requestBody: requestBody + }; + + return element; +} + +async function generateTokenGenerateRequestWithTime() { + let randomSuffix = Math.floor(Math.random() * 1_000_000_001); + let requestData = { 'optout_check': 1, 'email': `test${randomSuffix}@example.com` }; + return await generateRequestWithTime(requestData); +} + +async function generateIdentityMapRequestWithTime() { + let data = await generateIdentityMapRequest(); + return await generateRequestWithTime(data); +} + +async function generateKeySharingRequestWithTime() { + let requestData = { }; + return await generateRequestWithTime(requestData); +} + +const generateSinceTimestampStr = () => { + var date = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000 /* 2 days ago */); + var year = date.getFullYear(); + var month = (date.getMonth() + 1).toString().padStart(2, '0'); + var day = date.getDate().toString().padStart(2, '0'); + + return `${year}-${month}-${day}T00:00:00`; +}; diff --git a/performance-testing/uid2-operator/k6-token-generate.js b/performance-testing/uid2-operator/k6-token-generate.js index 6f10b36..588d81a 100755 --- a/performance-testing/uid2-operator/k6-token-generate.js +++ b/performance-testing/uid2-operator/k6-token-generate.js @@ -9,7 +9,7 @@ const clientSecret = __ENV.CLIENT_SECRET; const clientKey = __ENV.CLIENT_KEY; const generateVUs = vus; -const testDuration = '5m' +const testDuration = '15m' export const options = { insecureSkipTLSVerify: true, diff --git a/performance-testing/uid2-operator/start-generate-identitymaplarge.sh b/performance-testing/uid2-operator/start-generate-identitymaplarge.sh new file mode 100644 index 0000000..a5f9d8a --- /dev/null +++ b/performance-testing/uid2-operator/start-generate-identitymaplarge.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-identitymaplarge.js $COMMENT \ No newline at end of file diff --git a/performance-testing/uid2-operator/start-scenario-1-v3.sh b/performance-testing/uid2-operator/start-scenario-1-v3.sh new file mode 100644 index 0000000..fbf778f --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-1-v3.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-1-v3.js $COMMENT diff --git a/performance-testing/uid2-operator/start-scenario-1.sh b/performance-testing/uid2-operator/start-scenario-1.sh new file mode 100644 index 0000000..5b1c559 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-1.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-1.js $COMMENT \ No newline at end of file diff --git a/performance-testing/uid2-operator/start-scenario-2-v3.sh b/performance-testing/uid2-operator/start-scenario-2-v3.sh new file mode 100644 index 0000000..1cacc38 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-2-v3.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-2-v3.js $COMMENT diff --git a/performance-testing/uid2-operator/start-scenario-2.sh b/performance-testing/uid2-operator/start-scenario-2.sh new file mode 100644 index 0000000..64a3542 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-2.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-2.js $COMMENT \ No newline at end of file diff --git a/performance-testing/uid2-operator/start-scenario-3-v3.sh b/performance-testing/uid2-operator/start-scenario-3-v3.sh new file mode 100644 index 0000000..bb354f0 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-3-v3.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-3-v3.js $COMMENT diff --git a/performance-testing/uid2-operator/start-scenario-3.sh b/performance-testing/uid2-operator/start-scenario-3.sh new file mode 100644 index 0000000..bafbe00 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-3.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-3.js $COMMENT \ No newline at end of file diff --git a/performance-testing/uid2-operator/start-scenario-4-v3.sh b/performance-testing/uid2-operator/start-scenario-4-v3.sh new file mode 100644 index 0000000..fbb88d8 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-4-v3.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-4-v3.js $COMMENT diff --git a/performance-testing/uid2-operator/start-scenario-4.sh b/performance-testing/uid2-operator/start-scenario-4.sh new file mode 100644 index 0000000..fd43a5a --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-4.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-4.js $COMMENT \ No newline at end of file diff --git a/performance-testing/uid2-operator/start-scenario-5-v3.sh b/performance-testing/uid2-operator/start-scenario-5-v3.sh new file mode 100644 index 0000000..82b1fea --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-5-v3.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-5-v3.js $COMMENT diff --git a/performance-testing/uid2-operator/start-scenario-5.sh b/performance-testing/uid2-operator/start-scenario-5.sh new file mode 100644 index 0000000..96bb319 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-5.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-5.js $COMMENT diff --git a/performance-testing/uid2-operator/start-scenario-6-v3.sh b/performance-testing/uid2-operator/start-scenario-6-v3.sh new file mode 100644 index 0000000..edc30e2 --- /dev/null +++ b/performance-testing/uid2-operator/start-scenario-6-v3.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMMENT=$1 + +if [ "$#" -ne 1 ]; then + COMMENT=$( date '+%F_%H:%M:%S' ) +fi + +./start-named-test.sh k6-token-generate-refresh-identitymap-scenario-6-v3.js $COMMENT