This commit is contained in:
aniketfuryrocks 2023-03-30 07:56:24 +05:30
parent dee4af6d04
commit 0c232926bd
No known key found for this signature in database
GPG Key ID: FA6BFCFAA7D4B764
3 changed files with 65 additions and 38 deletions

View File

@ -1,50 +1,35 @@
import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, TransactionSignature, Transaction } from '@solana/web3.js';
import * as fs from 'fs';
import * as splToken from "@solana/spl-token";
import * as os from 'os';
import { deserialize_keypair, get_postional_arg, OutputFile, sleep } from './util';
// number of users
const tps: number = +process.argv[2];
const forSeconds: number = +process.argv[3];
const tps: number = +get_postional_arg(2, 10);
const forSeconds: number = +get_postional_arg(3, 10);
// 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;
const url = get_postional_arg(4, "http://0.0.0.0:8899");
//@ts-ignore
const skip_confirmations = get_postional_arg(5, false) === "true";
(async function main() {
//@ts-ignore
const InFile: {
fee_payers: [Keypair],
users: [{
publicKey: string;
secretKey: Uint8Array;
}];
tokenAccounts: [PublicKey];
mint: PublicKey;
minted_amount: number;
} = fs.readFileSync('out.json').toJSON();
const InFile: OutputFile = JSON.parse(fs.readFileSync('out.json').toString());
console.log("benching " + tps + " transactions per second on " + url + " for " + forSeconds + " seconds");
const connection = new Connection(url, 'finalized');
console.log('get latest blockhash')
const blockhash = await connection.getLatestBlockhash({
commitment: 'finalized'
});
console.log('blockhash : ' + blockhash.blockhash);
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 blockhash = await connection.getLatestBlockhash();
console.log('blockhash : ' + blockhash.blockhash);
const payers = InFile.fee_payers.map(deserialize_keypair);
const users = InFile.users.map(deserialize_keypair);
const userAccounts = InFile.tokenAccounts.map(x => new PublicKey(x));
let signatures_to_unpack: TransactionSignature[][] = new Array<TransactionSignature[]>(forSeconds);
let time_taken_to_send = [];
let payer_index = 0;
for (let i = 0; i < forSeconds; ++i) {
console.log('Sending transaction ' + i);
const start = performance.now();
@ -66,9 +51,16 @@ const skip_confirmations = process.argv.length > 5 ? process.argv[5] === "true"
splToken.createTransferInstruction(userFrom, userTo, users[fromIndex].publicKey, Math.ceil((Math.random() + 1) * 100))
);
transaction.recentBlockhash = blockhash;
transaction.feePayer = authority.publicKey;
transaction.feePayer = payers[payer_index].publicKey;
connection.sendTransaction(transaction, [authority, users[fromIndex]], { skipPreflight: true }).then(p => { signatures_to_unpack[i][j] = p });
connection
.sendTransaction(transaction, [payers[payer_index], users[fromIndex]], { skipPreflight: true })
.then(p => { signatures_to_unpack[i][j] = p });
payer_index++;
if (payer_index == payers.length) {
payer_index = 0;
}
}
const end = performance.now();
const diff = (end - start);
@ -79,7 +71,7 @@ const skip_confirmations = process.argv.length > 5 ? process.argv[5] === "true"
}
console.log('finish sending transactions');
await delay(10000)
await sleep(10000)
console.log('checking for confirmations');
if (skip_confirmations === false) {
const size = signatures_to_unpack.length

View File

@ -2,6 +2,7 @@ import { Connection, Keypair, LAMPORTS_PER_SOL, sendAndConfirmTransaction, Syste
import * as splToken from "@solana/spl-token";
import * as fs from 'fs';
import * as os from 'os';
import { serialized_keypair } from './util';
// number of users
const nbUsers = process.argv.length > 2 ? +process.argv[2] : 10;
@ -14,6 +15,7 @@ const number_of_fee_payers = process.argv.length > 4 ? +process.argv[4] : 10;
// outfile
const outFile = process.argv.length > 4 ? process.argv[4] : "out.json";
function check_if_out_file_exists() {
if (!fs.existsSync(outFile))
return;
@ -101,13 +103,11 @@ function check_if_out_file_exists() {
console.timeLog('Time taken');
const users = userKps.map(x => ({
'publicKey': x.publicKey.toBase58(),
'secretKey': Array.from(x.secretKey)
}));
const users = userKps.map(serialized_keypair);
const fee_payers_serialized = fee_payers.map(serialized_keypair);
const data = {
'fee_payers': fee_payers,
'fee_payers': fee_payers_serialized,
'users': users,
'tokenAccounts': accounts,
'mint': mint,

35
benches/util.ts Normal file
View File

@ -0,0 +1,35 @@
import { Keypair, PublicKey } from "@solana/web3.js";
export type KeypairSerialized = {
publicKey: string;
secretKey: number[]
}
export type OutputFile = {
fee_payers: KeypairSerialized[];
users: KeypairSerialized[];
tokenAccounts: PublicKey[];
mint: PublicKey;
minted_amount: number;
}
export function serialized_keypair(keypair: Keypair): {
} {
return {
'publicKey': keypair.publicKey.toBase58(),
'secretKey': Array.from(keypair.secretKey)
}
}
export function deserialize_keypair(keypair: KeypairSerialized): Keypair {
return Keypair.fromSecretKey(Uint8Array.from(keypair.secretKey))
}
export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function get_postional_arg<T>(index: number, default_value: T): T {
return process.argv.length > index ? process.argv[index] as T : default_value;
}