/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.ngrinder.perftest.service.samplinglistener;

import java.io.File;

import net.grinder.SingleConsole;
import net.grinder.SingleConsole.SamplingLifeCycleListener;
import net.grinder.statistics.StatisticsSet;

import org.ngrinder.infra.logger.CoreLogger;
import org.ngrinder.perftest.service.PerfTestService;

/**
 * PerfTest Sampling collection class.
 * 
 * @author JunHo Yoon
 * @since 3.1.1
 */
public class PerfTestSamplingCollectorListener implements SamplingLifeCycleListener {
	private final Long perfTestId;
	private final SingleConsole singleConsole;
	private final PerfTestService perfTestService;

	/**
	 * Constructor.
	 * 
	 * @param singleConsole
	 *            singleConsole to monitor
	 * @param perfTestId
	 *            perfTest id which this sampling start
	 * @param perfTestService
	 *            service
	 */
	public PerfTestSamplingCollectorListener(SingleConsole singleConsole, Long perfTestId,
					PerfTestService perfTestService) {
		this.singleConsole = singleConsole;
		this.perfTestId = perfTestId;
		this.perfTestService = perfTestService;
	}

	@Override
	public void onSamplingStarted() {
	}

	@Override
	public void onSampling(File file, StatisticsSet intervalStatistics, StatisticsSet cumulativeStatistics) {
		CoreLogger.LOGGER.debug("Sampling is performed");
		perfTestService.saveStatistics(singleConsole, perfTestId);
	}

	@Override
	public void onSamplingEnded() {
	}

}

@@ -637,16 +637,100 @@
 io.File;
+%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;
 %0A%0Aimport
@@ -1153,20 +1153,31 @@
 e final 
-Long
+PerfTestService
  perfTes
@@ -1177,18 +1177,23 @@
 perfTest
-Id
+Service
 ;%0A%09priva
@@ -1199,41 +1199,47 @@
 ate 
-final SingleConsole singleConsole
+ExecutorService newSingleThreadExecutor
 ;%0A%09p
@@ -1241,38 +1241,32 @@
 r;%0A%09private 
-final 
 PerfTest
 Service perf
@@ -1253,38 +1253,60 @@
 PerfTest
-Service perfTestServic
+CollectionRunnable perfTestCollectionRunnabl
 e;%0A%0A%09/**
@@ -1662,71 +1662,464 @@
 his.
-singleConsole = singleConsole;%0A%09%09this.perfTestId = perfTestId
+perfTestService = perfTestService;%0A%09%09this.newSingleThreadExecutor = Executors.newSingleThreadExecutor();%0A%09%09this.perfTestCollectionRunnable = new PerfTestCollectionRunnable(singleConsole, perfTestId);%0A%09%7D%0A%0A%09class PerfTestCollectionRunnable implements Runnable %7B%0A%09%09private final SingleConsole singleConsole;%0A%09%09private final Long perfTestId;%0A%0A%09%09PerfTestCollectionRunnable(SingleConsole singleConsole, Long perfTestId) %7B%0A%09%09%09this.singleConsole = singleConsole
 ;%0A
+%09
 %09%09th
@@ -2133,34 +2133,130 @@
 Test
-Service = perfTestService;
+Id = perfTestId;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void run() %7B%0A%09%09%09perfTestService.saveStatistics(singleConsole, perfTestId);%0A%09%09%7D%0A
 %0A%09%7D%0A
@@ -2482,64 +2482,71 @@
 ;%0A%09%09
-perfTestService.saveStatistics(singleConsole, perfTestId
+newSingleThreadExecutor.execute(this.perfTestCollectionRunnable
 );%0A%09
@@ -2588,18 +2588,56 @@
 ngEnded() %7B%0A
+%09%09newSingleThreadExecutor.shutdown();%0A
 %09%7D%0A%0A%7D%0A

Make the update statistics in perftestcollector async.