net.i2p.router.peermanager

## Class PeerTestJob

- java.lang.Object
- - net.i2p.router.JobImpl
  - - net.i2p.router.peermanager.PeerTestJob

- All Implemented Interfaces:
    Job

      public class PeerTestJob
      extends JobImpl

  Periodically tests selected peers to gather real-time performance data and update peer profiles.

  This job runs continuously when enabled, selecting peers based on bandwidth tier, reachability, and version compatibility. Tests are performed by sending a DatabaseStoreMessage containing the peer's own RouterInfo back to itself through the tunnel system, measuring response times and connectivity.

  Peer Selection Criteria:

  - High-bandwidth tiers (O, P, X) with reachability capability
  - Version 0.9.57+ for compatibility
  - Configurable concurrency based on system resources

  Performance Impact:

  - Adaptive delays based on system load and uptime
  - CPU throttling when load exceeds 80%
  - Automatic timeout adjustment based on historical test times

  Future Considerations:

  - Evaluate necessity of peer testing in current network conditions
  - Consider alternative test methods beyond RI self-messaging
  - Integration with PeerManager.selectPeers() optimization

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `PROP_ADAPTIVE_PEER_TEST`
    `static String` | `PROP_PEER_TEST_CONCURRENCY`
    `static String` | `PROP_PEER_TEST_DELAY`
    `static String` | `PROP_PEER_TEST_TIMEOUT`

    - ### Fields inherited from class net.i2p.router.JobImpl

      `_id`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PeerTestJob(RouterContext context)` Creates a new PeerTestJob instance and initializes required statistics.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `getAvgPeerTestTime()` Gets the average successful peer test time over the last minute.
    `String` | `getName()` Descriptive name of the task
    `int` | `getTotalAvgPeerTestTime()` Gets the total average peer test time including both successful and slow tests over the last hour.
    `void` | `runJob()` Main job execution loop that performs peer testing with adaptive scheduling.
    `void` | `startTesting(PeerManager manager)` Override concurrency to use aggressive mode under attack.
    `void` | `stopTesting()` Stops the peer testing process gracefully.

    - ### Methods inherited from class net.i2p.router.JobImpl

      `dropped, getContext, getJobId, getMadeReadyOn, getTiming, madeReady, requeue, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### PROP_ADAPTIVE_PEER_TEST

          public static final String PROP_ADAPTIVE_PEER_TEST

      See Also:
        Constant Field Values

    - #### PROP_PEER_TEST_CONCURRENCY

          public static final String PROP_PEER_TEST_CONCURRENCY

      See Also:
        Constant Field Values

    - #### PROP_PEER_TEST_DELAY

          public static final String PROP_PEER_TEST_DELAY

      See Also:
        Constant Field Values

    - #### PROP_PEER_TEST_TIMEOUT

          public static final String PROP_PEER_TEST_TIMEOUT

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### PeerTestJob

          public PeerTestJob(RouterContext context)

      Creates a new PeerTestJob instance and initializes required statistics.
      Parameters:
        `context` - the router context for accessing services like logging and statistics

  - ### Method Detail

    - #### getAvgPeerTestTime

          public int getAvgPeerTestTime()

      Gets the average successful peer test time over the last minute.
      Returns:
        average test time in milliseconds, or 0 if no context available

    - #### getName

          public String getName()

      Description copied from interface: `Job`
      Descriptive name of the task
      Returns:
        the descriptive name of the task

    - #### getTotalAvgPeerTestTime

          public int getTotalAvgPeerTestTime()

      Gets the total average peer test time including both successful and slow tests over the last hour.
      Returns:
        total average test time in milliseconds, or 0 if no context available

    - #### runJob

          public void runJob()

      Main job execution loop that performs peer testing with adaptive scheduling.

      Process flow:

      1. Check if testing should continue
      2. Select peers for testing based on criteria
      3. Test each selected peer
      4. Adapt next run delay based on system conditions

      Adaptive Behavior:

      - Double delay if job lag > 300ms (system overload)
      - Double delay if CPU load > 80% (resource conservation)
      - Enable aggressive testing when build success < 40% (attack mitigation)
      - Increase test concurrency under attack to identify failing peers faster

    - #### startTesting

          public void startTesting(PeerManager manager)

      Override concurrency to use aggressive mode under attack. Aggressive mode tests more peers concurrently to identify failures faster.

    - #### stopTesting

          public void stopTesting()

      Stops the peer testing process gracefully.

      The current test round will complete, but no new rounds will be scheduled.