如何测量Hyperledger Besu私有以太坊区块链的延迟与吞吐量?
Hyperledger Besu私有链延迟与吞吐量测量方案
一、延迟测量方法
延迟通常指交易从提交到被打包进区块并确认的时间,或链的平均出块间隔,以下是具体实现方式:
1. 自定义脚本直接捕获交易生命周期
使用Web3.js/Ethers.js编写脚本,手动记录交易发送时间与确认时间,计算差值:
const { ethers } = require("ethers"); async function measureTransactionDelay() { // 连接到Besu节点 const provider = new ethers.JsonRpcProvider("http://localhost:8545"); const wallet = new ethers.Wallet("your-private-key", provider); // 记录发送时间 const sendTime = Date.now(); // 发送测试交易(简单转账) const tx = await wallet.sendTransaction({ to: "0xRecipientAddress", value: ethers.parseEther("0.001") }); // 等待交易被打包确认 const receipt = await tx.wait(); const confirmTime = Date.now(); // 计算延迟(毫秒) const delay = confirmTime - sendTime; console.log(`交易延迟: ${delay}ms,区块高度: ${receipt.blockNumber}`); } measureTransactionDelay();
注:可循环发送多笔交易取平均值,减少误差。
2. 利用Besu内置Metrics监控
Besu支持暴露Prometheus兼容的监控指标,直接提取延迟相关数据:
- 启动Besu时开启Metrics:
besu --metrics-enabled --metrics-port=9545 - 关键指标:
besu_block_time_seconds: 平均出块间隔(链层面的延迟指标)besu_eth_transaction_pool_pending_time_seconds: 交易在pending池中的等待时间besu_eth_transaction_latency_seconds: 交易从提交到打包的总延迟
- 配置Prometheus抓取该端点,通过Grafana创建可视化面板,实时查看延迟趋势。
二、吞吐量测量方法
吞吐量指单位时间内链成功处理的交易数量,常用TPS(每秒交易数)表示,具体方法:
1. 压力测试工具批量压测
使用专业压测工具模拟高并发交易,统计成功处理量:
示例:用k6编写压测脚本
import http from 'k6/http'; import { check, sleep } from 'k6'; export const options = { vus: 50, // 并发虚拟用户数 duration: '30s', // 压测时长 }; const rpcUrl = 'http://localhost:8545'; const recipient = '0xRecipientAddress'; // 提前离线生成签名后的交易数据,避免压测时性能损耗 function generateSignedTx() { // 实际需用ethers.js等工具完成离线签名,生成rawTransaction return '0xf86c808504a817c80082520894...'; // 替换为真实签名交易数据 } export default function () { const rawTx = generateSignedTx(); const payload = JSON.stringify({ jsonrpc: '2.0', method: 'eth_sendRawTransaction', params: [rawTx], id: 1 }); const res = http.post(rpcUrl, payload, { headers: { 'Content-Type': 'application/json' }, }); check(res, { '交易提交成功': (r) => r.json().result !== undefined, }); sleep(0.1); // 控制单用户发送频率 }
运行脚本后,k6会输出TPS指标(http_reqs/压测时长),结合交易确认率计算有效TPS。
备选工具:Besu官方性能测试工具
使用besu-perf-test(需从源码编译或下载预编译包),直接模拟批量交易:
besu-perf-test --nodes=http://localhost:8545 --num-accounts=100 --tps=100 --duration=60s
工具会自动统计最终的有效TPS和交易成功率。
2. 链上区块数据分析
直接遍历链上区块,统计交易总量与时间跨度计算吞吐量:
# 示例:用curl结合jq统计TPS # 获取起始与结束区块高度 start_block=1 end_block=$(curl -s http://localhost:8545 -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' | jq .result | xargs printf "%d\n") # 获取起始区块时间 start_time=$(curl -s http://localhost:8545 -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x'$start_block'",false],"id":1}' | jq .result.timestamp | xargs printf "%d\n") # 获取结束区块时间 end_time=$(curl -s http://localhost:8545 -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x'$end_block'",false],"id":1}' | jq .result.timestamp | xargs printf "%d\n") # 统计总交易数 total_tx=0 for ((i=start_block; i<=end_block; i++)); do tx_count=$(curl -s http://localhost:8545 -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x'$i'",false],"id":1}' | jq .result.transactions | length) total_tx=$((total_tx + tx_count)) done # 计算TPS duration=$((end_time - start_time)) tps=$(echo "scale=2; $total_tx / $duration" | bc) echo "总交易数: $total_tx,时长: $duration秒,TPS: $tps"
注:该方法适合回溯历史吞吐量,需注意遍历区块的性能问题(可批量处理优化)。
测试注意事项
- 测试前确保链处于稳定状态(无同步、无异常节点)
- 调整Besu节点配置:如
--min-gas-price、--block-gas-limit、出块时间(--block-time),避免配置限制影响结果 - 多次测试取平均值,排除偶然波动
内容的提问来源于stack exchange,提问作者MinerNoob24
相关产品推荐
相关产品推荐

