Skip to content

Commit

Permalink
feat(gas-report): create package, move relevant files to it (#1147)
Browse files Browse the repository at this point in the history
* feat(gas-reporter): create package, move relevant files to it

* make gas-report a default command for mud-gas-report

* refactor(store): use gas-reporter

* refactor(world): use gas-reporter

* rename gas-reporter to gas-report

* use GasReporter in testEncodeDecodeSchema

* actions: install again after build

* gas-report readme

* Create quick-numbers-flash.md

* update playwright

* Revert "update playwright"

This reverts commit c9f0d4b.
  • Loading branch information
dk1a authored Jul 12, 2023
1 parent c6801b8 commit 66cc35a
Show file tree
Hide file tree
Showing 47 changed files with 298 additions and 50 deletions.
7 changes: 7 additions & 0 deletions .changeset/quick-numbers-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@latticexyz/cli": minor
"@latticexyz/gas-report": minor
"@latticexyz/store": minor
---

Create gas-report package, move gas-report cli command and GasReporter contract to it
4 changes: 4 additions & 0 deletions .github/actions/build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ runs:
shell: bash
run: pnpm turbo run build --cache-dir=.turbo --concurrency 10

- name: Install node dependencies again to update workspace binaries
shell: bash
run: pnpm install --frozen-lockfile

- name: Outdated files, run `pnpm build` and commit them
uses: ./.github/actions/require-empty-diff
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@improbable-eng/grpc-web-node-http-transport": "^0.15.0",
"@latticexyz/common": "workspace:*",
"@latticexyz/config": "workspace:*",
"@latticexyz/gas-report": "workspace:*",
"@latticexyz/schema-type": "workspace:*",
"@latticexyz/services": "workspace:*",
"@latticexyz/solecs": "workspace:*",
Expand All @@ -51,8 +52,6 @@
"nice-grpc-web": "^2.0.1",
"openurl": "^1.1.1",
"path": "^0.12.7",
"strip-ansi": "^7.1.0",
"table": "^6.8.1",
"throttle-debounce": "^5.0.0",
"typechain": "^8.1.1",
"typescript": "^4.9.5",
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CommandModule } from "yargs";

import gasReport from "@latticexyz/gas-report";

