Merge pull request #19 from blockworks-foundation/adding_some_scripts

adding scripts to create n keypairs and bench with these kps
This commit is contained in:
galactus 2022-12-10 14:59:20 +01:00 committed by GitHub
commit 437d5dbbfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 9403 additions and 1854 deletions

101
bench_transactions.ts Normal file
View File

@ -0,0 +1,101 @@
import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, TransactionSignature } from '@solana/web3.js';
import * as fs from 'fs';
import * as splToken from "@solana/spl-token";
import * as os from 'os';
// number of users
const tps : number = +process.argv[2];
const forSeconds : number = +process.argv[3];
// url
const url = process.argv.length > 4 ? process.argv[4] : "http://localhost:8899";
const skip_confirmations = process.argv.length > 5 ? process.argv[5] === "true": false;
import * as InFile from "./out.json";
function sleep(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}
console.log("benching " + tps + " transactions per second on " + url + " for " + forSeconds + " seconds");
export async function main() {
const connection = new Connection(url, 'confirmed');
const authority = Keypair.fromSecretKey(
Uint8Array.from(
JSON.parse(
process.env.KEYPAIR ||
fs.readFileSync(os.homedir() + '/.config/solana/id.json', 'utf-8'),
),
),
);
const users = InFile.users.map(x => Keypair.fromSecretKey(Uint8Array.from(x.secretKey)));
const userAccounts = InFile.tokenAccounts.map(x => new PublicKey(x));
let promises_to_unpack : Promise<TransactionSignature>[][] = [];
for (let i = 0; i<forSeconds; ++i)
{
const start = performance.now();
let promises : Promise<TransactionSignature>[] = [];
for (let j=0; j<tps; ++j)
{
const toIndex = Math.floor(Math.random() * users.length);
let fromIndex = toIndex;
while (fromIndex === toIndex)
{
fromIndex = Math.floor(Math.random() * users.length);
}
const userFrom = userAccounts[fromIndex];
const userTo = userAccounts[toIndex];
if(skip_confirmations === false) {
promises.push(
splToken.transfer(
connection,
authority,
userFrom,
userTo,
users[fromIndex],
100,
)
)
}
}
if (skip_confirmations === false)
{
promises_to_unpack.push(promises)
}
const end = performance.now();
const diff = (end - start);
if (diff > 0) {
await sleep(1000 - diff)
}
}
console.log('checking for confirmations');
if(skip_confirmations === false) {
const size = promises_to_unpack.length
let successes : Uint32Array = new Uint32Array(size).fill(0);
let failures : Uint32Array = new Uint32Array(size).fill(0);
for (let i=0; i< size; ++i)
{
const promises = promises_to_unpack[i];
await Promise.all( promises.map( promise => {
promise.then((_fullfil)=>{
Atomics.add(successes, i, 1);
},
(_reject)=>{
Atomics.add(failures, i, 1);
})
}))
}
console.log("sucesses " + successes)
console.log("failures " + failures)
}
}
main().then(x => {
console.log('finished sucessfully')
}).catch(e => {
console.log('caught an error : ' + e)
})

76
create_n_users.ts Normal file
View File

@ -0,0 +1,76 @@
import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
import * as fs from 'fs';
import * as splToken from "@solana/spl-token";
import * as os from 'os';
// number of users
const nbUsers = +process.argv[2];
// url
const url = process.argv.length > 3 ? process.argv[3] : "http://localhost:8899";
// outfile
const outFile = process.argv.length > 4 ? process.argv[4] : "out.json";
console.log("creating " + nbUsers + " Users on " + url + " out file " + outFile);
export async function main() {
const connection = new Connection(url, 'confirmed');
let authority = Keypair.fromSecretKey(
Uint8Array.from(
JSON.parse(
process.env.KEYPAIR ||
fs.readFileSync(os.homedir() + '/.config/solana/id.json', 'utf-8'),
),
),
);
let userKps = [...Array(nbUsers)].map(_x => Keypair.generate())
let mint = await splToken.createMint(
connection,
authority,
authority.publicKey,
null,
6,
);
let accounts = await Promise.all( userKps.map(x => {
return splToken.createAccount(
connection,
authority,
mint,
x.publicKey,
)
}));
let res = await Promise.all( accounts.map(x=> {
return splToken.mintTo(
connection,
authority,
mint,
x,
authority,
1_000_000_000_000,
)
}));
const users = userKps.map(x => {
const info = {};
info['publicKey'] = x.publicKey.toBase58();
info['secretKey'] = Array.from(x.secretKey);
return info;
});
const data = {
'users' : users,
'tokenAccounts' : accounts,
'mint' : mint,
'minted_amount' : 1_000_000_000_000
};
console.log('created ' + nbUsers + ' Users and minted 10^12 tokens for mint ' + mint);
fs.writeFileSync(outFile, JSON.stringify(data));
}
main().then(x => {
console.log('finished sucessfully')
}).catch(e => {
console.log('caught an error : ' + e)
})

7241
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,11 +6,14 @@
"license": "MIT",
"private": true,
"dependencies": {
"@solana/web3.js": "^1.66.2",
"@solana/web3.js": "^1.62.0",
"@types/jest": "^29.2.3",
"jest": "^29.3.1",
"start-server-and-test": "^1.14.0",
"ts-jest": "^29.0.3"
"ts-jest": "^29.0.3",
"@solana/spl-token": "^0.3.5",
"ts-node": "^10.9.1",
"yarn": "^1.22.19"
},
"scripts": {
"test": "jest --detectOpenHandles",

View File

@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
@ -35,7 +35,7 @@
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
@ -76,7 +76,7 @@
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}

3826
yarn.lock

File diff suppressed because it is too large Load Diff