98 lines
3.1 KiB
TypeScript
98 lines
3.1 KiB
TypeScript
import { bench, describe } from 'vitest';
|
|
import { type Activity, Activities } from '~/src/wallet';
|
|
import { StoreInMemory } from '~/src/storage'; // Adjust import path as needed
|
|
|
|
// Helper function to generate test activities
|
|
function generateActivities(count: number) {
|
|
const activities: Array<Activity> = [];
|
|
for (let i = 0; i < count; i++) {
|
|
activities.push({
|
|
timestamp: BigInt(i),
|
|
type: 'WalletName',
|
|
id: `wallet${i % 1000}`, // Creates some overlap in IDs to test conflict resolution
|
|
value: `Value ${i}`,
|
|
});
|
|
}
|
|
return activities;
|
|
}
|
|
|
|
describe('Activities Performance Benchmarks', () => {
|
|
bench('Adding 100,000 activities sequentially', async () => {
|
|
const store = new StoreInMemory();
|
|
const activities = new Activities(store);
|
|
const testActivities = generateActivities(100_000);
|
|
|
|
for (const activity of testActivities) {
|
|
await activities.addActivity(activity);
|
|
}
|
|
});
|
|
|
|
bench('Adding 100,000 activities in batches of 1000', async () => {
|
|
const store = new StoreInMemory();
|
|
const activities = new Activities(store);
|
|
const testActivities = generateActivities(100_000);
|
|
const batchSize = 1000;
|
|
|
|
for (let i = 0; i < testActivities.length; i += batchSize) {
|
|
const batch = testActivities.slice(i, i + batchSize);
|
|
await Promise.all(
|
|
batch.map((activity) => activities.addActivity(activity)),
|
|
);
|
|
}
|
|
});
|
|
|
|
bench('Compiling snapshot after 100,000 activities', async () => {
|
|
const store = new StoreInMemory();
|
|
const activities = new Activities(store);
|
|
const testActivities = generateActivities(100_000);
|
|
|
|
// First add all activities
|
|
await Promise.all(
|
|
testActivities.map((activity) => activities.addActivity(activity)),
|
|
);
|
|
|
|
// Benchmark the snapshot compilation
|
|
await activities.compileSnapshot();
|
|
});
|
|
|
|
bench('Mixed operations benchmark', async () => {
|
|
const store = new StoreInMemory();
|
|
const activities = new Activities(store);
|
|
|
|
// Add activities in smaller batches with intermittent snapshot compilations
|
|
const batchSize = 10000;
|
|
const totalActivities = 100_000;
|
|
const testActivities = generateActivities(totalActivities);
|
|
|
|
for (let i = 0; i < totalActivities; i += batchSize) {
|
|
const batch = testActivities.slice(i, i + batchSize);
|
|
await Promise.all(
|
|
batch.map((activity) => activities.addActivity(activity)),
|
|
);
|
|
|
|
// Compile snapshot every 100,000 activities
|
|
if ((i + batchSize) % 100000 === 0) {
|
|
await activities.compileSnapshot();
|
|
}
|
|
}
|
|
});
|
|
|
|
bench('Activity conflict resolution performance', async () => {
|
|
const store = new StoreInMemory();
|
|
const activities = new Activities(store);
|
|
|
|
// Generate activities with lots of conflicts (same id/type)
|
|
const testActivities = Array.from({ length: 100_000 }, (_, i) => ({
|
|
timestamp: BigInt(i),
|
|
type: 'WalletName',
|
|
id: `wallet${i % 100}`, // Only 100 unique wallets, forcing lots of conflicts
|
|
value: `Value ${i}`,
|
|
}));
|
|
|
|
await Promise.all(
|
|
testActivities.map((activity) => activities.addActivity(activity)),
|
|
);
|
|
await activities.compileSnapshot();
|
|
});
|
|
});
|