aboutsummaryrefslogtreecommitdiffstats
path: root/packages/metacoin/test
diff options
context:
space:
mode:
Diffstat (limited to 'packages/metacoin/test')
-rw-r--r--packages/metacoin/test/global_hooks.ts7
-rw-r--r--packages/metacoin/test/metacoin_test.ts3
-rw-r--r--packages/metacoin/test/utils/profiler.ts27
-rw-r--r--packages/metacoin/test/utils/web3_wrapper.ts71
4 files changed, 95 insertions, 13 deletions
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<TransferContractEventArgs>;
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);