diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-13 22:10:05 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-13 22:10:05 +0800 |
commit | b4fead9606ecc9655b2038fcef1c75217752f67d (patch) | |
tree | b363eee11085850328837004b41ff23c5d31efb6 /packages/sol-cov/src/profiler_manager.ts | |
parent | 61243b418e4d962cd8d8a1d7a49f04510b3c1c7f (diff) | |
parent | 4efd28c092e74b438d0397069c0c55cc90c537f2 (diff) | |
download | dexon-sol-tools-b4fead9606ecc9655b2038fcef1c75217752f67d.tar dexon-sol-tools-b4fead9606ecc9655b2038fcef1c75217752f67d.tar.gz dexon-sol-tools-b4fead9606ecc9655b2038fcef1c75217752f67d.tar.bz2 dexon-sol-tools-b4fead9606ecc9655b2038fcef1c75217752f67d.tar.lz dexon-sol-tools-b4fead9606ecc9655b2038fcef1c75217752f67d.tar.xz dexon-sol-tools-b4fead9606ecc9655b2038fcef1c75217752f67d.tar.zst dexon-sol-tools-b4fead9606ecc9655b2038fcef1c75217752f67d.zip |
Merge branch 'v2-prototype' into feature/combinatorial-testing
* v2-prototype: (26 commits)
Rename _coverageCollector -> _profilerCollector in TraceCollectionSubprovider
Refactor sol-cov to de-duplicate code for coverage and profiling
Rename popByte and popAddress
Hard code test addresses/bytes32 instead of generating pseudorandom ones
Update artifacts
Rename computeCoverageAsync -> computeSingleTraceCoverageAsync
Fix linter errors
Refactor sol-cov to avoid keeping traceInfo in memory
Unpop byte rather than making deep copy
Pass gas in to marketBuyOrdersNoThrow
Looks up the memory location of makerAssetData/takerAssetData
Make ZRX_PROXY_ID constant rather than popping it from ZRX_ASSET_DATA
Add tests for deepCopyBytes and missing write methods from LibBytes
Pop id from assetData before dispatching to AssetProxies
Upgrade solidity-parser-entlr 0.2.11 => 0.2.12
Fix import order
Fix typos
Add CHANGELOGs
Speed-up sol-cov
Increase delay when sending transactions during devnet startup
...
Diffstat (limited to 'packages/sol-cov/src/profiler_manager.ts')
-rw-r--r-- | packages/sol-cov/src/profiler_manager.ts | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/packages/sol-cov/src/profiler_manager.ts b/packages/sol-cov/src/profiler_manager.ts deleted file mode 100644 index 0ab0ea544..000000000 --- a/packages/sol-cov/src/profiler_manager.ts +++ /dev/null @@ -1,134 +0,0 @@ -import { promisify } from '@0xproject/utils'; -import { stripHexPrefix } from 'ethereumjs-util'; -import * as fs from 'fs'; -import { Collector } from 'istanbul'; -import * as _ from 'lodash'; -import { getLogger, levels, Logger } from 'loglevel'; -import * as mkdirp from 'mkdirp'; - -import { AbstractArtifactAdapter } from './artifact_adapters/abstract_artifact_adapter'; -import { collectCoverageEntries } from './collect_coverage_entries'; -import { constants } from './constants'; -import { parseSourceMap } from './source_maps'; -import { - ContractData, - Coverage, - SingleFileSourceRange, - SourceRange, - Subtrace, - TraceInfo, - TraceInfoExistingContract, - TraceInfoNewContract, -} from './types'; -import { utils } from './utils'; - -const mkdirpAsync = promisify<undefined>(mkdirp); - -/** - * ProfilerManager is used by ProfilerSubprovider to profile code while running Solidity tests based on collected trace data. - * HACK: It's almost the exact copy of CoverageManager but instead of reporting how much times was each statement executed - it reports - how expensive it was gaswise. - */ -export class ProfilerManager { - private _artifactAdapter: AbstractArtifactAdapter; - private _logger: Logger; - private _traceInfos: TraceInfo[] = []; - /** - * Computed partial coverage for a single file & subtrace - * @param contractData Contract metadata (source, srcMap, bytecode) - * @param subtrace A subset of a transcation/call trace that was executed within that contract - * @param pcToSourceRange A mapping from program counters to source ranges - * @param fileIndex Index of a file to compute coverage for - * @return Partial istanbul coverage for that file & subtrace - */ - private static _getSingleFileCoverageForSubtrace( - contractData: ContractData, - subtrace: Subtrace, - pcToSourceRange: { [programCounter: number]: SourceRange }, - fileIndex: number, - ): Coverage { - const absoluteFileName = contractData.sources[fileIndex]; - const profilerEntriesDescription = collectCoverageEntries(contractData.sourceCodes[fileIndex]); - const gasConsumedByStatement: { [statementId: string]: number } = {}; - const statementIds = _.keys(profilerEntriesDescription.statementMap); - for (const statementId of statementIds) { - const statementDescription = profilerEntriesDescription.statementMap[statementId]; - const totalGasCost = _.sum( - _.map(subtrace, structLog => { - const sourceRange = pcToSourceRange[structLog.pc]; - if (_.isUndefined(sourceRange)) { - return 0; - } - if (sourceRange.fileName !== absoluteFileName) { - return 0; - } - if (utils.isRangeInside(sourceRange.location, statementDescription)) { - return structLog.gasCost; - } else { - return 0; - } - }), - ); - gasConsumedByStatement[statementId] = totalGasCost; - } - const partialProfilerOutput = { - [absoluteFileName]: { - ...profilerEntriesDescription, - path: absoluteFileName, - f: {}, // I's meaningless in profiling context - s: gasConsumedByStatement, - b: {}, // I's meaningless in profiling context - }, - }; - return partialProfilerOutput; - } - constructor(artifactAdapter: AbstractArtifactAdapter, isVerbose: boolean) { - this._artifactAdapter = artifactAdapter; - 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 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, - ); - for (let fileIndex = 0; fileIndex < contractData.sources.length; fileIndex++) { - const singleFileCoverageForTrace = ProfilerManager._getSingleFileCoverageForSubtrace( - contractData, - traceInfo.subtrace, - pcToSourceRange, - fileIndex, - ); - collector.add(singleFileCoverageForTrace); - } - } - return collector.getFinalCoverage(); - } -} |