import devnode from "./devnode";
import faucet from "./faucet";
import gasReport from "./gas-report";
import hello from "./hello";
import tablegen from "./tablegen";
import tsgen from "./tsgen";
Expand All @@ -18,7 +19,7 @@ export const commands: CommandModule<any, any>[] = [
deploy,
devnode,
faucet,
gasReport,
gasReport as CommandModule,
hello,
tablegen,
tsgen,
Expand Down
11 changes: 11 additions & 0 deletions packages/gas-report/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cache
abi
out
docs
_docs
DOCS.md
artifacts
yarn-error.log
API
dist
types/ethers-contracts
9 changes: 9 additions & 0 deletions packages/gas-report/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*

!abi/**
!src/**
!ts/**
!types/**
!package.json
!README.md
!dist/**
8 changes: 8 additions & 0 deletions packages/gas-report/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", ">=0.8.0"],
"avoid-low-level-calls": "off",
"func-visibility": ["warn", { "ignoreConstructors": true }]
}
}
4 changes: 4 additions & 0 deletions packages/gas-report/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
26 changes: 26 additions & 0 deletions packages/gas-report/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Gas Report

Gas reporter for specific lines within forge tests

Add some reports to your forge tests

```solidity
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
contract ExampleTest is Test, GasReporter {
function testGas() public {
startGasReport("Describe what is being gas-reported on");
// do something here
endGasReport();
}
}
```

Then use the cli command to run tests and save the report:

```console
pnpm mud-gas-report --save gas-report.json
```

Run `pnpm mud-gas-report --help` for more details.
11 changes: 11 additions & 0 deletions packages/gas-report/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[profile.default]
solc_version = '0.8.13'
ffi = false
fuzz_runs = 256
optimizer = true
optimizer_runs = 3000
verbosity = 2
allow_paths= ["../../node_modules"]
src = "src"
out = "out"
bytecode_hash = "none"
49 changes: 49 additions & 0 deletions packages/gas-report/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@latticexyz/gas-report",
"version": "1.42.0",
"description": "Gas reporter for specific lines within forge tests",
"repository": {
"type": "git",
"url": "https://github.com/latticexyz/mud.git",
"directory": "packages/gas-report"
},
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/index.js"
},
"typesVersions": {
"*": {
"index": [
"./ts/index.ts"
]
}
},
"bin": {
"mud-gas-report": "./dist/mud-gas-report.js"
},
"scripts": {
"build": "tsup",
"clean": "rimraf dist",
"dev": "tsup --watch",
"test": "vitest typecheck --run --passWithNoTests && vitest --run --passWithNoTests && forge test"
},
"dependencies": {
"chalk": "^5.3.0",
"dotenv": "^16.0.3",
"execa": "^7.0.0",
"strip-ansi": "^7.1.0",
"table": "^6.8.1",
"yargs": "^17.7.1"
},
"devDependencies": {
"@types/node": "^18.15.11",
"@types/yargs": "^17.0.10",
"ds-test": "https://github.com/dapphub/ds-test.git#c9ce3f25bde29fc5eb9901842bf02850dfd2d084",
"forge-std": "https://github.com/foundry-rs/forge-std.git#b4f121555729b3afb3c5ffccb62ff4b6e2818fd3",
"rimraf": "^3.0.2",
"tsup": "^6.7.0",
"vitest": "0.31.4"
},
"gitHead": "914a1e0ae4a573d685841ca2ea921435057deb8f"
}
2 changes: 2 additions & 0 deletions packages/gas-report/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ds-test/=node_modules/ds-test/src/
forge-std/=node_modules/forge-std/src/
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "../src/test/GasReporter.sol";
import { GasReporter } from "../src/GasReporter.sol";

contract GasReporterTest is Test, GasReporter {
function testGasReporterGas() public {
Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions packages/gas-report/ts/mud-gas-report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import gasReport from ".";

// Load .env file into process.env
import * as dotenv from "dotenv";
import chalk from "chalk";
dotenv.config();

// $0 makes this a default command (as opposed to a sub-command),
// which replaces `mud-gas-report gas-report` with just `mud-gas-report`
gasReport.command = "$0";

yargs(hideBin(process.argv))
.scriptName("mud-gas-report")
// Use the commands directory to scaffold
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- command array overload isn't typed, see https://github.com/yargs/yargs/blob/main/docs/advanced.md#esm-hierarchy
.command(gasReport as any)
// Enable strict mode.
.strict()
// Custom error handler
.fail((msg, err) => {
console.error(chalk.red(msg));
if (msg.includes("Missing required argument")) {
console.log(
chalk.yellow(`Run 'pnpm mud ${process.argv[2]} --help' for a list of available and required arguments.`)
);
}

process.exit(1);
})
// Useful aliases.
.alias({ h: "help" }).argv;
20 changes: 20 additions & 0 deletions packages/gas-report/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
"target": "es2021",
"types": ["node"],
"module": "esnext",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"rootDir": ".",
"outDir": "dist",
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["ts"]
}
11 changes: 11 additions & 0 deletions packages/gas-report/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["ts/index.ts", "ts/mud-gas-report.ts"],
target: "esnext",
format: ["esm"],
dts: false,
sourcemap: true,
clean: true,
minify: true,
});
6 changes: 6 additions & 0 deletions packages/store/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
"name": "get total of PackedCounter",
"gasUsed": 27
},
{
"file": "test/Schema.t.sol",
"test": "testEncodeDecodeSchema",
"name": "encode schema with 6 entries [SchemaLib.encode]",
"gasUsed": 6040
},
{
"file": "test/Schema.t.sol",
"test": "testEncodeDecodeSchema",
Expand Down
3 changes: 2 additions & 1 deletion packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"clean:typechain": "rimraf types",
"dev": "tsup --watch",
"docs": "rimraf API && hardhat docgen",
"gas-report": "../cli/dist/mud.js gas-report --save gas-report.json",
"gas-report": "mud-gas-report --save gas-report.json",
"generate-tightcoder": "tsx ./scripts/generate-tightcoder.ts && prettier --write '**/tightcoder/*.sol'",
"lint": "solhint --config ./.solhint.json 'src/**/*.sol'",
"test": "vitest typecheck --run && vitest --run --passWithNoTests && forge test"
Expand All @@ -56,6 +56,7 @@
"@ethersproject/providers": "^5.7.2",
"@latticexyz/common": "workspace:*",
"@latticexyz/config": "workspace:*",
"@latticexyz/gas-report": "workspace:*",
"@latticexyz/schema-type": "workspace:*",
"abitype": "0.8.7",
"ethers": "^5.7.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/Bytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Bytes } from "../src/Bytes.sol";

contract BytesTest is Test, GasReporter {
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/Gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test, console } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Bytes } from "../src/Bytes.sol";
import { SliceLib } from "../src/Slice.sol";
import { EncodeArray } from "../src/tightcoder/EncodeArray.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/KeyEncoding.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { KeyEncoding, KeyEncodingTableId } from "../src/codegen/Tables.sol";
import { ExampleEnum } from "../src/codegen/Types.sol";
import { StoreCore } from "../src/StoreCore.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/Mixed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { Mixed, MixedData, MixedTableId } from "../src/codegen/Tables.sol";
import { StoreCore } from "../src/StoreCore.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/PackedCounter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { PackedCounter, PackedCounterLib } from "../src/PackedCounter.sol";

contract PackedCounterTest is Test, GasReporter {
Expand Down
7 changes: 3 additions & 4 deletions packages/store/test/Schema.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
pragma solidity >=0.8.0;

import { Test, console } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { Schema, SchemaLib } from "../src/Schema.sol";

// TODO add tests for all schema types
contract SchemaTest is Test, GasReporter {
function testEncodeDecodeSchema() public {
uint256 gas = gasleft();
startGasReport("encode schema with 6 entries [SchemaLib.encode]");
Schema schema = SchemaLib.encode(
SchemaType.UINT8, // 1 byte
SchemaType.UINT16, // 2 bytes
Expand All @@ -18,8 +18,7 @@ contract SchemaTest is Test, GasReporter {
SchemaType.UINT256, // 32 bytes
SchemaType.UINT32_ARRAY // 0 bytes (because it's dynamic)
);
gas = gas - gasleft();
console.log("GAS REPORT: encode schema with 6 entries [SchemaLib.encode]: %s", gas);
endGasReport();

startGasReport("get schema type at index");
SchemaType schemaType1 = schema.atIndex(0);
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/Slice.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Memory } from "../src/Memory.sol";
import { Slice, SliceLib } from "../src/Slice.sol";

Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/Storage.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Storage } from "../src/Storage.sol";
import { Utils } from "../src/Utils.sol";
import { Bytes } from "../src/Bytes.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/StoreCoreDynamic.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test, console } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { SliceLib } from "../src/Slice.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/StoreCoreGas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/std-contracts/src/test/GasReporter.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
import { StoreCore, StoreCoreInternal } from "../src/StoreCore.sol";
import { Utils } from "../src/Utils.sol";
Expand Down
Loading

0 comments on commit 66cc35a

Please sign in to comment.