参数
要返回的样本数量(默认:720)
响应
代码示例
基本请求
复制
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getRecentPerformanceSamples",
"params": []
}'
带限制的请求
复制
curl https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method": "getRecentPerformanceSamples",
"params": [
100
]
}'
使用 web3.js
复制
import { Connection } from '@solana/web3.js';
const connection = new Connection('https://fra.rpc.orbitflare.com?api_key=YOUR-API-KEY');
// Get recent performance samples
const samples = await connection.getRecentPerformanceSamples();
console.log('Performance samples:', samples);
// Calculate average TPS
function calculateAverageTPS(samples: any[]): number {
const totalTransactions = samples.reduce(
(sum, sample) => sum + sample.numTransactions,
0
);
const totalSeconds = samples.reduce(
(sum, sample) => sum + sample.samplePeriodSecs,
0
);
return totalTransactions / totalSeconds;
}
注意事项
- 返回网络的近期性能样本
- 每个样本覆盖一段时间(通常为 60 秒)
- 样本包括交易计数和 slot 信息
- 响应是即时的,因为它从当前状态读取
- 默认限制为 720 个样本(约 12 小时)
最佳实践
- 根据分析需求使用适当的限制数量
- 在适当时缓存结果以减少 RPC 负载
- 监控性能的显著变化
- 考虑使用 WebSocket 订阅获取实时更新
- 适当处理网络错误并重试
常见错误
| 错误码 | 消息 | 解决方案 |
|---|---|---|
| -32601 | Method not found | 验证是否连接到 Solana RPC 节点 |
| -32602 | Invalid params | 检查 limit 参数 |
| -32007 | Performance samples unavailable | 节点可能正在启动或同步中 |
用例
-
性能分析
复制
interface PerformanceMetrics { averageTPS: number; peakTPS: number; totalTransactions: number; totalSlots: number; metadata: { timestamp: number; sampleCount: number; }; } async function analyzePerformance( limit: number = 720 ): Promise<PerformanceMetrics> { const samples = await connection.getRecentPerformanceSamples(limit); const totalTransactions = samples.reduce( (sum, sample) => sum + sample.numTransactions, 0 ); const totalSeconds = samples.reduce( (sum, sample) => sum + sample.samplePeriodSecs, 0 ); const peakTPS = Math.max( ...samples.map(sample => sample.numTransactions / sample.samplePeriodSecs) ); return { averageTPS: totalTransactions / totalSeconds, peakTPS, totalTransactions, totalSlots: samples.reduce((sum, sample) => sum + sample.numSlots, 0), metadata: { timestamp: Date.now(), sampleCount: samples.length } }; } -
性能监控
复制
interface PerformanceAlert { type: 'high_tps' | 'low_tps' | 'anomaly'; currentTPS: number; threshold: number; metadata: { timestamp: number; slot: number; }; } class PerformanceMonitor { private previousTPS: number | null = null; private readonly threshold = 0.2; // 20% change threshold async monitorPerformance( interval: number = 60000 ): Promise<PerformanceAlert | null> { const samples = await connection.getRecentPerformanceSamples(1); const currentSample = samples[0]; const currentTPS = currentSample.numTransactions / currentSample.samplePeriodSecs; if (this.previousTPS === null) { this.previousTPS = currentTPS; return null; } const change = Math.abs(currentTPS - this.previousTPS) / this.previousTPS; if (change > this.threshold) { const alert: PerformanceAlert = { type: change > 0 ? 'high_tps' : 'low_tps', currentTPS, threshold: this.threshold, metadata: { timestamp: Date.now(), slot: currentSample.slot } }; this.previousTPS = currentTPS; return alert; } return null; } } -
性能历史
复制
interface PerformanceHistory { samples: Array<{ slot: number; tps: number; nonVoteTPS: number; timestamp: number; }>; summary: { averageTPS: number; peakTPS: number; totalTransactions: number; }; metadata: { startTime: number; endTime: number; sampleCount: number; }; } async function getPerformanceHistory( limit: number = 720 ): Promise<PerformanceHistory> { const samples = await connection.getRecentPerformanceSamples(limit); const processedSamples = samples.map(sample => ({ slot: sample.slot, tps: sample.numTransactions / sample.samplePeriodSecs, nonVoteTPS: sample.numNonVoteTransaction / sample.samplePeriodSecs, timestamp: Date.now() - (samples.length - samples.indexOf(sample)) * sample.samplePeriodSecs * 1000 })); const totalTransactions = samples.reduce( (sum, sample) => sum + sample.numTransactions, 0 ); const totalSeconds = samples.reduce( (sum, sample) => sum + sample.samplePeriodSecs, 0 ); return { samples: processedSamples, summary: { averageTPS: totalTransactions / totalSeconds, peakTPS: Math.max(...processedSamples.map(sample => sample.tps)), totalTransactions }, metadata: { startTime: processedSamples[0].timestamp, endTime: processedSamples[processedSamples.length - 1].timestamp, sampleCount: samples.length } }; }