diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-13 03:42:14 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-13 03:42:14 +0800 |
commit | 33f066910021bc4969f4d564fc1648f6769ee3ec (patch) | |
tree | 8e02cd187b0d1d3e38c832a6e2861a1985820fcd /packages/sol-cov/src/profiler_manager.ts | |
parent | 787015f5370718e31c7990446fb1da298ed13e6b (diff) | |
download | dexon-sol-tools-33f066910021bc4969f4d564fc1648f6769ee3ec.tar dexon-sol-tools-33f066910021bc4969f4d564fc1648f6769ee3ec.tar.gz dexon-sol-tools-33f066910021bc4969f4d564fc1648f6769ee3ec.tar.bz2 dexon-sol-tools-33f066910021bc4969f4d564fc1648f6769ee3ec.tar.lz dexon-sol-tools-33f066910021bc4969f4d564fc1648f6769ee3ec.tar.xz dexon-sol-tools-33f066910021bc4969f4d564fc1648f6769ee3ec.tar.zst dexon-sol-tools-33f066910021bc4969f4d564fc1648f6769ee3ec.zip |
Refactor sol-cov to avoid keeping traceInfo in memory
Diffstat (limited to 'packages/sol-cov/src/profiler_manager.ts')
-rw-r--r-- | packages/sol-cov/src/profiler_manager.ts | 67 |
1 files changed, 29 insertions, 38 deletions
diff --git a/packages/sol-cov/src/profiler_manager.ts b/packages/sol-cov/src/profiler_manager.ts index 0ab0ea544..bec92f424 100644 --- a/packages/sol-cov/src/profiler_manager.ts +++ b/packages/sol-cov/src/profiler_manager.ts @@ -31,7 +31,8 @@ const mkdirpAsync = promisify<undefined>(mkdirp); export class ProfilerManager { private _artifactAdapter: AbstractArtifactAdapter; private _logger: Logger; - private _traceInfos: TraceInfo[] = []; + private _contractsData!: ContractData[]; + private _collector = new Collector(); /** * Computed partial coverage for a single file & subtrace * @param contractData Contract metadata (source, srcMap, bytecode) @@ -86,49 +87,39 @@ export class ProfilerManager { this._logger = getLogger('sol-cov'); this._logger.setLevel(isVerbose ? levels.TRACE : levels.ERROR); } - public appendTraceInfo(traceInfo: TraceInfo): void { - this._traceInfos.push(traceInfo); - } public async writeProfilerOutputAsync(): Promise<void> { - const finalCoverage = await this._computeCoverageAsync(); + const finalCoverage = this._collector.getFinalCoverage(); const stringifiedCoverage = JSON.stringify(finalCoverage, null, '\t'); await mkdirpAsync('coverage'); fs.writeFileSync('coverage/coverage.json', stringifiedCoverage); } - private async _computeCoverageAsync(): Promise<Coverage> { - const contractsData = await this._artifactAdapter.collectContractsDataAsync(); - const collector = new Collector(); - for (const traceInfo of this._traceInfos) { - const isContractCreation = traceInfo.address === constants.NEW_CONTRACT; - const bytecode = isContractCreation - ? (traceInfo as TraceInfoNewContract).bytecode - : (traceInfo as TraceInfoExistingContract).runtimeBytecode; - const contractData = utils.getContractDataIfExists(contractsData, bytecode); - if (_.isUndefined(contractData)) { - const errMsg = isContractCreation - ? `Unknown contract creation transaction` - : `Transaction to an unknown address: ${traceInfo.address}`; - this._logger.warn(errMsg); - continue; - } - const bytecodeHex = stripHexPrefix(bytecode); - const sourceMap = isContractCreation ? contractData.sourceMap : contractData.sourceMapRuntime; - const pcToSourceRange = parseSourceMap( - contractData.sourceCodes, - sourceMap, - bytecodeHex, - contractData.sources, + public async computeCoverageAsync(traceInfo: TraceInfo): Promise<void> { + if (_.isUndefined(this._contractsData)) { + this._contractsData = await this._artifactAdapter.collectContractsDataAsync(); + } + const isContractCreation = traceInfo.address === constants.NEW_CONTRACT; + const bytecode = isContractCreation + ? (traceInfo as TraceInfoNewContract).bytecode + : (traceInfo as TraceInfoExistingContract).runtimeBytecode; + const contractData = utils.getContractDataIfExists(this._contractsData, bytecode); + if (_.isUndefined(contractData)) { + const errMsg = isContractCreation + ? `Unknown contract creation transaction` + : `Transaction to an unknown address: ${traceInfo.address}`; + this._logger.warn(errMsg); + return; + } + const bytecodeHex = stripHexPrefix(bytecode); + const sourceMap = isContractCreation ? contractData.sourceMap : contractData.sourceMapRuntime; + const pcToSourceRange = parseSourceMap(contractData.sourceCodes, sourceMap, bytecodeHex, contractData.sources); + for (let fileIndex = 0; fileIndex < contractData.sources.length; fileIndex++) { + const singleFileCoverageForTrace = ProfilerManager._getSingleFileCoverageForSubtrace( + contractData, + traceInfo.subtrace, + pcToSourceRange, + fileIndex, ); - for (let fileIndex = 0; fileIndex < contractData.sources.length; fileIndex++) { - const singleFileCoverageForTrace = ProfilerManager._getSingleFileCoverageForSubtrace( - contractData, - traceInfo.subtrace, - pcToSourceRange, - fileIndex, - ); - collector.add(singleFileCoverageForTrace); - } + this._collector.add(singleFileCoverageForTrace); } - return collector.getFinalCoverage(); } } |