testStarted
method을 사용하여 모든 스레드에 대한 연결을 초기화 할 수 있습니다. 이 메소드는 스레드가 복제되기 전에 한 번 실행되므로, testStarted
에 연결 풀이 있어야합니다. 그러면 연결 풀을 가져 와서 사용할 수 있습니다. 예를 들어 매우 원시적 인 연결 풀은 키와 연결 객체에 대한 순차적 ID가있는 맵입니다.
이
그래서 같은 간단한 풀이로 초기화 할 수 :
@Override
public void testStarted()
{
int maxConnections = getThreadContext().getThreadGroup().getNumThreads();
ConcurrentMap<Integer, Object> connections = new ConcurrentHashMap<Integer, Object>();
for(int i = 0; i < maxConnections; i++)
{
Object connection = //... whatever you need to do to connect
connections.put(new Integer(i), connection);
}
// Put in the context of thread group
JMeterContextService.getContext().getVariables().putObject("MyConnections", connections);
}
(연결 개체가 필요에 따라보다 구체적인 유형, 수) 각 스레드는 스레드 수에 따라, 그 풀에서 하나의 연결을 것입니다 .
나중에는 sample
방법을 사용할 수 있습니다
// Get connections pool from context
ConcurrentMap<Integer, Object> connections = (ConcurrentHashMap<Integer, Object>) JMeterContextService.getContext().getVariables().getObject("MyConnections");
// Find connection by thread ID, so each thread goes to a different connection
connections.get(getThreadContext().getThreadNum());
을 스레드 수는 실행 시간과 내가 연결 초기화에 사용되는 초기 순차적 인 정수로 반환 사이에 여기에 내가 순진 완벽한 매핑을 가정합니다. 최선의 가정은 아니지만 개선 될 수는 있지만 유효한 출발점입니다.
그런 다음 testEnded method에서 연결을 닫고 제거 할 수 있습니다. 이 방법은 한 번 실행, 그래서 우리는 모든 연결을 닫습니다
@Override
public void testEnded()
{
for(Entry<Integer, Object> connection : connections.entrySet())
{
connection.close(); // or do whatever you need to close it
connections.remove(connection.getKey());
}
}
또는 모든 연결이 종료 될 때 그냥 connections.clear()
를 호출 할 수 있습니다.
공개 :이 답변에서 코드를 직접 테스트하지는 않았지만 과거에는 비슷한 코드 조각을 사용하여이 질문에 대답했습니다. 문제가 있으면이 답변을 업데이트하십시오.
@ user7294900 링크를 공유해 주셔서 감사합니다. 필자는이 링크를 이미 방문했지만 나의 요구 사항에 맞지 않습니다. 왜냐하면 제 경우에는 연결 공유를 구현하는 방법을 이해할 필요가있는 사용자 정의 샘플러가 있기 때문입니다. – VinothNair