-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathwallet-init.test.ts
164 lines (149 loc) · 5.3 KB
/
wallet-init.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable import/imports-first */
import * as dotenv from 'dotenv';
import path from 'path';
// This line must come before loading the env, to configure the location of the .env file
dotenv.config({ path: path.join(__dirname, '../../../.env') });
import { BaseWallet, createPersonalWallet } from '@cardano-sdk/wallet';
import { Logger } from 'ts-log';
import { bufferCount, bufferTime, from, mergeAll, tap } from 'rxjs';
import { logger } from '@cardano-sdk/util-dev';
import { Bip32Account, util } from '@cardano-sdk/key-management';
import {
MeasurementUtil,
assetProviderFactory,
bip32Ed25519Factory,
chainHistoryProviderFactory,
drepProviderFactory,
getEnv,
getLoadTestScheduler,
keyManagementFactory,
networkInfoProviderFactory,
rewardsProviderFactory,
stakePoolProviderFactory,
txSubmitProviderFactory,
utxoProviderFactory,
waitForWalletStateSettle,
walletVariables
} from '../../../src';
// Example call that creates 5000 wallets in 10 minutes:
// VIRTUAL_USERS_GENERATE_DURATION=600 VIRTUAL_USERS_COUNT=5000 yarn load-test-custom:wallet-init
const env = getEnv([...walletVariables, 'VIRTUAL_USERS_COUNT', 'VIRTUAL_USERS_GENERATE_DURATION']);
const intermediateResultsInterval = 10_000;
const walletsShutdownBatchSize = 100;
const testLogger: Logger = console;
enum MeasureTarget {
keyAgent = 'keyAgent',
wallet = 'wallet'
}
const measurementUtil = new MeasurementUtil<keyof typeof MeasureTarget>();
// Utility methods to help setup the test. They could be part of another file
const getProviders = async () => ({
assetProvider: await assetProviderFactory.create(
env.TEST_CLIENT_ASSET_PROVIDER,
env.TEST_CLIENT_ASSET_PROVIDER_PARAMS,
logger
),
chainHistoryProvider: await chainHistoryProviderFactory.create(
env.TEST_CLIENT_CHAIN_HISTORY_PROVIDER,
env.TEST_CLIENT_CHAIN_HISTORY_PROVIDER_PARAMS,
logger
),
drepProvider: await drepProviderFactory.create(
env.TEST_CLIENT_DREP_PROVIDER,
env.TEST_CLIENT_DREP_PROVIDER_PARAMS,
logger
),
networkInfoProvider: await networkInfoProviderFactory.create(
env.TEST_CLIENT_NETWORK_INFO_PROVIDER,
env.TEST_CLIENT_NETWORK_INFO_PROVIDER_PARAMS,
logger
),
rewardsProvider: await rewardsProviderFactory.create(
env.TEST_CLIENT_REWARDS_PROVIDER,
env.TEST_CLIENT_REWARDS_PROVIDER_PARAMS,
logger
),
stakePoolProvider: await stakePoolProviderFactory.create(
env.TEST_CLIENT_STAKE_POOL_PROVIDER,
env.TEST_CLIENT_STAKE_POOL_PROVIDER_PARAMS,
logger
),
txSubmitProvider: await txSubmitProviderFactory.create(
env.TEST_CLIENT_TX_SUBMIT_PROVIDER,
env.TEST_CLIENT_TX_SUBMIT_PROVIDER_PARAMS,
logger
),
utxoProvider: await utxoProviderFactory.create(
env.TEST_CLIENT_UTXO_PROVIDER,
env.TEST_CLIENT_UTXO_PROVIDER_PARAMS,
logger
)
});
const getKeyAgent = async (accountIndex: number) => {
const createKeyAgent = await keyManagementFactory.create(
env.KEY_MANAGEMENT_PROVIDER,
{ ...env.KEY_MANAGEMENT_PARAMS, accountIndex },
logger
);
const bip32Ed25519 = await bip32Ed25519Factory.create(env.KEY_MANAGEMENT_PARAMS.bip32Ed25519, null, logger);
const keyAgent = await createKeyAgent({ bip32Ed25519, logger });
return { keyAgent };
};
const createWallet = async (accountIndex: number): Promise<BaseWallet> => {
measurementUtil.addStartMarker(MeasureTarget.keyAgent, accountIndex);
const providers = await getProviders();
const { keyAgent } = await getKeyAgent(accountIndex);
measurementUtil.addMeasureMarker(MeasureTarget.keyAgent, accountIndex);
measurementUtil.addStartMarker(MeasureTarget.wallet, accountIndex);
return createPersonalWallet(
{ name: `Wallet ${accountIndex}` },
{
...providers,
bip32Account: await Bip32Account.fromAsyncKeyAgent(keyAgent),
logger,
witnesser: util.createBip32Ed25519Witnesser(keyAgent)
}
);
};
const initWallet = async (idx: number) => {
const wallet = await createWallet(idx);
await waitForWalletStateSettle(wallet);
measurementUtil.addMeasureMarker(MeasureTarget.wallet, idx);
return wallet;
};
// A very simple print function. Measurement util returns an object that could be used in any way.
const showResults = () => {
testLogger.info('Measurements:', measurementUtil.getMeasurements([MeasureTarget.wallet, MeasureTarget.keyAgent]));
};
// Starts observing measurement markers. If this method is not called, no measurements are done.
measurementUtil.start();
// Simple scheduler that distributes the requested number of calls evenly in the duration time
getLoadTestScheduler<BaseWallet>(
{
// callUnderTest must be a method returning an observable
callUnderTest: (id) => from(initWallet(id)),
callsPerDuration: env.VIRTUAL_USERS_COUNT,
duration: env.VIRTUAL_USERS_GENERATE_DURATION
},
{ logger: testLogger }
)
.pipe(
bufferTime(intermediateResultsInterval),
tap(() => {
testLogger.info(`\nPartial results every ${intermediateResultsInterval}ms:`);
showResults();
}),
mergeAll(),
bufferCount(walletsShutdownBatchSize)
)
.subscribe({
complete: () => {
testLogger.info('--------- Final results -----------------');
showResults();
measurementUtil.stop();
},
next: (wallets) => {
testLogger.info(`Shutting down ${wallets.length} wallets`);
for (const wallet of wallets) wallet.shutdown();
}
});