Skip to content

Commit

Permalink
example -> add timing for each part
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed Feb 13, 2024
1 parent 6558aab commit 94fde99
Showing 1 changed file with 39 additions and 52 deletions.
91 changes: 39 additions & 52 deletions examples/typescript-esm/transaction_with_predefined_abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Network,
NetworkToNetworkName,
parseTypeTag,
SimpleTransaction,
TypeTagAddress,
TypeTagU64,
U64,
Expand Down Expand Up @@ -48,13 +49,23 @@ const balance = async (aptos: Aptos, name: string, address: AccountAddress) => {
return amount;
};

async function timeSubmission(aptos: Aptos, submit: () => Promise<string>): Promise<[number, number]> {
async function timeSubmission(
aptos: Aptos,
signer: Account,
buildTxn: () => Promise<SimpleTransaction>,
): Promise<void> {
const start = Date.now();
const hash = await submit();
const rawTxn = await buildTxn();
const buildTime = Date.now();
const submittedTxn = await aptos.signAndSubmitTransaction({ signer, transaction: rawTxn });
const submitTime = Date.now();
await aptos.waitForTransaction({ transactionHash: hash });
await aptos.waitForTransaction({ transactionHash: submittedTxn.hash });
const endTime = Date.now();
return [submitTime - start, endTime - start];
const builtLatency = buildTime - start;
const submitLatency = submitTime - start;
const e2eLatency = endTime - start;

console.log(`Time for building: ${builtLatency}ms | submission: ${submitLatency}ms | total E2E: ${e2eLatency}ms`);
}

const example = async () => {
Expand Down Expand Up @@ -103,76 +114,60 @@ const example = async () => {
console.log("\n=== Remote ABI, normal inputs ===\n");
const aliceAddressString = alice.accountAddress.toString();
const bobAddressString = alice.accountAddress.toString();
const [submit1, e2e1] = await timeSubmission(aptos, async () => {
const txn = await aptos.transaction.build.simple({
await timeSubmission(aptos, alice, async () =>
aptos.transaction.build.simple({
sender: aliceAddressString,
data: {
function: "0x1::coin::transfer",
typeArguments: [APTOS_COIN_TYPE],
functionArguments: [bobAddressString, TRANSFER_AMOUNT],
},
});

const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn });
return committedTxn.hash;
});
console.log(`Took for submission: ${submit1}ms | E2E: ${e2e1}ms`);
}),
);

console.log("\n=== Remote ABI, BCS inputs ===\n");
const [submit2, e2e2] = await timeSubmission(aptos, async () => {
const txn = await aptos.transaction.build.simple({
await timeSubmission(aptos, alice, async () =>
aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: [APTOS_COIN_TYPE],
functionArguments: [bob.accountAddress, new U64(TRANSFER_AMOUNT)],
},
});

const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn });
return committedTxn.hash;
});
console.log(`Took for submission: ${submit2}ms | E2E: ${e2e2}ms`);
}),
);

console.log("\n=== Local ABI, normal inputs ===\n");
const [submit3, e2e3] = await timeSubmission(aptos, async () => {
const txn = await aptos.transaction.build.simple({
await timeSubmission(aptos, alice, async () =>
aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: [APTOS_COIN_TYPE],
functionArguments: [bobAddressString, TRANSFER_AMOUNT],
abi: transferAbi,
},
});

const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn });
return committedTxn.hash;
});
console.log(`Took for submission: ${submit3}ms | E2E: ${e2e3}ms`);
}),
);

console.log("\n=== Local ABI, BCS inputs ===\n");
const [submit4, e2e4] = await timeSubmission(aptos, async () => {
const txn = await aptos.transaction.build.simple({
await timeSubmission(aptos, alice, async () =>
aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: [APTOS_COIN_TYPE],
functionArguments: [bob.accountAddress, new U64(TRANSFER_AMOUNT)],
abi: transferAbi,
},
});

const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn });
return committedTxn.hash;
});
console.log(`Took for submission: ${submit4}ms | E2E: ${e2e4}ms`);
}),
);

console.log("\n=== Local ABI, BCS inputs, sequence number already cached ===\n");
const accountData = await aptos.account.getAccountInfo({ accountAddress: alice.accountAddress });
const sequenceNumber = BigInt(accountData.sequence_number);
const [submit5, e2e5] = await timeSubmission(aptos, async () => {
const txn = await aptos.transaction.build.simple({
await timeSubmission(aptos, alice, async () =>
aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
Expand All @@ -183,16 +178,12 @@ const example = async () => {
options: {
accountSequenceNumber: sequenceNumber,
},
});

const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn });
return committedTxn.hash;
});
console.log(`Took for submission: ${submit5}ms | E2E: ${e2e5}ms`);
}),
);

console.log("\n=== Local ABI, BCS inputs, sequence number and gas already cached ===\n");
const [submit6, e2e6] = await timeSubmission(aptos, async () => {
const txn = await aptos.transaction.build.simple({
await timeSubmission(aptos, alice, async () =>
aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
Expand All @@ -205,12 +196,8 @@ const example = async () => {
gasUnitPrice: 100,
maxGasAmount: 1000,
},
});

const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction: txn });
return committedTxn.hash;
});
console.log(`Took for submission: ${submit6}ms | E2E: ${e2e6}ms`);
}),
);
};

example();

0 comments on commit 94fde99

Please sign in to comment.