@silvana-one/mina-utils
Version:
Silvana Mina Utils
80 lines (71 loc) • 2.77 kB
text/typescript
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function makeString(length: number): string {
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
let outString: string = ``;
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
const inOptions: string = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`;
for (let i = 0; i < length; i++) {
outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));
}
return outString;
}
export function formatTime(ms: number): string {
if (ms === undefined) return "";
if (ms < 1000) return ms.toString() + " ms";
if (ms < 60 * 1000)
return parseInt((ms / 1000).toString()).toString() + " sec";
if (ms < 60 * 60 * 1000) {
const minutes = parseInt((ms / 1000 / 60).toString());
const seconds = parseInt(((ms - minutes * 60 * 1000) / 1000).toString());
return minutes.toString() + " min " + seconds.toString() + " sec";
} else {
const hours = parseInt((ms / 1000 / 60 / 60).toString());
const minutes = parseInt(
((ms - hours * 60 * 60 * 1000) / 1000 / 60).toString()
);
return hours.toString() + " h " + minutes.toString() + " min";
}
}
export class Memory {
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
static rss: number = 0;
constructor() {
Memory.rss = 0;
}
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
public static info(description: string = ``, fullInfo: boolean = false) {
const memoryData = process.memoryUsage();
const formatMemoryUsage = (data: number) =>
`${Math.round(data / 1024 / 1024)} MB`;
const oldRSS = Memory.rss;
Memory.rss = Math.round(memoryData.rss / 1024 / 1024);
const memoryUsage = fullInfo
? {
step: `${description}:`,
rssDelta: `${(oldRSS === 0
? 0
: Memory.rss - oldRSS
).toString()} MB -> Resident Set Size memory change`,
rss: `${formatMemoryUsage(
memoryData.rss
)} -> Resident Set Size - total memory allocated`,
heapTotal: `${formatMemoryUsage(
memoryData.heapTotal
)} -> total size of the allocated heap`,
heapUsed: `${formatMemoryUsage(
memoryData.heapUsed
)} -> actual memory used during the execution`,
external: `${formatMemoryUsage(
memoryData.external
)} -> V8 external memory`,
}
: `RSS memory ${description}: ${formatMemoryUsage(memoryData.rss)}${
oldRSS === 0
? ``
: `, changed by ` + (Memory.rss - oldRSS).toString() + ` MB`
}`;
console.log(memoryUsage);
}
}