Loading test using java timer vs scheduler task

I need to perform a load test using Java, in which one of the test strategies requires x threads to start for every y time period in z minutes, and after that have a constant total number of threads executed for the duration of the load test (for example, a total of 100 threads, start 10 threads at 5 second intervals until all 100 threads start running and continue to execute 100 threads in total (once it finishes executing, it must restart) for the specified test duration, say one hour)

I tried to use a timer task but it seems to be a limitation, can there be a better thread scheduler? What would be the best approach?

public class MyTask extends TimerTask{
    public void run() {
        System.out.println("STARTING THREAD "+ counter +" "+ new Date());
        //execute test
        counter++;

        if (counter > maxIterations) {
            MyTask.this.cancel();
            return;
        }
    }

    List<TimerTask> MyTaskList = new ArrayList<TimerTask>();   
    for (int i = 1 ; i <= threadsPerIteration ; i++) {
        TimerTask MyTimerTask = new MyTask(NumberOfIterations);
        MyTaskList.add(MyTimerTask);

        timer.schedule(MyTimerTask, initialDelayMilli, threadDelayMilli);
    }

      

thank

+3


source to share


2 answers


Thanks for the help, I decided to use the threadpool executor along with the timertask class like this:

I used the Executors.newScheduledThreadPool (int x) method to control the number of threads that can be run concurrently, along with a timer task that is configured to increase the size of the thread's thread every time:



   TimerTask DelayTimerTask = new TimerTask() { //task to increase threadpool size

          public void run() {
            MyExecutor.setCorePoolSize(i * incrementAmount);          //timer task increments threadpool size by threadPoolIncrement
            i++;
        }
      };
    timer.scheduleAtFixedRate(DelayTimerTask,0,intervalLength);

      

thus, the number of parallel threads will increase with incrementAmount every intervalLength.

0


source


Don't use TimerTask for every thread. Instead, use a single TimerTask that fires once per interval, with example numbers once every 5 seconds.



Each of the first 10 times the TimerTask fires, it generates 10 threads. On each subsequent run, it checks the number of active threads and spawns enough new threads to bring the total to 100, before the end of your test.

+1


source







All Articles