Selaa lähdekoodia

memory监视模块重构完毕

龚成明 3 vuotta sitten
vanhempi
commit
5cda4a46b5
4 muutettua tiedostoa jossa 34 lisäystä ja 68 poistoa
  1. 4 26
      scripts/lp/lpGenerate.ts
  2. 3 21
      scripts/lp/lpMaintenance.ts
  3. 3 21
      scripts/path/level2Generate.ts
  4. 24 0
      utils/memory.ts

+ 4 - 26
scripts/lp/lpGenerate.ts

@@ -4,15 +4,14 @@ import contracts from "../../config/contracts";
 import logger from "../../utils/logger";
 import {BigNumber} from "ethers";
 import {replaceAll} from "hardhat/internal/util/strings";
-import swapPath from "../interface/swapPath";
 import history from "../interface/history";
-import {LpMaintenance} from "./lpMaintenance";
 import Time from "../../utils/time";
+import MemoryUtils from "../../utils/memory";
 const ierc20abi = require('../../abi/IERC20_ABI.json')
 
 export class LpGenerate {
   tokenInstance: any = {}
-  maxMemoryOfByte: number = 0
+  memoryUtils: MemoryUtils = new MemoryUtils()
 
   async handlePosition(routerObj: any) {
     const block = routerObj.type === 'univ2' ? 'UNIV2DEX' : 'UNIV3DEX'
@@ -161,16 +160,6 @@ export class LpGenerate {
     return router.positionManager
   }
 
-  async checkPosition(pm: any, position: any) {
-    try {
-      await pm.methods.positions(position).call()
-
-      return true
-    } catch (e) {
-      return false
-    }
-  }
-
   // 二分法查length
   async getPositionLength(pm: any, low: number) {
     let high = low + 8
@@ -207,7 +196,6 @@ export class LpGenerate {
       return 'ok'
     }
 
-    let count = 0
     while (true) {
       let checkRst = await check(checkNumber)
       // logger.debug(`[${low}, ${high}], c: ${checkNumber}, ${checkRst}. [${++count}]`)
@@ -276,23 +264,13 @@ export class LpGenerate {
       await this.saveToken(lp, 'token')
     }
 
-    const memoryUsage = process.memoryUsage()
-    if (this.maxMemoryOfByte < memoryUsage.rss) {
-      this.maxMemoryOfByte = memoryUsage.rss
-    }
     if (lp) {
-      logger.debug(`${position + 1} / ${pairsLength}, ${lp.name}-${lp.LP}-${routerObj.chain}`
-        + `  ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
+      this.memoryUtils.logWithMemoryOnMemoryChange(`${position + 1} / ${pairsLength}, ${lp.name}-${lp.LP}-${routerObj.chain}`)
     } else {
-      logger.debug(`lp get filed. ${position + 1} / ${pairsLength}, ${routerObj.name}-${routerObj.router}-${routerObj.chain}`
-        + `  ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
+      this.memoryUtils.logWithMemoryOnMemoryChange(`lp get filed. ${position + 1} / ${pairsLength}, ${routerObj.name}-${routerObj.router}-${routerObj.chain}`)
     }
   }
 
-  format (bytes: any) {
-    return (bytes / 1024 / 1024).toFixed(2) + ' MB';
-  }
-
   async run() {
     logger.debug('Pull lp start.')
 

+ 3 - 21
scripts/lp/lpMaintenance.ts

@@ -5,6 +5,7 @@ import logger from "../../utils/logger";
 import {BigNumber} from "ethers";
 import {replaceAll} from "hardhat/internal/util/strings";
 import Time from "../../utils/time";
+import MemoryUtils from "../../utils/memory";
 
 // 初始化410 v2工具箱
 const v2ToolBy410Abi = require('../../abi/410_V2_TOOLS.json')
@@ -19,8 +20,7 @@ const ethTokenAddressList = baseTokenAddressList.filter(baseTokenAddress => {
 
 export class LpMaintenance {
   tokenAssembly: any = {}
-  maxMemoryOfByte: number = 0
-  maxMemoryChanged: boolean = true
+  memoryUtils: MemoryUtils = new MemoryUtils()
 
   // 计算价格要用
   allMaxValueLpGroupBySum: any = {}
@@ -132,24 +132,6 @@ export class LpMaintenance {
     await this.checkTokenByAddress(lp.token1, tokenType)
   }
 
-  showMemory (mainInfo: string) {
-    const memoryUsage = process.memoryUsage()
-    if (this.maxMemoryOfByte < memoryUsage.rss) {
-      this.maxMemoryOfByte = memoryUsage.rss
-      this.maxMemoryChanged = true
-    }
-
-    if (this.maxMemoryChanged) {
-      logger.debug(`${mainInfo}  ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
-
-      this.maxMemoryChanged = false
-    }
-  }
-
-  format (bytes: any) {
-    return (bytes / 1024 / 1024).toFixed(2) + ' MB';
-  }
-
   generateAddressListByLpList (lpList: any) {
     const lpAddressList: any = []
 
@@ -269,7 +251,7 @@ export class LpMaintenance {
     while (true) {
       await Time.delay(12000)
 
-      this.showMemory('a loop...')
+      this.memoryUtils.logWithMemoryOnMemoryChange('a loop...')
 
       try {
         const topLpPullRst = await History.findByBlock('topLp')

+ 3 - 21
scripts/path/level2Generate.ts

@@ -1,11 +1,11 @@
 import logger from "../../utils/logger";
 import history from "../interface/history";
 import SwapPath from "../interface/swapPath";
+import MemoryUtils from "../../utils/memory";
 
 class Level2Generate {
-  maxMemoryOfByte: number = 0
-  maxMemoryChanged: boolean = true
   lpSumObj: any = {}
+  memoryUtils: MemoryUtils = new MemoryUtils()
   prevTypeLpListLength: number = 0
 
   buildLpObj (lpList: any) {
@@ -14,24 +14,6 @@ class Level2Generate {
     }
   }
 
-  showMemory (mainInfo: string) {
-    const memoryUsage = process.memoryUsage()
-    if (this.maxMemoryOfByte < memoryUsage.rss) {
-      this.maxMemoryOfByte = memoryUsage.rss
-      this.maxMemoryChanged = true
-    }
-
-    if (this.maxMemoryChanged) {
-      logger.debug(`${mainInfo}  ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
-
-      this.maxMemoryChanged = false
-    }
-  }
-
-  format (bytes: any) {
-    return (bytes / 1024 / 1024).toFixed(2) + ' MB';
-  }
-
   buildSumObjGroupBySum2(allTypeLpList: any) {
     // 根据sum值进行池子分类
     if (this.prevTypeLpListLength != allTypeLpList.length){
@@ -81,7 +63,7 @@ class Level2Generate {
   async run() {
     logger.debug('Level2 maintenance start.')
     while (true) {
-      this.showMemory('Level2 a loop...')
+      this.memoryUtils.logWithMemoryOnMemoryChange('Level2 a loop...')
 
       try {
         this.lpSumObj = {}

+ 24 - 0
utils/memory.ts

@@ -0,0 +1,24 @@
+import logger from "./logger";
+
+export default class MemoryUtils {
+  maxMemoryOfByte: number = 0
+  maxMemoryChanged: boolean = true
+
+  logWithMemoryOnMemoryChange (logInfo: string) {
+    const memoryUsage = process.memoryUsage()
+    if (this.maxMemoryOfByte < memoryUsage.rss) {
+      this.maxMemoryOfByte = memoryUsage.rss
+      this.maxMemoryChanged = true
+    }
+
+    if (this.maxMemoryChanged) {
+      logger.debug(`${logInfo}  ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
+
+      this.maxMemoryChanged = false
+    }
+  }
+
+  format (bytes: any) {
+    return (bytes / 1024 / 1024).toFixed(2) + ' MB';
+  }
+}