Skip to content

Commit 09eb25e

Browse files
authored
Refactor secureRandom function and update logging
1 parent 2e4d416 commit 09eb25e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

main-workshop/load-generator/src/module1-observability.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
import http from 'k6/http';
99
import { check, sleep } from 'k6';
1010
import { Counter, Trend } from 'k6/metrics';
11+
import { randomBytes } from 'k6/crypto';
1112
import { generateUUID } from './aws-utils.js';
1213

1314
// Helper: cryptographically secure [0,1)
1415
function secureRandom() {
15-
// Uint32 can store values 0 - 2^32-1, so scale appropriately
16-
const array = new Uint32Array(1);
17-
crypto.getRandomValues(array);
18-
return array[0] / 4294967296; // 2^32
16+
// Get 4 random bytes and convert to Uint32
17+
const bytes = randomBytes(4);
18+
const view = new DataView(bytes);
19+
const value = view.getUint32(0, false);
20+
return value / 4294967296; // 2^32
1921
}
2022

2123
// Custom metrics
@@ -117,7 +119,7 @@ export function generateErrorRequests() {
117119

118120
if (success) {
119121
errorRequests.add(1);
120-
console.log(` Error request (status ${response.status}) - ${duration}ms [${correlationId}]`);
122+
console.log(` Error request (status ${response.status}) - ${duration}ms [${correlationId}]`);
121123
} else {
122124
console.log(`⚠️ Error request got unexpected status ${response.status} - ${duration}ms [${correlationId}]`);
123125
}

0 commit comments

Comments
 (0)