跳转到内容

本地模拟、基准测试和 Gas 分析

之前的教程展示了如何使用各种 CLI 命令部署合约以及与 Move 合约进行交互.

通常,这些指令会把交易发送到远程的全节点(fullnode)去模拟和执行.如果您想改变这个过程,在本地模拟这个交易,您可以在指令后面加上下列命令行选项之一,以执行您偏好的行为:

  • --local: 在您的电脑上直接模拟这笔交易,而不进行任何额外的测量或分析工作.
  • --benchmark: 进行交易的基准测试,并汇报其执行所需的时间.
  • --profile-gas: 对交易进行 Gas 使用情况的详细分析.

这些额外的选项可以与以下 CLI 命令结合使用:

  • aptos move run
  • aptos move run-script
  • aptos move publish

另外,如果您想要重放(replaying)之前的交易,不妨参考这个教程.

为了演示目的,我们将继续使用hello_blockchain 包作为示例.

首先,将该包部署到开发网(devnet)或者测试网(testnet),如果您之前未进行过这一步.

进入包目录

Terminal window
cd aptos-move/move-examples/hello_blockchain

然后使用以下命令发布包

Terminal window
aptos move publish --named-addresses hello_blockchain=default --assume-yes
Output
Terminal window
{
"Result": {
"transaction_hash": "0xe4ae0ec4ea3474b2123838885b04d7f4b046c174d14d7dc1c56916f2eb553bcf",
"gas_used": 1118,
"gas_unit_price": 100,
"sender": "dbcbe741d003a7369d87ec8717afb5df425977106497052f96f4e236372f7dd5",
"sequence_number": 5,
"success": true,
"timestamp_us": 1713914742422749,
"version": 1033819503,
"vm_status": "Executed successfully"
}
}

请注意,你需要正确设置你的 CLI 配置文件并正确绑定地址.关于更多的详细信息,请参阅CLI配置.

接下来,使用附加命令行选项 --local 启用本地模拟,执行入口函数 message::set_message.这将在不进行任何进一步测量或分析的情况下在本地执行交易.

Terminal window
aptos move run --function-id 'default::message::set_message' --args 'string:abc' --local
Output
Terminal window
Simulating transaction locally...
{
"Result": {
"transaction_hash": "0x5aab20980688185eed2c9a27bab624c84b8b8117241cd4a367ba2a012069f57b",
"gas_used": 441,
"gas_unit_price": 100,
"sender": "dbcbe741d003a7369d87ec8717afb5df425977106497052f96f4e236372f7dd5",
"success": true,
"version": 1033887414,
"vm_status": "status EXECUTED of type Execution"
}
}

若要测试您交易的执行时间,请使用 --benchmark 选项.

Terminal window
aptos move run --function-id 'default::message::set_message' --args 'string:abc' --benchmark
Output
Terminal window
Benchmarking transaction locally...
Running time (cold code cache): 985.141µs
Running time (warm code cache): 848.159µs
{
"Result": {
"transaction_hash": "0xa2fe548d37f12ee79df13e70fdd8212e37074c1b080b89b7d92e82550684ecdb",
"gas_used": 441,
"gas_unit_price": 100,
"sender": "dbcbe741d003a7369d87ec8717afb5df425977106497052f96f4e236372f7dd5",
"success": true,
"version": 1033936831,
"vm_status": "status EXECUTED of type Execution"
}
}

注意,这些执行时间仅供参考,因为它们取决于您本地机器的配置,并且可能受到干扰或其他随机因素的影响.

如果您想对合约进行优化,请务必根据合约的 Gas 消耗剖析(gas profiling)结果来制定决策.

Aptos Gas 剖析器是一款强力工具,能助您深入理解 Aptos 交易中的 Gas 消耗情况.一旦启用,它会利用一个增强了监测功能的虚拟机来模拟交易,并生成一份基于网页的报告.

由于该报告还包含了完整的执行过程,Gas 剖析器也同样可以用作调试器.

添加 --profile-gas 选项即可启用 Gas 剖析器.

