跳转到主要内容

参数

limit
number
要返回的样本数量(默认:720)

响应

result
array
性能样本对象数组
slot
number
Slot 编号
numSlots
number
样本中的 slot 数量
numTransactions
number
样本中的交易数量
samplePeriodSecs
number
样本周期(以秒为单位)
numNonVoteTransaction
number
非投票交易的数量

代码示例

基本请求

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;
}

注意事项

  1. 返回网络的近期性能样本
  2. 每个样本覆盖一段时间(通常为 60 秒)
  3. 样本包括交易计数和 slot 信息
  4. 响应是即时的,因为它从当前状态读取
  5. 默认限制为 720 个样本(约 12 小时)

最佳实践

  1. 根据分析需求使用适当的限制数量
  2. 在适当时缓存结果以减少 RPC 负载
  3. 监控性能的显著变化
  4. 考虑使用 WebSocket 订阅获取实时更新
  5. 适当处理网络错误并重试

常见错误

错误码消息解决方案
-32601Method not found验证是否连接到 Solana RPC 节点
-32602Invalid params检查 limit 参数
-32007Performance samples unavailable节点可能正在启动或同步中

用例

  1. 性能分析
    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
        }
      };
    }
    
  2. 性能监控
    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;
      }
    }
    
  3. 性能历史
    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
        }
      };
    }