Created
February 4, 2022 17:11
-
-
Save 0xmikko/e8f0d2c5902e6b9a81bbee0ee97f1831 to your computer and use it in GitHub Desktop.
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 childProcess from "child_process"; | |
import fs from "fs"; | |
const v1Metrics = "gasV1.json"; | |
const v2Metrics = "gasV2.json"; | |
export type GasMeasures = | |
| "openCreditAccount" | |
| "swapOnUniswapFastCheck" | |
| "swapOnUniswapFullCheck" | |
| "addCollateral" | |
| "increaseBorrow" | |
| "closeAccount" | |
| "repayAccount" | |
| "liquidateAccount" | |
| "totalLiquidation"; | |
export type GasUsageData = Record<GasMeasures, number>; | |
export interface GasUsageVersion extends GasUsageData { | |
version: number; | |
branch: string; | |
commit: string; | |
} | |
export class GasTest { | |
protected v1metrics: GasUsageData; | |
protected version: number; | |
protected branch: string; | |
protected commit: string; | |
protected gasUsageData: GasUsageData; | |
constructor(version: number) { | |
this.version = version; | |
// @ts-ignore | |
this.gasUsageData = {}; | |
if (version === 2) { | |
this.commit = childProcess | |
.execSync("git rev-parse HEAD") | |
.toString() | |
.trim(); | |
this.branch = childProcess | |
.execSync("git symbolic-ref -q HEAD") | |
.toString() | |
.replace("refs/heads/", "") | |
.trim(); | |
console.log("Reading v1 metrics"); | |
if (!fs.existsSync(v1Metrics)) | |
throw new Error(`Cant find v1metrics file: ${v1Metrics}`); | |
const v1metricsJSON = fs.readFileSync(v1Metrics, "utf-8"); | |
this.v1metrics = JSON.parse(v1metricsJSON); | |
} else { | |
console.log("Creating v1 metrics"); | |
} | |
console.log(this); | |
} | |
setUsage(measure: GasMeasures, value: number) { | |
console.log(`${measure}:`, value); | |
this.gasUsageData[measure] = value; | |
} | |
showDiff() { | |
if (this.version === 1) { | |
console.log("You are at version 1"); | |
return; | |
} | |
const diffTable = Object.fromEntries( | |
Object.keys(this.gasUsageData).map((key) => [ | |
key, | |
{ | |
v1: this.v1metrics[key as GasMeasures] || "-", | |
current: this.gasUsageData[key as GasMeasures] || "-", | |
diff: | |
this.v1metrics[key as GasMeasures] && | |
this.gasUsageData[key as GasMeasures] | |
? this.v1metrics[key as GasMeasures] - | |
this.gasUsageData[key as GasMeasures] | |
: "-", | |
}, | |
]) | |
); | |
console.table(diffTable); | |
} | |
saveData() { | |
if (this.version === 2) { | |
const measures: Array<GasUsageVersion> = !fs.existsSync(v2Metrics) | |
? [] | |
: JSON.parse(fs.readFileSync(v2Metrics, "utf-8")); | |
const currentMeasure: GasUsageVersion = { | |
version: this.version, | |
branch: this.branch, | |
commit: this.commit, | |
...this.gasUsageData, | |
}; | |
fs.writeFileSync( | |
v2Metrics, | |
JSON.stringify([currentMeasure, ...measures]) | |
); | |
} else { | |
fs.writeFileSync(v1Metrics, JSON.stringify(this.gasUsageData)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment