codechecks.md

October 13, 2020 · View on GitHub

Guide to using Codechecks

This reporter comes with a codechecks CI integration that displays a pull request's gas consumption changes relative to its target branch in the Github UI. It's like coveralls for gas. The codechecks service is free for open source and maintained by MakerDao engineer @krzkaczor.

Screen Shot 2019-06-18 at 12 25 49 PM

Setup

  • Enable your project on codechecks.io. Check out the getting started guide. (All you really have to do is toggle your repo 'on' and copy-paste a token into your CI environment variables settings.)

  • Install the codechecks client library as a dev dependency:

npm install --save-dev @codechecks/client
  • Add a codechecks.yml to your project's root directory as below:
checks:
  - name: eth-gas-reporter/codechecks
  • Run codechecks as a step in your build
# CircleCI Example
steps:
  - checkout
  - run: npm install
  - run: npm test
  - run: npx codechecks

# Travis
script:
  - npm test
  - npx codechecks
  • You're done! :elephant:

Multiple reports (for different CI jobs)

For each report, create a codechecks.yml file, e.g

codechecks.testing.yml
codechecks.production.yml

Use the name option in your .yml config to individuate the report:

# codechecks.production.yml
checks:
  - name: eth-gas-reporter/codechecks
    options:
      name: production

When running codechecks as a command in CI, specify the relevant codechecks config .yml

production:
  docker:
    - image: circleci/node:10.13.0
  steps:
    - checkout
    - run: npm install
    - run: npm test
    - run: npx codechecks codechecks.production.yml

Failure thresholds

You can ask Codechecks to report the CI run as a failure by using the maxMethodDiff and maxDeploymentDiff reporter options. These set the integer percentage difference over which an increase in gas usage by any method (or deployment) is forbidden.

Examples

// truffle-config.js
mocha: {
  reporter: "eth-gas-reporter",
  reporterOptions: {
    maxMethodDiff: 25,
  }
}

// buidler.config.js
gasReporter: {
  maxMethodDiff: 25,
}

Codechecks is new :wrench:

Codechecks is new and some of its quirks are still being ironed out:

  • If you're using CircleCI and the report seems to be missing from the first build of a pull request, you can configure your codechecks.yml's branch setting to make it work as expected.
  • Both Travis and Circle must be configured to run on commit/push (this is true by default and will only be a problem if you've turned those builds off to save resources.)

Diff Report Example

Something like this will be displayed in the checks tab of your GitHub pull request. Increases in gas usage relative to the PR's target branch are highlighted with a red cross, decreases are highlighted with a green check.

Deployments

GasDiffDiff %Block %chf avg cost
ConvertLib111,791001.4 %0.48
EtherRouter278,020003.5 %1.20
Factory324,331passed-14,2224%4.1 %1.40
MetaCoin358,572failed+73,53426%4.5 %1.55
MultiContractFileA90,745001.1 %0.39
MultiContractFileB90,745001.1 %0.39
Resolver430,580005.4 %1.86
VariableConstructor1,001,8900012.5 %4.34
VariableCosts930,5280011.6 %4.03
VersionA88,665001.1 %0.38
Wallet217,795002.7 %0.94

Methods

GasDiffDiff %Callschf avg cost
EtherRouter
       setResolver43,1920010.19
Factory
       deployVersionB107,1230010.46
MetaCoin
       sendCoin51,0190010.22
MultiContractFileA
       hello41,4190010.18
MultiContractFileB
       goodbye41,4190010.18
Resolver
       register37,6330020.16
VariableCosts
       addToMap90,3410070.39
       methodThatThrows41,5990020.18
       otherContractMethod57,4070010.25
       removeFromMap36,4810080.16
       sendPayment32,3350010.14
       setString28,787passed-21568%40.12
       transferPayment32,1860010.14
VersionA
       setValue25,6630010.11
VersionB
       setValue25,6850010.11
Wallet
       sendPayment32,1810010.14
       transferPayment32,1640010.14

Build Configuration

OptionSettings
solc: version0.5.0+commit.1d4f565a
solc: optimizedfalse
solc: runs200
gas: block limit8,000,000
gas: price21 gwei/gas
gas: currency/eth rate206.15 chf/eth

Gas Reporter JSON output

The gas reporter now writes the data it collects as JSON to a file at ./gasReporterOutput.json whenever the environment variable CI is set to true. You can see an example of this output here. You may find it useful as a base to generate more complex or long running gas analyses, develop CI integrations with, or make nicer tables.