mirror of
https://github.com/Febbweiss/log-generator.git
synced 2026-03-04 22:25:40 +00:00
Add repeat feature
This commit is contained in:
@@ -12,10 +12,11 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(App.class);
|
private static final Logger log = LoggerFactory.getLogger(App.class);
|
||||||
private static AtomicLong counter = new AtomicLong(0);
|
private static AtomicLong total = new AtomicLong(0);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
|
||||||
|
// parse params
|
||||||
InputParameters params = new InputParameters();
|
InputParameters params = new InputParameters();
|
||||||
JCommander commander = new JCommander(params);
|
JCommander commander = new JCommander(params);
|
||||||
try {
|
try {
|
||||||
@@ -25,26 +26,59 @@ public class App {
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// init
|
||||||
log.trace("starting");
|
log.trace("starting");
|
||||||
|
|
||||||
long start_time = System.nanoTime();
|
final long start = System.nanoTime();
|
||||||
LogExecutor executor = new LogExecutor(params.threads);
|
final int threads = params.threads;
|
||||||
|
|
||||||
|
// add shutdown hook
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
|
public void run() {
|
||||||
|
long elapsed_loop = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
|
||||||
|
log.trace("generated {} logs in {}ms using {} threads", total.get(), elapsed_loop, threads);
|
||||||
|
log.trace("shutdown");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// will be repeated every params.repeat milliseconds
|
||||||
|
do {
|
||||||
|
|
||||||
|
final long start_loop = System.nanoTime();
|
||||||
|
AtomicLong counter = new AtomicLong(0);
|
||||||
|
LogExecutor executor = new LogExecutor(threads);
|
||||||
|
|
||||||
|
//generate params.logs
|
||||||
while (counter.get() < params.logs) {
|
while (counter.get() < params.logs) {
|
||||||
|
total.incrementAndGet();
|
||||||
|
counter.incrementAndGet();
|
||||||
int seed = new Random().nextInt(10);
|
int seed = new Random().nextInt(10);
|
||||||
if (seed > 6) {
|
if (seed > 6) {
|
||||||
executor.add(new SellRequest(counter.incrementAndGet()));
|
executor.add(new SellRequest(total.get()));
|
||||||
} else {
|
} else {
|
||||||
executor.add(new SearchRequest(counter.incrementAndGet()));
|
executor.add(new SearchRequest(total.get()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wait the end
|
||||||
executor.finish();
|
executor.finish();
|
||||||
|
|
||||||
long elapsed_time = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start_time);
|
// if repeat option
|
||||||
|
if (params.repeat > 0) {
|
||||||
|
|
||||||
log.trace("generated {} logs in {}ms using {} threads", counter.get(), elapsed_time, params.threads);
|
// print log generated during the loop
|
||||||
log.trace("shutdown");
|
long elapsed_loop = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start_loop);
|
||||||
|
log.trace("generated {} logs in {}ms using {} threads", counter.get(), elapsed_loop, params.threads);
|
||||||
|
|
||||||
|
// sleep until the end of the interval
|
||||||
|
long sleep_time = params.repeat - elapsed_loop;
|
||||||
|
if (sleep_time > 0) {
|
||||||
|
log.trace("sleep: {}ms", sleep_time);
|
||||||
|
Thread.sleep(sleep_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (params.repeat > 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ public class InputParameters {
|
|||||||
public Long logs;
|
public Long logs;
|
||||||
|
|
||||||
@Parameter(names = {"-threads", "-t"}, description = "Number of threads to use")
|
@Parameter(names = {"-threads", "-t"}, description = "Number of threads to use")
|
||||||
public Integer threads = 2;
|
public Integer threads = 1;
|
||||||
|
|
||||||
|
@Parameter(names = {"-repeat", "-r"}, description = "Repeat every N milliseconds")
|
||||||
|
public Integer repeat = 0;
|
||||||
|
|
||||||
@Parameter(names = "--help", help = true)
|
@Parameter(names = "--help", help = true)
|
||||||
public boolean help;
|
public boolean help;
|
||||||
|
|||||||
Reference in New Issue
Block a user