diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-06-09 02:18:32 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-06-09 05:56:45 +0800 |
commit | 760bab8f866ec3d5fc7627ce9bbf5c2eaaef1f36 (patch) | |
tree | 05911f7eaa9cd8a48448b371f46186d2ffc5541e /packages/contracts | |
parent | 817c332d11835f02726f0609374d1c25c9ab39b5 (diff) | |
download | dexon-0x-contracts-760bab8f866ec3d5fc7627ce9bbf5c2eaaef1f36.tar.gz dexon-0x-contracts-760bab8f866ec3d5fc7627ce9bbf5c2eaaef1f36.tar.zst dexon-0x-contracts-760bab8f866ec3d5fc7627ce9bbf5c2eaaef1f36.zip |
Implement SolidityProfiler & adapt sol-cov to work with Geth
Diffstat (limited to 'packages/contracts')
-rw-r--r-- | packages/contracts/coverage/.gitkeep | 0 | ||||
-rw-r--r-- | packages/contracts/package.json | 2 | ||||
-rw-r--r-- | packages/contracts/src/utils/coverage.ts | 3 | ||||
-rw-r--r-- | packages/contracts/src/utils/profiler.ts | 27 | ||||
-rw-r--r-- | packages/contracts/src/utils/web3_wrapper.ts | 22 | ||||
-rw-r--r-- | packages/contracts/test/global_hooks.ts | 5 |
6 files changed, 58 insertions, 1 deletions
diff --git a/packages/contracts/coverage/.gitkeep b/packages/contracts/coverage/.gitkeep deleted file mode 100644 index e69de29bb..000000000 --- a/packages/contracts/coverage/.gitkeep +++ /dev/null diff --git a/packages/contracts/package.json b/packages/contracts/package.json index b533a22ce..dcbeddbd8 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -18,6 +18,7 @@ "test": "yarn run_mocha", "rebuild_and_test": "run-s build test", "test:coverage": "SOLIDITY_COVERAGE=true run-s build run_mocha coverage:report:text coverage:report:lcov", + "test:profiler": "SOLIDITY_PROFILER=true run-s build run_mocha profiler:report:html", "run_mocha": "mocha --require source-map-support/register 'lib/test/**/*.js' --timeout 100000 --bail --exit", "compile": "sol-compiler", "clean": "shx rm -rf lib src/generated_contract_wrappers", @@ -26,6 +27,7 @@ "lint": "tslint --project . --exclude **/src/contract_wrappers/**/* --exclude **/lib/**/*", "coverage:report:text": "istanbul report text", "coverage:report:html": "istanbul report html && open coverage/index.html", + "profiler:report:html": "istanbul report html && open coverage/index.html", "coverage:report:lcov": "istanbul report lcov", "test:circleci": "yarn test" }, diff --git a/packages/contracts/src/utils/coverage.ts b/packages/contracts/src/utils/coverage.ts index 41c83f703..de29a3ecc 100644 --- a/packages/contracts/src/utils/coverage.ts +++ b/packages/contracts/src/utils/coverage.ts @@ -14,7 +14,8 @@ export const coverage = { _getCoverageSubprovider(): CoverageSubprovider { const defaultFromAddress = devConstants.TESTRPC_FIRST_ADDRESS; const solCompilerArtifactAdapter = new SolCompilerArtifactAdapter(); - const subprovider = new CoverageSubprovider(solCompilerArtifactAdapter, defaultFromAddress); + const isVerbose = true; + const subprovider = new CoverageSubprovider(solCompilerArtifactAdapter, defaultFromAddress, isVerbose); return subprovider; }, }; diff --git a/packages/contracts/src/utils/profiler.ts b/packages/contracts/src/utils/profiler.ts new file mode 100644 index 000000000..85ee24f22 --- /dev/null +++ b/packages/contracts/src/utils/profiler.ts @@ -0,0 +1,27 @@ +import { devConstants } from '@0xproject/dev-utils'; +import { ProfilerSubprovider, SolCompilerArtifactAdapter } from '@0xproject/sol-cov'; +import * as _ from 'lodash'; + +let profilerSubprovider: ProfilerSubprovider; + +export const profiler = { + start(): void { + profiler.getProfilerSubproviderSingleton().start(); + }, + stop(): void { + profiler.getProfilerSubproviderSingleton().stop(); + }, + getProfilerSubproviderSingleton(): ProfilerSubprovider { + if (_.isUndefined(profilerSubprovider)) { + profilerSubprovider = profiler._getProfilerSubprovider(); + } + return profilerSubprovider; + }, + _getProfilerSubprovider(): ProfilerSubprovider { + const defaultFromAddress = devConstants.TESTRPC_FIRST_ADDRESS; + const solCompilerArtifactAdapter = new SolCompilerArtifactAdapter(); + const isVerbose = true; + const subprovider = new ProfilerSubprovider(solCompilerArtifactAdapter, defaultFromAddress, isVerbose); + return subprovider; + }, +}; diff --git a/packages/contracts/src/utils/web3_wrapper.ts b/packages/contracts/src/utils/web3_wrapper.ts index df9bf88c8..c475d96a9 100644 --- a/packages/contracts/src/utils/web3_wrapper.ts +++ b/packages/contracts/src/utils/web3_wrapper.ts @@ -1,8 +1,10 @@ import { devConstants, env, EnvVars, web3Factory } from '@0xproject/dev-utils'; import { prependSubprovider } from '@0xproject/subproviders'; +import { logUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { coverage } from './coverage'; +import { profiler } from './profiler'; enum ProviderType { Ganache = 'ganache', @@ -45,9 +47,29 @@ const providerConfigs = testProvider === ProviderType.Ganache ? ganacheConfigs : export const provider = web3Factory.getRpcProvider(providerConfigs); const isCoverageEnabled = env.parseBoolean(EnvVars.SolidityCoverage); +const isProfilerEnabled = env.parseBoolean(EnvVars.SolidityProfiler); +if (isCoverageEnabled && isProfilerEnabled) { + throw new Error( + `Unfortunately for now you can't enable both coverage and profiler at the same time. They both use coverage.json file and there is no way to configure that.`, + ); +} if (isCoverageEnabled) { const coverageSubprovider = coverage.getCoverageSubproviderSingleton(); prependSubprovider(provider, coverageSubprovider); } +if (isProfilerEnabled) { + if (testProvider === ProviderType.Ganache) { + logUtils.warn( + "Gas costs in Ganache traces are incorrect and we don't recommend using it for profiling. Please switch to Geth", + ); + process.exit(1); + } + const profilerSubprovider = profiler.getProfilerSubproviderSingleton(); + logUtils.log( + "By default profilerSubprovider is stopped so that you don't get noise from setup code. Don't forget to start it before the code you want to profile and stop it afterwards", + ); + profilerSubprovider.stop(); + prependSubprovider(provider, profilerSubprovider); +} export const web3Wrapper = new Web3Wrapper(provider); diff --git a/packages/contracts/test/global_hooks.ts b/packages/contracts/test/global_hooks.ts index 8b48b030d..83263c5b3 100644 --- a/packages/contracts/test/global_hooks.ts +++ b/packages/contracts/test/global_hooks.ts @@ -1,10 +1,15 @@ import { env, EnvVars } from '@0xproject/dev-utils'; import { coverage } from '../src/utils/coverage'; +import { profiler } from '../src/utils/profiler'; after('generate coverage report', async () => { if (env.parseBoolean(EnvVars.SolidityCoverage)) { const coverageSubprovider = coverage.getCoverageSubproviderSingleton(); await coverageSubprovider.writeCoverageAsync(); } + if (env.parseBoolean(EnvVars.SolidityProfiler)) { + const profilerSubprovider = profiler.getProfilerSubproviderSingleton(); + await profilerSubprovider.writeProfilerOutputAsync(); + } }); |