Skip to content

Commit

Permalink
Web3 v4 benchmarking (#6649)
Browse files Browse the repository at this point in the history
  • Loading branch information
avkos authored Dec 16, 2023
1 parent c097b9a commit 6b2dbe3
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 21 deletions.
83 changes: 73 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- run: yarn prebuild
- run: yarn build:cjs
- run: tar -czf /tmp/web3-${{ matrix.node }}.js.tar.gz --exclude="./.git" ./
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp/web3-${{ matrix.node }}.js.tar.gz
Expand All @@ -42,7 +42,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
Expand All @@ -59,7 +59,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
Expand All @@ -73,7 +73,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-18.js.tar.gz
path: /tmp
Expand All @@ -88,7 +88,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-18.js.tar.gz
path: /tmp
Expand Down Expand Up @@ -128,7 +128,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
Expand Down Expand Up @@ -158,7 +158,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
Expand All @@ -183,7 +183,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
Expand All @@ -206,7 +206,7 @@ jobs:
node-version: ${{ matrix.node }}
- uses: browser-actions/setup-firefox@latest
if: matrix.browser == 'firefox'
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
Expand All @@ -230,9 +230,72 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
- run: tar -xf /tmp/web3-${{ matrix.node }}.js.tar.gz -C ./
- run: yarn build:docs

benchmark:
name: Benchmark Tests
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
node: [ 18 ]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- uses: actions/download-artifact@v4
with:
name: web3-${{ matrix.node }}.js.tar.gz
path: /tmp
- run: tar -xf /tmp/web3-${{ matrix.node }}.js.tar.gz -C ./
# @octokit/core not supported on node 16, so I can't add it to the package.json
- run: npm install --no-package-lock --no-save --force @octokit/core
- name: Restore main branch benchmark data
uses: actions/cache/restore@v3
with:
path: web3-benchmark-main.json
key: ${{ runner.os }}-web3-benchmark-main.json
- run: yarn test:benchmark
- name: Compare benchmark result and make comment
uses: benchmark-action/github-action-benchmark@v1
with:
# What benchmark tool the output.txt came from
tool: 'benchmarkjs'
# Where the output from the benchmark tool is stored
output-file-path: benchmark-data.txt
# Where the previous data file is stored
external-data-json-path: web3-benchmark-main.json
# Workflow will fail when an alert happens
fail-on-alert: false
# GitHub API token to make a commit comment
github-token: ${{ secrets.GITHUB_TOKEN }}
# Enable alert commit comment
comment-always: true
save-data-file: false
# copy comment from commit to Pull Request
- run: node scripts/copyCommitCommentToPrComment.js ${{ secrets.GITHUB_TOKEN }} ${{github.event.pull_request.head.sha}} ${{github.event.number}}
- name: Compare benchmark result and fail if threshold is reached
uses: benchmark-action/github-action-benchmark@v1
with:
# What benchmark tool the output.txt came from
tool: 'benchmarkjs'
# Where the output from the benchmark tool is stored
output-file-path: benchmark-data.txt
# Where the previous data file is stored
external-data-json-path: web3-benchmark-main.json
# Workflow will fail when an alert happens
fail-on-alert: true
# Enable alert commit comment
alert-threshold: '100%'
comment-always: false
- name: Save main branch benchmark data
uses: actions/cache/save@v3
if: github.event_name == 'push' && github.ref == 'refs/heads/4.x'
with:
path: web3-benchmark-main.json
key: ${{ runner.os }}-web3-benchmark-main.json
4 changes: 2 additions & 2 deletions .github/workflows/deploy-docs.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Docs CloudFlare Deploy
on:
on:
push:
branches-ignore:
- "1.x"
Expand All @@ -25,4 +25,4 @@ jobs:
accountId: 2238a825c5aca59233eab1f221f7aefb
projectName: web3-js-docs
directory: ./docs/build
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ tmp/

# Incubed (in3) nodelist
packages/web3/.in3/

# benchmark results
benchmark-data.txt
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"test:coverage:integration": "lerna run test:coverage:integration --stream --parallel",
"test:unit": "lerna run test:unit --stream --parallel && jest --config=./scripts/jest.config.js",
"test:integration": "lerna run test:integration --stream",
"test:benchmark": "lerna run test:benchmark",
"test:integration:stress": "lerna run test:integration:stress --stream",
"test:e2e:ganache:http": "./scripts/test-runner.sh ganache http",
"test:e2e:ganache:ws": "./scripts/test-runner.sh ganache ws",
Expand Down Expand Up @@ -93,7 +94,7 @@
"test:blackbox:infura:http": "yarn pre-blackbox && ./scripts/verdaccio.sh startBackgroundAndPublish && lerna run test:blackbox:infura:http --stream && yarn post-blackbox",
"test:blackbox:infura:ws": "yarn pre-blackbox && ./scripts/verdaccio.sh startBackgroundAndPublish && lerna run test:blackbox:infura:ws --stream && yarn post-blackbox",
"test:manual:long-connection-ws": "node packages/web3/test/stress/long_ws_tests/nodejs_test/long_connection_ws.js",
"test:stress":"yarn test:stress:geth:ws && yarn test:stress:geth:http && yarn test:stress:geth:ipc && yarn test:e2e:stress:geth:ws:chrome",
"test:stress": "yarn test:stress:geth:ws && yarn test:stress:geth:http && yarn test:stress:geth:ipc && yarn test:e2e:stress:geth:ws:chrome",
"husky:install": "husky install",
"husky:uninstall": "husky uninstall",
"postinstall": "yarn build",
Expand Down
3 changes: 1 addition & 2 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import {
ContractAbiWithSignature,
ContractOptions,
} from 'web3-types';
import { format, isDataFormat, keccak256, toChecksumAddress } from 'web3-utils';
import { format, isDataFormat, keccak256, toChecksumAddress , isContractInitOptions } from 'web3-utils';
import {
isNullish,
validator,
Expand All @@ -106,7 +106,6 @@ import {
getEstimateGasParams,
getEthTxCallParams,
getSendTxParams,
isContractInitOptions,
isWeb3ContractContext,
} from './utils.js';

Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth-contract/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ export * from './encoding.js';
export * from './contract.js';
export * from './log_subscription.js';
export * from './types.js';
export * from './utils.js';

export default Contract;
9 changes: 4 additions & 5 deletions packages/web3-eth-contract/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const dataInputEncodeMethodHelper = (
if (isNullish(tx.input) && isNullish(tx.data)) {
tx[dataInputFill as 'data' | 'input'] = encodeMethodABI(abi, params);
}

return { data: tx.data as HexString, input: tx.input as HexString };
};

Expand Down Expand Up @@ -164,11 +163,11 @@ export const getEstimateGasParams = ({
return txParams as TransactionWithSenderAPI;
};

export { isContractInitOptions } from 'web3-utils';

export const isWeb3ContractContext = (options: unknown): options is Web3ContractContext =>
typeof options === 'object' && !isNullish(options) &&
Object.keys(options).length !== 0 && !isContractInitOptions(options);
typeof options === 'object' &&
!isNullish(options) &&
Object.keys(options).length !== 0 &&
!isContractInitOptions(options);

export const getCreateAccessListParams = ({
abi,
Expand Down
5 changes: 4 additions & 1 deletion packages/web3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@
"test:blackbox:geth:ws": "./scripts/black_box_test.sh geth ws",
"test:blackbox:infura:http": "./scripts/black_box_test.sh infura http",
"test:blackbox:infura:ws": "./scripts/black_box_test.sh infura ws",
"test:e2e:chrome:stress": "npx cypress run --headless --browser chrome"
"test:e2e:chrome:stress": "npx cypress run --headless --browser chrome",
"test:benchmark": "npx ts-node --preferTsExts ../web3/test/benchmark/benchmark.ts"
},
"devDependencies": {
"@truffle/hdwallet-provider": "^2.0.12",
"@types/benchmark": "^2.1.5",
"@types/jest": "^28.1.6",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"benchmark": "^2.1.4",
"eslint": "^8.20.0",
"eslint-config-base-web3": "0.1.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
28 changes: 28 additions & 0 deletions packages/web3/test/benchmark/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import { decodeParameters, encodeParameters } from 'web3-eth-abi';

export const abiDecode = async () => {
return decodeParameters(
['uint256', 'string'],
'0x000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000',
);
};
export const abiEncode = async () => {
return encodeParameters(['uint256', 'string'], ['2345675643', 'Hello!%']);
};
47 changes: 47 additions & 0 deletions packages/web3/test/benchmark/benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import Benchmark from 'benchmark';
import fs from 'fs';
import path from 'path';

import { processingTx } from './processingTx';
import {
processingContractDeploy,
processingContractMethodSend,
processingContractMethodCall,
} from './processingContract';
import { abiEncode, abiDecode } from './abi';
import { sign, verify } from './wallet';

const suite = new Benchmark.Suite();
const results: any[] = [];
suite
.add('processingTx', processingTx)
.add('processingContractDeploy', processingContractDeploy)
.add('processingContractMethodSend', processingContractMethodSend)
.add('processingContractMethodCall', processingContractMethodCall)
.add('abiEncode', abiEncode)
.add('abiDecode', abiDecode)
.add('sign', sign)
.add('verify', verify)
.on('cycle', (event: any) => {
results.push(String(event.target));
})
.run({ async: true })
.on('complete', () => {
fs.writeFileSync(path.join('..', '..', 'benchmark-data.txt'), results.join('\n'));
});
23 changes: 23 additions & 0 deletions packages/web3/test/benchmark/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import { Web3Context } from 'web3-core';

export const context = new Web3Context({
// eslint-disable-next-line @typescript-eslint/no-empty-function
request: () => {},
});
Loading

0 comments on commit 6b2dbe3

Please sign in to comment.