aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src/profiler_manager.ts
diff options
context:
space:
mode:
authorAlex Browne <stephenalexbrowne@gmail.com>2018-06-13 06:38:28 +0800
committerGitHub <noreply@github.com>2018-06-13 06:38:28 +0800
commitf50d3088dcc7fc7fb88ebfce397684b32aa037aa (patch)
treef49441f5d72f5e16dc8b29614bee05093e4e3151 /packages/sol-cov/src/profiler_manager.ts
parent787015f5370718e31c7990446fb1da298ed13e6b (diff)
parent627ea6c860e97a6e04877f10f9f842c5d5a907d6 (diff)
downloaddexon-sol-tools-f50d3088dcc7fc7fb88ebfce397684b32aa037aa.tar
dexon-sol-tools-f50d3088dcc7fc7fb88ebfce397684b32aa037aa.tar.gz
dexon-sol-tools-f50d3088dcc7fc7fb88ebfce397684b32aa037aa.tar.bz2
dexon-sol-tools-f50d3088dcc7fc7fb88ebfce397684b32aa037aa.tar.lz
dexon-sol-tools-f50d3088dcc7fc7fb88ebfce397684b32aa037aa.tar.xz
dexon-sol-tools-f50d3088dcc7fc7fb88ebfce397684b32aa037aa.tar.zst
dexon-sol-tools-f50d3088dcc7fc7fb88ebfce397684b32aa037aa.zip
Merge pull request #691 from 0xProject/fix/sol-cov-memory
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.ts67
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..88e183cbd 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 computeSingleTraceCoverageAsync(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();
}
}