Ilyar
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Tutorial collect contract gas metric **PR:** https://github.com/ton-community/ton-docs/pull/1294 ![collect contract gas metric](https://hackmd.io/_uploads/HktFcGBSee.png) When writing contracts for the TON blockchain, it's important to consider how efficiently [gas is consumed when executing](https://docs.ton.org/v3/documentation/smart-contracts/transaction-fees/fees) the logic you implement, in addition, unlike other blockchains, in the TON blockchain you need to [pay for storing contract data](https://docs.ton.org/v3/documentation/smart-contracts/transaction-fees/fees-low-level#storage-fee) and for [forward messages](http://docs.ton.org/v3/documentation/smart-contracts/transaction-fees/forward-fees) between contracts Therefore, when developing a contract, it's important to pay attention to how data size changes and how gas consumption changes after you modify the contract's behavior or add new functionality To make it easier to track changes in gas consumption and data size, we added the ability to generate reports and compare these metrics between different implementation versions To make this possible, it's enough to write test scenarios that implement the main usage logic of the contract being developed and verify the correctness of the expected behavior, this will be sufficient to obtain relevant metrics and later compare how they change when you update the implementation Before running the tests, a store is created and metrics are collected from all transactions that generate the tests, upon completion, the metrics are supplemented with [information from the ABI in Snapshot](https://github.com/ton-org/sandbox/blob/main/docs/collect-metric-api.md#abi-auto-mapping), and a report is generated based on them, more [metrics are collected](https://github.com/ton-org/sandbox/blob/main/docs/collect-metric-api.md#snapshot-structure) than are used in the current report format - for the current report, only `compute.phase`, `state.code` and `state.data` are used ## Let’s look at this process using an example Create new project just use `npm create ton@latest` ```bash npm create ton@latest -y -- sample --type func-counter --contractName Sample cd sample ``` A contract will be created `contracts/sample.fc` ```func #include "imports/stdlib.fc"; const op::increase = "op::increase"c; global int ctx_id; global int ctx_counter; () load_data() impure { var ds = get_data().begin_parse(); ctx_id = ds~load_uint(32); ctx_counter = ds~load_uint(32); ds.end_parse(); } () save_data() impure { set_data( begin_cell() .store_uint(ctx_id, 32) .store_uint(ctx_counter, 32) .end_cell() ); } () recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure { if (in_msg_body.slice_empty?()) { ;; ignore all empty messages return (); } slice cs = in_msg_full.begin_parse(); int flags = cs~load_uint(4); if (flags & 1) { ;; ignore all bounced messages return (); } load_data(); int op = in_msg_body~load_uint(32); int query_id = in_msg_body~load_uint(64); if (op == op::increase) { int increase_by = in_msg_body~load_uint(32); ctx_counter += increase_by; save_data(); return (); } throw(0xffff); } int get_counter() method_id { load_data(); return ctx_counter; } int get_id() method_id { load_data(); return ctx_id; } ``` Getting current gas report ```bash npx blueprint test --gas-report ... PASS Comparison metric mode: gas depth: 1 Gas report write in 'gas-report.json' ┌───────────┬──────────────┬───────────────────────────┐ │ │ │ current │ │ Contract │ Method ├──────────┬────────┬───────┤ │ │ │ gasUsed │ cells │ bits │ ├───────────┼──────────────┼──────────┼────────┼───────┤ │ │ sendDeploy │ 1937 │ 11 │ 900 │ │ ├──────────────┼──────────┼────────┼───────┤ │ │ send │ 515 │ 11 │ 900 │ │ Sample ├──────────────┼──────────┼────────┼───────┤ │ │ sendIncrease │ 1937 │ 11 │ 900 │ │ ├──────────────┼──────────┼────────┼───────┤ │ │ 0x7e8764ef │ 2681 │ 11 │ 900 │ └───────────┴──────────────┴──────────┴────────┴───────┘ ``` Note that the `op::increase` method appears in the report as `0x7e8764ef`, this opcode can be assigned a more human-readable name by editing the `contract.abi.json` file that was generated along with the current report ```diff --- a/contract.abi.json +++ b/contract.abi.json @@ -6,13 +6,13 @@ "receiver": "internal", "message": { "kind": "typed", - "type": "0x7e8764ef" + "type": "increase" } } ], "types": [ { - "name": "0x7e8764ef", + "name": "increase", "header": 2122802415 } ], ``` Repeated getting current gas report ```bash npx blueprint test --gas-report ... │ ├──────────────┼──────────┼────────┼───────┤ │ │ increase │ 2681 │ 11 │ 900 │ └───────────┴──────────────┴──────────┴────────┴───────┘ ``` To be able to make comparisons, it is necessary to make a snapshot ```bash npx blueprint snapshot --label "v1" ... PASS Collect metric mode: "gas" Report write in '.snapshot/1749821319408.json' ``` Let's try to optimize this contract and add the [inline specifier](https://docs.ton.org/v3/documentation/smart-contracts/func/docs/functions#inline-specifier) for methods ```diff --- a/contracts/sample.fc +++ b/contracts/sample.fc -() load_data() impure { +() load_data() impure inline { -() save_data() impure { +() save_data() impure inline { -() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure { +() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure inline { ``` In order to understand how this change affected your contract performance indicators, we will again receive a gas report, this time we will receive a comparison with the previous version ```bash npx blueprint test --gas-report ... PASS Comparison metric mode: gas depth: 2 Gas report write in 'gas-report.json' ┌───────────┬──────────────┬─────────────────────────────────────────┬───────────────────────────┐ │ │ │ current │ v1 │ │ Contract │ Method ├──────────────┬───────────┬──────────────┼──────────┬────────┬───────┤ │ │ │ gasUsed │ cells │ bits │ gasUsed │ cells │ bits │ ├───────────┼──────────────┼──────────────┼───────────┼──────────────┼──────────┼────────┼───────┤ │ │ sendDeploy │ 1937 same │ 7 -36.36% │ 1066 +18.44% │ 1937 │ 11 │ 900 │ │ ├──────────────┼──────────────┼───────────┼──────────────┼──────────┼────────┼───────┤ │ │ send │ 446 -13.40% │ 7 -36.36% │ 1066 +18.44% │ 515 │ 11 │ 900 │ │ Sample ├──────────────┼──────────────┼───────────┼──────────────┼──────────┼────────┼───────┤ │ │ sendIncrease │ 1937 same │ 7 -36.36% │ 1066 +18.44% │ 1937 │ 11 │ 900 │ │ ├──────────────┼──────────────┼───────────┼──────────────┼──────────┼────────┼───────┤ │ │ increase │ 1961 -26.86% │ 7 -36.36% │ 1066 +18.44% │ 2681 │ 11 │ 900 │ └───────────┴──────────────┴──────────────┴───────────┴──────────────┴──────────┴────────┴───────┘ ``` ## Project exist If project exist, need setup jest config, for it ypr have tow approach **Common config update exist `jest.config.ts`:** ```diff import type { Config } from 'jest'; const config: Config = { preset: 'ts-jest', + testEnvironment: '@ton/sandbox/jest-environment', testPathIgnorePatterns: ['/node_modules/', '/dist/'], + reporters: [ + 'default', + ['@ton/sandbox/jest-reporter', {}], + ] }; export default config; ``` > [!TIP] > see full list options in [Sandbox docs](https://github.com/ton-org/sandbox?tab=readme-ov-file#setup-in-jestconfigts) **Or create separate config `gas-report.config.ts`:** ```ts import config from './jest.config'; // use filter tests if need, see https://jestjs.io/docs/cli#--testnamepatternregex // config.testNamePattern = '^Foo should increase counter$' config.testEnvironment = '@ton/sandbox/jest-environment' config.reporters = [ ['@ton/sandbox/jest-reporter', { }], ] export default config; ``` If a separate configuration is used, the path to this file must be specified as an option when executing the command ```bash npx blueprint test --gas-report -- --config gas-report.config.ts npx blueprint snapshot --label 'v2' -- --config gas-report.config.ts ``` ## Use this without testing ```typescript import { Blockchain, createMetricStore, makeSnapshotMetric, resetMetricStore } from '@ton/sandbox'; const store = createMetricStore(); async function someDo() { const blockchain = await Blockchain.create(); const [alice, bob] = await blockchain.createWallets(2); await alice.send({ to: bob.address, value: 1 }); } async function main() { resetMetricStore(); await someDo(); const metric = makeSnapshotMetric(store); console.log(metric); } main().catch((error) => { console.log(error.message); }); ``` See more details in [Collect contract gas metric API for low-level control](https://github.com/ton-org/sandbox/blob/develop/docs/collect-metric-api.md#example)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully