Created
October 3, 2023 11:42
-
-
Save grsLammy/f7d1dc1d6499bcf6ad398f7a396e64d1 to your computer and use it in GitHub Desktop.
code snippet for performance testing and logging of Heimdal API for Milestone finality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const MILESTONE_API = "https://heimdall-api-testnet.polygon.technology/milestone/latest"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch, { Response } from "node-fetch"; | |
import { MILESTONE_API } from "./constants"; | |
import { MilestoneData, MilestoneApiResponse } from "./types"; | |
export async function milestoneFinalisedBlock(): Promise<MilestoneData> { | |
try { | |
const response: Response = await fetch(MILESTONE_API); | |
const milestoneData: MilestoneApiResponse = await response.json(); // Extract JSON data from the response | |
const endBlock: number = milestoneData.result.end_block; | |
const timestamp: number = milestoneData.result.timestamp * 1000; // Convert time to milisec from sec | |
const milestone_id: string = milestoneData.result.milestone_id; | |
return { endBlock, timestamp, milestone_id }; | |
} catch (error) { | |
throw new Error(`Error in milestoneFinalisedBlock: ${error}`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function msToMinAndSec(milliseconds: number) { | |
const totalSeconds = Math.floor(milliseconds / 1000); | |
const minutes = Math.floor(totalSeconds / 60); | |
const seconds = totalSeconds % 60; | |
return `${minutes} min and ${seconds} sec`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { milestoneFinalisedBlock } from "./utils/milestoneFinalisedBlock"; | |
import { msToMinAndSec } from "./utils/msToMinAndSec"; | |
import { saveLog } from "./utils/saveLog"; | |
async function perfTest() { | |
try { | |
let lastTimestamp: number | null = null; | |
let count: number = 0; | |
const maxCount: number = 50; | |
do { | |
const { timestamp, milestone_id } = await milestoneFinalisedBlock(); | |
if (lastTimestamp === null) lastTimestamp = timestamp; | |
if (lastTimestamp !== timestamp) { | |
const difference = timestamp - lastTimestamp; | |
const totalTime: string = await msToMinAndSec(difference); | |
const index = count + 1; | |
const obj = { index, milestone_id, totalTime }; | |
await saveLog(obj); | |
count++; | |
lastTimestamp = timestamp; | |
} | |
} while (count < maxCount); | |
} catch (error) { | |
console.log(`Error at perfTest: ${error}`); | |
process.exit(1); | |
} | |
} | |
perfTest() | |
.then(() => { | |
console.log("\n\n---------- ENDING ALL PROCESS ----------\n\n"); | |
process.exit(0); | |
}) | |
.catch((error) => { | |
console.error("error", error); | |
process.exit(1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from "fs"; | |
// Function to save receipt locally as a JSON log file | |
export async function saveLog(obj) { | |
try { | |
if (fs.existsSync("log/log.json")) { | |
const existingLog = fs.readFileSync("log/log.json", "utf-8"); | |
const parseLog = JSON.parse(existingLog); | |
parseLog.push(obj); | |
const appendExistingLog = await JSON.stringify(parseLog, null, 2); | |
fs.writeFileSync("log/log.json", appendExistingLog, "utf-8"); | |
console.log(`\nindex[${obj.index}] logged.`); | |
return; | |
} | |
fs.writeFileSync("log/log.json", "[]", "utf-8"); | |
const emptyArrayLog = fs.readFileSync("log/log.json", "utf-8"); | |
const parseLog = JSON.parse(emptyArrayLog); | |
parseLog.push(obj); | |
const newLog = await JSON.stringify(parseLog, null, 2); | |
fs.writeFileSync("log/log.json", newLog, "utf-8"); | |
console.log(`\nlog.json file created and index[${obj.index}] logged.`); | |
} catch (error) { | |
console.log(`Error in saveReceipt: ${error}`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from "ethers"; | |
export interface MilestoneData { | |
endBlock: number; | |
timestamp: number; | |
milestone_id: string; | |
} | |
export interface MilestoneApiResponse { | |
height: string; | |
result: { | |
proposer: string; | |
start_block: number; | |
end_block: number; | |
hash: string; | |
bor_chain_id: string; | |
milestone_id: string; | |
timestamp: number; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment