Skip to content

Instantly share code, notes, and snippets.

@grsLammy
Created October 3, 2023 11:42
Show Gist options
  • Save grsLammy/f7d1dc1d6499bcf6ad398f7a396e64d1 to your computer and use it in GitHub Desktop.
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
export const MILESTONE_API = "https://heimdall-api-testnet.polygon.technology/milestone/latest";
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}`);
}
}
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`;
}
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);
});
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}`);
}
}
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