Terminal window
aptos move run --function-id 'default::message::set_message' --args 'string:abc' --profile-gas
Output
Terminal window
Simulating transaction locally using the gas profiler...
Gas report saved to gas-profiling/txn-d0bc3422-0xdbcb-message-set_message.
{
"Result": {
"transaction_hash": "0xd0bc342232f14a6a7d2d45251719aee45373bdb53f68403cfc6dc6062c74fa9e",
"gas_used": 441,
"gas_unit_price": 100,
"sender": "dbcbe741d003a7369d87ec8717afb5df425977106497052f96f4e236372f7dd5",
"success": true,
"version": 1034003962,
"vm_status": "status EXECUTED of type Execution"
}
}

接下来,您可以在 gas-profiling 文件夹中查看生成的 Gas 使用报告:

  • 文件夹hello_blockchain/
    • Move.toml
    • 文件夹sources/
    • 文件夹gas-profiling/
      • 文件夹txn-XXXXXXXX-0xXXXX-message-set_message/
        • 文件夹assets/
        • index.html

index.html 是报告的主页面,您可以使用网络浏览器查看.示例报告

Gas 报告由三部分组成,这些部分帮助您通过不同的视角理解 Gas 的使用情况.

第一部分展示了可视化的 Gas 使用情况,它通过两个火焰图(flamegraphs)来呈现:一个展示执行和输入输出(I/O)操作的 Gas 消耗,另一个展示存储方面的 Gas 消耗.之所以使用两幅图表是因为它们分别采用了两种不同的计量单位:一种是以 Gas 单位衡量,另一种是以 APT 作为单位.

你可以与图表中的各个元素进行互动.当鼠标悬停在某个元素上时,系统会展示出该元素的精确成本及其所占的百分比. gas-profiling-flamegraph-0.png

如果您点击一个项目,您能够放大它并更清楚地看到其子项目.您可以通过点击左上角的“重置缩放(Reset Zoom)”按钮来重置视图. gas-profiling-flamegraph-1.png

在右上角还有一个“搜索(Search)”按钮,这将匹配特定项目并突出显示它们. gas-profiling-flamegraph-2.png

第二部分提供了所有 Gas 成本的详尽分解.此部分展示的数据进行了详细分类,集中汇总并有条理地进行了排序.这对于需要明确知道自己想要关注的数据指标的用户来说,尤其有用.

举个例子,下面的表格列出了所有 Move 语言字节码指令 / 操作的执行成本.这里的百分比是针对相应类别(本例中为执行和 IO)总成本的相对数值.

gas-profiling-cost-break-down-table.png

Gas 报告的最后一部分是交易的完整执行的追踪记录,其内容展现如下:

intrinsic 2.76 85.12%
dependencies 0.0607 1.87%
0xdbcb..::message 0.0607 1.87%
0xdbcb..::message::set_message 0.32416 10.00%
create_ty 0.0004 0.01%
create_ty 0.0004 0.01%
create_ty 0.0004 0.01%
create_ty 0.0004 0.01%
create_ty 0.0008 0.02%
imm_borrow_loc 0.00022 0.01%
call 0.00441 0.14%
0x1::signer::address_of 0.007534 0.23%
create_ty 0.0008 0.02%
move_loc 0.000441 0.01%
call 0.004043 0.12%
0x1::signer::borrow_address 0.000735 0.02%
read_ref 0.001295 0.04%
ret 0.00022 0.01%
st_loc 0.000441 0.01%
copy_loc 0.000854 0.03%
load<0xdbcb..::0xdbcb..::message::MessageHolder> 0.302385 9.33%
exists_generic 0.000919 0.03%
not 0.000588 0.02%
br_false 0.000441 0.01%
imm_borrow_loc 0.00022 0.01%
move_loc 0.000441 0.01%
pack 0.000955 0.03%
move_to_generic 0.001838 0.06%
branch 0.000294 0.01%
@28
ret 0.00022 0.01%
ledger writes 0.097756 3.01%
transaction
events
state write ops 0.097756 3.01%
create<0xdbcb..::0xdbcb..::message::MessageHolder> 0.097756 3.01%

左侧列展示了报告中涉及的所有 Move 指令和操作,其中逐级的缩进用来标示函数调用的层级关系.

中间列展现了与这些操作相对应的 Gas 消耗成本.

此外,还用到了一种特殊标记 @number 来表示字节码中向某个特定位置的跳转(例如在上述示例中的 @28).这一符号的使用主要用于提供信息,帮助用户更好地理解程序的执行流程.