From 83ddaccf4a7c855ed726399cb587ec7e1abe28f4 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 11 Jun 2018 11:59:48 -0700 Subject: Add profiler and geth tests to metacoin --- packages/metacoin/README.md | 97 ++++++++++++++++++++++++++++ packages/metacoin/package.json | 3 +- packages/metacoin/test/global_hooks.ts | 7 +- packages/metacoin/test/metacoin_test.ts | 3 + packages/metacoin/test/utils/profiler.ts | 27 ++++++++ packages/metacoin/test/utils/web3_wrapper.ts | 71 ++++++++++++++++---- 6 files changed, 194 insertions(+), 14 deletions(-) create mode 100644 packages/metacoin/README.md create mode 100644 packages/metacoin/test/utils/profiler.ts (limited to 'packages') diff --git a/packages/metacoin/README.md b/packages/metacoin/README.md new file mode 100644 index 000000000..8f45154d7 --- /dev/null +++ b/packages/metacoin/README.md @@ -0,0 +1,97 @@ +## @0xproject/metacoin + +This is a small example project that server as a showroom/polygon for 0x dev tools. +It suppports: + +* Compiling & testing smart contracts +* Typed contract wrappers +* Coverage +* Profiling +* Running tests against Ganache +* Running tests against our fork of Geth that supports snapshots & time travel + +## Contributing + +We welcome improvements and fixes from the wider community! To report bugs within this package, please create an issue in this repository. + +Please read our [contribution guidelines](../../CONTRIBUTING.md) before getting started. + +### Install dependencies + +If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them: + +```bash +yarn config set workspaces-experimental true +``` + +Then install dependencies + +```bash +yarn install +``` + +### Build + +To build this package and all other monorepo packages that it depends on, run the following from the monorepo root directory: + +```bash +PKG=@0xproject/metacoin yarn build +``` + +Or continuously rebuild on change: + +```bash +PKG=@0xproject/metacoin yarn watch +``` + +### Clean + +```bash +yarn clean +``` + +### Lint + +```bash +yarn lint +``` + +### Test providers + +By default tests run against an in-process Ganache instance. If you want to use Geth you'll need to [start a Geth dev node](https://github.com/0xProject/0x-monorepo/blob/v2-prototype/packages/devnet/README.md) first. + +```bash +cd ../devnet +docker build -t 0x-devnet . +docker run -it --rm -p 8501:8501 0x-devnet +``` + +This Geth version supports snapshots and time travel. Then - run your tests against it. + +``` +TEST_PROVIDER=geth yarn test +``` + +### Coverage + +```bash +yarn test:coverage +yarn coverage:report:html +``` + +### Profiling + +Please note that traces emmited by ganache have incorrect gas costs so we recommend using Geth for profiling. + +```bash +TEST_PROVIDER=geth yarn test:profile +``` + +You'll see a warning that you need to explicitly enable and disable the profiler before and after the block of code you want to profile. + +```typescript +import { profiler } from './utils/profiler'; +profiler.start(); +// Some solidity stuff +profiler.stop(); +``` diff --git a/packages/metacoin/package.json b/packages/metacoin/package.json index 335bf66ac..9b706fa09 100644 --- a/packages/metacoin/package.json +++ b/packages/metacoin/package.json @@ -7,7 +7,6 @@ "private": true, "description": "Example solidity project using 0x dev tools", "scripts": { - "watch": "tsc -w", "lint": "tslint --project . --exclude **/src/contract_wrappers/**/*", "watch_without_deps": "yarn pre_build && tsc -w", "build": "yarn pre_build && tsc", @@ -17,10 +16,12 @@ "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:profile": "SOLIDITY_PROFILER=true run-s build run_mocha profiler:report:html", "run_mocha": "mocha --require source-map-support/register lib/test/**/*_test.js lib/test/global_hooks.js --bail --exit --timeout 10000", "generate_contract_wrappers": "abi-gen --abis 'artifacts/Metacoin.json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/contract_wrappers --backend ethers", "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:coverage", "compile": "sol-compiler compile" diff --git a/packages/metacoin/test/global_hooks.ts b/packages/metacoin/test/global_hooks.ts index 509dc6837..4ab2ddc2f 100644 --- a/packages/metacoin/test/global_hooks.ts +++ b/packages/metacoin/test/global_hooks.ts @@ -1,10 +1,15 @@ import { env, EnvVars } from '@0xproject/dev-utils'; import { coverage } from './utils/coverage'; +import { profiler } from './utils/profiler'; -after('generate coverage report', async () => { +after('generate coverage & profiler 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(); + } }); diff --git a/packages/metacoin/test/metacoin_test.ts b/packages/metacoin/test/metacoin_test.ts index 09e0d6b36..6b21c1a22 100644 --- a/packages/metacoin/test/metacoin_test.ts +++ b/packages/metacoin/test/metacoin_test.ts @@ -10,6 +10,7 @@ import { MetacoinContract, TransferContractEventArgs } from '../src/contract_wra import { chaiSetup } from './utils/chai_setup'; import { config } from './utils/config'; +import { profiler } from './utils/profiler'; import { provider, web3Wrapper } from './utils/web3_wrapper'; const artifact: ContractArtifact = MetacoinArtifact as any; @@ -44,6 +45,7 @@ describe('Metacoin', () => { const amount = INITIAL_BALANCE.div(2); const oldBalance = await metacoin.balances.callAsync(ZERO_ADDRESS); expect(oldBalance).to.be.bignumber.equal(0); + // profiler.start(); const txHash = await metacoin.transfer1.sendTransactionAsync( { to: ZERO_ADDRESS, @@ -51,6 +53,7 @@ describe('Metacoin', () => { }, { from: devConstants.TESTRPC_FIRST_ADDRESS }, ); + // profiler.stop(); const txReceipt = await web3Wrapper.awaitTransactionSuccessAsync(txHash); const transferLogs = txReceipt.logs[0] as LogWithDecodedArgs; expect(transferLogs.args).to.be.deep.equal({ diff --git a/packages/metacoin/test/utils/profiler.ts b/packages/metacoin/test/utils/profiler.ts new file mode 100644 index 000000000..701337451 --- /dev/null +++ b/packages/metacoin/test/utils/profiler.ts @@ -0,0 +1,27 @@ +import { devConstants } from '@0xproject/dev-utils'; +import { ProfilerSubprovider, SolCompilerArtifactAdapter } from '@0xproject/sol-cov'; +import * as _ from 'lodash'; + +import { config } from './config'; + +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 zeroExArtifactsAdapter = new SolCompilerArtifactAdapter(config.artifactsDir, config.contractsDir); + return new ProfilerSubprovider(zeroExArtifactsAdapter, defaultFromAddress); + }, +}; diff --git a/packages/metacoin/test/utils/web3_wrapper.ts b/packages/metacoin/test/utils/web3_wrapper.ts index 273cdf961..d3ffa5f87 100644 --- a/packages/metacoin/test/utils/web3_wrapper.ts +++ b/packages/metacoin/test/utils/web3_wrapper.ts @@ -1,31 +1,78 @@ -import { env, EnvVars } from '@0xproject/dev-utils'; +import { devConstants, env, EnvVars } from '@0xproject/dev-utils'; import { GanacheSubprovider, prependSubprovider } from '@0xproject/subproviders'; +import { logUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as fs from 'fs'; import ProviderEngine = require('web3-provider-engine'); +import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); import { config } from './config'; import { coverage } from './coverage'; +import { profiler } from './profiler'; + +enum ProviderType { + Ganache = 'ganache', + Geth = 'geth', +} + +let testProvider: ProviderType; +switch (process.env.TEST_PROVIDER) { + case undefined: + testProvider = ProviderType.Ganache; + break; + case 'ganache': + testProvider = ProviderType.Ganache; + break; + case 'geth': + testProvider = ProviderType.Geth; + break; + default: + throw new Error(`Unknown TEST_PROVIDER: ${process.env.TEST_PROVIDER}`); +} export const provider = new ProviderEngine(); -provider.addProvider( - new GanacheSubprovider({ - logger: { - log: (arg: any) => { - fs.appendFileSync(config.ganacheLogFile, `${arg}\n`); +if (testProvider === ProviderType.Ganache) { + provider.addProvider( + new GanacheSubprovider({ + logger: { + log: (arg: any) => { + fs.appendFileSync(config.ganacheLogFile, `${arg}\n`); + }, }, - }, - verbose: env.parseBoolean(EnvVars.SolidityCoverage), - networkId: config.networkId, - mnemonic: config.mnemonic, - }), -); + verbose: env.parseBoolean(EnvVars.SolidityCoverage), + networkId: config.networkId, + mnemonic: config.mnemonic, + }), + ); +} else { + provider.addProvider(new RpcSubprovider({ rpcUrl: 'http://localhost:8501' })); +} provider.start(); 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. Check README for more details", + ); + 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); -- cgit v1.2.3 From 682f6d273c7ed4a53d02df0a33e07415836d5732 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 11 Jun 2018 12:49:46 -0700 Subject: Fix linter issues --- packages/metacoin/test/metacoin_test.ts | 2 +- packages/metacoin/test/utils/web3_wrapper.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'packages') diff --git a/packages/metacoin/test/metacoin_test.ts b/packages/metacoin/test/metacoin_test.ts index 6b21c1a22..22e63eead 100644 --- a/packages/metacoin/test/metacoin_test.ts +++ b/packages/metacoin/test/metacoin_test.ts @@ -10,7 +10,7 @@ import { MetacoinContract, TransferContractEventArgs } from '../src/contract_wra import { chaiSetup } from './utils/chai_setup'; import { config } from './utils/config'; -import { profiler } from './utils/profiler'; +// import { profiler } from './utils/profiler'; import { provider, web3Wrapper } from './utils/web3_wrapper'; const artifact: ContractArtifact = MetacoinArtifact as any; diff --git a/packages/metacoin/test/utils/web3_wrapper.ts b/packages/metacoin/test/utils/web3_wrapper.ts index d3ffa5f87..c09f8d2df 100644 --- a/packages/metacoin/test/utils/web3_wrapper.ts +++ b/packages/metacoin/test/utils/web3_wrapper.ts @@ -1,4 +1,4 @@ -import { devConstants, env, EnvVars } from '@0xproject/dev-utils'; +import { env, EnvVars } from '@0xproject/dev-utils'; import { GanacheSubprovider, prependSubprovider } from '@0xproject/subproviders'; import { logUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; -- cgit v1.2.3 From 534a0d68362abb5a7e643852015c3496bead705c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 11 Jun 2018 22:03:07 -0700 Subject: Fix typos --- packages/metacoin/README.md | 14 +++++++------- packages/metacoin/test/global_hooks.ts | 2 +- packages/metacoin/test/metacoin_test.ts | 1 + packages/metacoin/test/utils/web3_wrapper.ts | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) (limited to 'packages') diff --git a/packages/metacoin/README.md b/packages/metacoin/README.md index 8f45154d7..15816cc67 100644 --- a/packages/metacoin/README.md +++ b/packages/metacoin/README.md @@ -1,14 +1,14 @@ ## @0xproject/metacoin -This is a small example project that server as a showroom/polygon for 0x dev tools. -It suppports: +This is an example project that demonstrates how the many Ethereum dev tools developed by 0x can be used in any Solidity/TS project. +It supports: * Compiling & testing smart contracts -* Typed contract wrappers -* Coverage -* Profiling +* Generating typed contract wrappers +* Solidity coverage +* Solidity gas profiling * Running tests against Ganache -* Running tests against our fork of Geth that supports snapshots & time travel +* Running tests against our fork of Geth (it supports snapshotting & time travel) ## Contributing @@ -81,7 +81,7 @@ yarn coverage:report:html ### Profiling -Please note that traces emmited by ganache have incorrect gas costs so we recommend using Geth for profiling. +Please note that traces emitted by ganache have incorrect gas costs so we recommend using Geth for profiling. ```bash TEST_PROVIDER=geth yarn test:profile diff --git a/packages/metacoin/test/global_hooks.ts b/packages/metacoin/test/global_hooks.ts index 4ab2ddc2f..7328531e4 100644 --- a/packages/metacoin/test/global_hooks.ts +++ b/packages/metacoin/test/global_hooks.ts @@ -3,7 +3,7 @@ import { env, EnvVars } from '@0xproject/dev-utils'; import { coverage } from './utils/coverage'; import { profiler } from './utils/profiler'; -after('generate coverage & profiler report', async () => { +after('generate coverage || profiler report', async () => { if (env.parseBoolean(EnvVars.SolidityCoverage)) { const coverageSubprovider = coverage.getCoverageSubproviderSingleton(); await coverageSubprovider.writeCoverageAsync(); diff --git a/packages/metacoin/test/metacoin_test.ts b/packages/metacoin/test/metacoin_test.ts index 22e63eead..78de57a2a 100644 --- a/packages/metacoin/test/metacoin_test.ts +++ b/packages/metacoin/test/metacoin_test.ts @@ -10,6 +10,7 @@ import { MetacoinContract, TransferContractEventArgs } from '../src/contract_wra import { chaiSetup } from './utils/chai_setup'; import { config } from './utils/config'; +// Comment out the next line enable profiling // import { profiler } from './utils/profiler'; import { provider, web3Wrapper } from './utils/web3_wrapper'; diff --git a/packages/metacoin/test/utils/web3_wrapper.ts b/packages/metacoin/test/utils/web3_wrapper.ts index c09f8d2df..9bc3d05ac 100644 --- a/packages/metacoin/test/utils/web3_wrapper.ts +++ b/packages/metacoin/test/utils/web3_wrapper.ts @@ -1,6 +1,6 @@ import { env, EnvVars } from '@0xproject/dev-utils'; import { GanacheSubprovider, prependSubprovider } from '@0xproject/subproviders'; -import { logUtils } from '@0xproject/utils'; +import { logUtils, errorUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as fs from 'fs'; import ProviderEngine = require('web3-provider-engine'); @@ -27,7 +27,7 @@ switch (process.env.TEST_PROVIDER) { testProvider = ProviderType.Geth; break; default: - throw new Error(`Unknown TEST_PROVIDER: ${process.env.TEST_PROVIDER}`); + throw errorUtils.spawnSwitchErr('TEST_PROVIDER', process.env.TEST_PROVIDER); } export const provider = new ProviderEngine(); -- cgit v1.2.3