내 연결 풀이 올바른 방식으로 작동하는지 어떻게 테스트할 수 있나요?

StackOverflow https://stackoverflow.com/questions/1093888

문제

저는 Apache Axis 2를 사용하여 SOAP 클라이언트를 구현하고 있습니다.SOAP 클라이언트는 많은 양의 요청을 처리해야 하므로 연결 풀을 사용하고 있습니다.

이를 위해 WSDL 파일에서 생성된 스텁의 몇 가지 전송 계층 구성을 설정해야 했습니다.

stub._getServiceClient().getOptions().setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE);

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setDefaultMaxConnectionsPerHost(MAX_CONNECTIONS_PER_HOST);
connectionManager.closeIdleConnections(IDLE_CONNECTION_TIMEOUT);
HttpClient httpClient = new HttpClient(connectionManager);

stub._getServiceClient().getOptions().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

내 고객은 잘 작동하는 것 같습니다.하지만 연결 풀이 올바른 방식으로 작동하는지 테스트할 수 있는 방법을 알고 싶습니다(예:생성된 연결은 IDLE_CONNECTION_TIMEOUT 상수에 정의된 시간 이후에만 삭제됩니다.어떤 아이디어가 있나요?

도움이 되었습니까?

해결책

JUnit 3.x 기반 의사 코드:

  setUp() {
    initialize connection manager;
    initialize connection by creating client;
  }

  tearDown() {
    close connection if necessary;
    close connection manager if necessary;
  }

  testConnectionOpen() {
    assert that connection is open;
    pause for time of idle connection timeout - 1 second;
    assert that connection **is still open**;
  }

  testConnectionClosed() {
    assert that connection is open;
    pause for time of idle connection timeout + 1 second;
    assert that connection **is closed**;
  }

1초를 더하고 1초를 빼는 것은 연결 관리자의 민감도에 따라 조정되어야 합니다.

다른 팁

많은 수의 요청을 생성하고 연결 수가 MAX_CONNECTIONS를 넘지 않도록 하는 테스트베드 애플리케이션을 작성합니다.jconsole 또는 VisualVM을 프로세스에 연결하여 후자를 확인할 수 있습니다.

당신은 또한 사용을 살펴볼 수 있습니다 자카르타 JMeter 클래스/클라이언트에 로드를 생성한 다음 여러 데이터 포인트를 그래프로 표시합니다(생성된 클라이언트 연결 수에 어떻게 액세스할 수 있는지는 확실하지 않음).

매개변수를 변경하여 테스트할 수 있습니다.

  • NO_OF_THREADS
  • POOL_SIZE
  • ENABLE_CONNECTION_POOLING

/** number of concurrent searches */
private static final int NO_OF_THREADS = 20;

/** size of the http connection pool */
private static final int POOL_SIZE = 20;

/** enabling or disabling the connection pool */
private static boolean ENABLE_CONNECTION_POOLING = true;

/** close idle connection time in milliseconds
 * connections will be release if they are idle for this time */
private static int CLOSE_IDLE_CONNECTION_TIME = 1000;

public void test()
{
    init();

    long start = System.currentTimeMillis();

    List<Thread> threads = new ArrayList<Thread>();
    for ( int i = 0; i < NO_OF_THREADS; i++ )
    {
        SimpleThread thread = new SimpleThread();
        thread.start();

        threads.add( thread );
    }

    for ( Thread t : threads )
    {
        try
        {
            t.join();
        }
        catch ( InterruptedException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    logger.info( "************* Test Finish In *******************" + ( System.currentTimeMillis() - start ) + " ms" );
}

public void init()
{
    super.init();

    try
    {
        long t1 = System.currentTimeMillis();
        stub = new Viva_x0020_external_x0020_API_x0020_for_x0020_partnersStub( endUrl );

        if ( ENABLE_CONNECTION_POOLING )
        {
            stub._getServiceClient().getOptions().setProperty( HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_TRUE );
            MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            connectionManager.getParams().setDefaultMaxConnectionsPerHost( POOL_SIZE );
            connectionManager.closeIdleConnections( CLOSE_IDLE_CONNECTION_TIME );
            HttpClient httpClient = new HttpClient( connectionManager );
            stub._getServiceClient().getOptions().setProperty( HTTPConstants.CACHED_HTTP_CLIENT, httpClient );
        }

        logger.info( "Connection Established in " + ( System.currentTimeMillis() - t1 ) + "ms" );
    }
    catch ( AxisFault e1 )
    {
        e1.printStackTrace();
        fail( "Error on creating the stub" );
    }

    logger.info( "Connection Initialized successfully" );
}

public class SimpleThread extends Thread
{
    public void run()
    {
        search();
    }
}

private static void search()
{
    logger.info( "$$$$$$$$$$$$$ Start the Search $$$$$$$$$$$$$$" );
    GetAirportConnections request = new GetAirportConnections();
    request.setAirportCode( "MTY" );

    GetAirportConnectionsResponse response = null;
    try
    {
        long t1 = System.currentTimeMillis();
        response = stub.getAirportConnections( request, getUserCredentials() );
        logger.info( "Results Retrived in " + ( System.currentTimeMillis() - t1 ) + "ms" );
    }
    catch ( Exception e )
    {
        logger.error( "------------------------- Connection Timeout --------------------------" );
        e.printStackTrace();
        fail( e.getMessage() );
    }

    Airport[] airports = response.getGetAirportConnectionsResult().getAirport();
    logger.info("Number of airports : " + airports.length );
}

}

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top