Skip to content

Commit

Permalink
Add variance to mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Prithpal-Sooriya committed Oct 2, 2024
1 parent e2b965c commit a796ac7
Showing 1 changed file with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,43 @@ export function createSHA256Hash(data: string): string {
return bytesToHex(hashedData);
}

const mockData = {
const getRandomEthereumAddress = () => {
const chars = '0123456789abcdef';
let address = '0x';
for (let i = 0; i < 40; i++) {
address += chars[Math.floor(Math.random() * chars.length)];
}
return address;
};

const getRandomName = () => {
const names = [
'Quick',
'Silent',
'Brave',
'Clever',
'Mighty',
'Swift',
'Wise',
'Bold',
'Fierce',
'Noble',
];
return names[Math.floor(Math.random() * names.length)];
};

const createMockData = () => ({
v: 1,
a: '0x3fB1cB1D6A3b1cB1D6A3b1cB1D6A3b1cB1D6A3b1',
a: getRandomEthereumAddress(),
id: '3f29b2e4-8c3b-4d6a-9f1c-1a2b3c4d5e6f',
n: 'My Mock Account',
n: getRandomName(),
nlu: Date.now(),
};
});

// const mockData = JSON.stringify(createMockData());
const mockDataArray = Array.from({ length: 1000 }, createMockData).map((d) =>
JSON.stringify(d),
);

/**
* runEncryptionPerfTestWithoutCache
Expand All @@ -271,18 +301,17 @@ export async function runEncryptionPerfTestWithoutCache(
nativeScript?: NativeScrypt,
) {
const password = 'test-password';
const plaintext = JSON.stringify(mockData);

const testCases = [1, 10, 100, 1000];
// const testCases = [1, 10];

for (const count of testCases) {
clearCache();
const start = Date.now();
const start = performance.now();
for (let i = 0; i < count; i++) {
const plaintext = mockDataArray[i % mockDataArray.length];
await encryption.encryptString(plaintext, password, nativeScript);
}
const end = Date.now();
const end = performance.now();
console.log(
`runEncryptionPerfTestWithoutCache: Time taken for ${count} accounts to encrypt: ${
end - start
Expand All @@ -301,20 +330,19 @@ export async function runEncryptionPerfTestWithCache(
nativeScript?: NativeScrypt,
) {
const password = 'test-password';
const plaintext = JSON.stringify(mockData);

const testCases = [1, 10, 100, 1000];
// const testCases = [1, 10];

// Something to populate cache
await encryption.encryptString(plaintext, password);
await encryption.encryptString('warm up cache', password);

for (const count of testCases) {
const start = Date.now();
const start = performance.now();
for (let i = 0; i < count; i++) {
const plaintext = mockDataArray[i % mockDataArray.length];
await encryption.encryptString(plaintext, password, nativeScript);
}
const end = Date.now();
const end = performance.now();
console.log(
`runEncryptionPerfTestWithCache: Time taken for ${count} accounts to encrypt: ${
end - start
Expand Down

0 comments on commit a796ac7

Please sign in to comment.