Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register

How to make each thread act differently


nGrinder distributes the script into multiple agent machines and each agent invokes multiple processes  containing designated multiple threads per process. It follows basic The Grinder execution logic. Sometimes, some user asked me how to make each thread does diffent job. Usually, it’s to simulate the real world senario which a lot of users are involved or to eliminate the cache effect.

The most easiest way to achieve this is to use grinder.threadNumber property to get the current thread number. grinder.threadNumber returns the thread number starting from 0 within a single process. A script can make branches to act differently depending on this value. However if more than 100 threads are necessary, it’s better to combine the agent number, the process number and the thread number to generate the unique thread id. Following code shows how to do this.

def curNumber;
@BeforeThread
public void beforeThread() {
  int totalProcessCount = grinder.getProperties().getInt("grinder.processes", 1);
  int totalThreadCount = grinder.getProperties().getInt("grinder.threads", 1);
  int agentNumber = grinder.agentNumber
  int processNumber = grinder.processNumber
  int threadNumber = grinder.threadNumber
  // Calc the current thread's unique id
  curNumber = (agentNumber * totalProcessCount * totalThreadCount)
                   + (processNumber * totalThreadCount) + threadNumber;
}

@Test
public doTest() {
   // Make branches with curNumber.
}

In the beforeThread method, the curNumber is calculated with current test execution environment information. curNumber is unique within the test execution context. You can make branches in the doTest method to perform differently depending on the thread.

comments powered by Disqus
문서 정보
viewed 1640 times
번역 en
작성자
posted 7달 전
junoyoon
공헌자
마지막 수정시간 3달 전
변경 내역 보기
Share this article