Add time execution log.trace

This commit is contained in:
Vincent Spiewak
2013-09-13 15:42:11 +02:00
parent c63da86119
commit 3cee4afaf0
2 changed files with 36 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ package com.github.vspiewak.loggenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;
public class App {
@@ -10,26 +11,43 @@ public class App {
private static final Logger log = LoggerFactory.getLogger(App.class);
private static AtomicLong counter = new AtomicLong(0);
public static final long NB_LOGS = 10;
public static final long DEFAULT_NB_LOGS = 100;
public static final int DEFAULT_NB_THREAD = 2;
public static void main(String... args) {
public static void main(String[] args) {
log.debug("starting");
long nbLogsToGenerate = DEFAULT_NB_LOGS;
int nbThreads = DEFAULT_NB_THREAD;
LogExecutor executor = new LogExecutor(5);
if(args.length > 0) {
try {
nbLogsToGenerate = Long.parseLong(args[0]);
} catch(NumberFormatException e) {
log.info("Usage: first argument must be the number of logs to generate");
System.exit(1);
}
}
while (counter.get() < NB_LOGS) {
log.trace("starting");
long start_time = System.nanoTime();
LogExecutor executor = new LogExecutor(nbThreads);
while (counter.get() < nbLogsToGenerate) {
long number = counter.incrementAndGet();
SellTask task1 = new SellTask(number);
SearchTask task2 = new SearchTask(number);
executor.add(task1)
.add(task2);
SearchTask aSearchTask = new SearchTask(number);
SellTask aSellTask = new SellTask(number);
executor.addAll(Arrays.asList(aSearchTask, aSellTask));
}
executor.execute();
log.debug("shutdown");
long end_time = System.nanoTime();
double difference = (end_time - start_time)/1e6;
log.trace("generated {} logs in {}ms using {} threads", counter.get(), (int)difference, nbThreads);
log.trace("shutdown");
}

View File

@@ -6,14 +6,9 @@ import java.util.concurrent.Executors;
public class LogExecutor {
public static final int DEFAULT_NB_THREADS = 4;
private final int nbThreads;
private ExecutorService executor;
public LogExecutor() {
this(DEFAULT_NB_THREADS);
}
public LogExecutor(int nbThreads) {
this.nbThreads = nbThreads;
executor = Executors.newFixedThreadPool(this.nbThreads);
@@ -24,6 +19,13 @@ public class LogExecutor {
return this;
}
public LogExecutor addAll(Iterable<Callable<Long>> tasks) {
for(Callable<Long> task : tasks) {
executor.submit(task);
}
return this;
}
public void execute() {
// This will make the executor accept no new threads