mirror of
https://github.com/Febbweiss/log-generator.git
synced 2026-03-04 14:15:42 +00:00
Big bang refactoring
This commit is contained in:
@@ -5,7 +5,7 @@ import com.beust.jcommander.ParameterException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@@ -25,20 +25,22 @@ public class App {
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
log.trace("initialization");
|
||||
log.trace("starting");
|
||||
|
||||
long start_time = System.nanoTime();
|
||||
LogExecutor executor = new LogExecutor(params.threads);
|
||||
|
||||
while (counter.get() < params.logs) {
|
||||
SearchTask aSearchTask = new SearchTask(counter.incrementAndGet());
|
||||
SellTask aSellTask = new SellTask(counter.incrementAndGet());
|
||||
executor.addAll(Arrays.asList(aSearchTask, aSellTask));
|
||||
int seed = new Random().nextInt(10);
|
||||
if (seed > 6) {
|
||||
executor.add(new SellRequest(counter.incrementAndGet()));
|
||||
} else {
|
||||
executor.add(new SearchRequest(counter.incrementAndGet()));
|
||||
}
|
||||
}
|
||||
|
||||
log.trace("initialization done");
|
||||
executor.finish();
|
||||
|
||||
long start_time = System.nanoTime();
|
||||
executor.execute();
|
||||
long elapsed_time = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start_time);
|
||||
|
||||
log.trace("generated {} logs in {}ms using {} threads", counter.get(), elapsed_time, params.threads);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.github.vspiewak.loggenerator;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -14,19 +13,12 @@ public class LogExecutor {
|
||||
executor = Executors.newFixedThreadPool(this.nbThreads);
|
||||
}
|
||||
|
||||
public LogExecutor add(Callable<Long> task) {
|
||||
public LogExecutor add(Runnable task) {
|
||||
executor.submit(task);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LogExecutor addAll(Iterable<Callable<Long>> tasks) {
|
||||
for (Callable<Long> task : tasks) {
|
||||
executor.submit(task);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void execute() {
|
||||
public void finish() {
|
||||
|
||||
// This will make the executor accept no new threads
|
||||
// and finish all existing threads in the queue
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.github.vspiewak.loggenerator;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SearchRequest implements Runnable {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SearchRequest.class);
|
||||
private final long id;
|
||||
|
||||
public SearchRequest(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("{} - {}", id, Utils.getRandomSearch());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.github.vspiewak.loggenerator;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class SearchTask implements Callable<Long> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SearchTask.class);
|
||||
private final long id;
|
||||
|
||||
public SearchTask(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long call() throws Exception {
|
||||
log.info("{} - {}", id, Utils.getRandomIP());
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.github.vspiewak.loggenerator;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SellRequest implements Runnable {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SellRequest.class);
|
||||
private final long id;
|
||||
|
||||
public SellRequest(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("{} - {}", id, Utils.getRandomSell());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.github.vspiewak.loggenerator;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class SellTask implements Callable<Long> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SellTask.class);
|
||||
private final long id;
|
||||
|
||||
public SellTask(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long call() throws Exception {
|
||||
log.info("{} - {}", id, Utils.getRandomIP());
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,25 +11,98 @@ import java.util.Scanner;
|
||||
|
||||
public class Utils {
|
||||
|
||||
private static final String LOG_SEPARATOR = " - ";
|
||||
private static final Logger log = LoggerFactory.getLogger(Utils.class);
|
||||
private static final List<String> ips = new ArrayList<String>();
|
||||
private static final List<String> products = new ArrayList<String>();
|
||||
|
||||
static {
|
||||
private static void readFromFile(String file, List<String> list) {
|
||||
try {
|
||||
InputStream is = Utils.class.getClassLoader().getResourceAsStream("ips.txt");
|
||||
InputStream is = Utils.class.getClassLoader().getResourceAsStream(file);
|
||||
Scanner scan = new Scanner(is);
|
||||
while (scan.hasNext()) {
|
||||
String ip = scan.next().trim();
|
||||
if (ip.length() > 0)
|
||||
ips.add(ip);
|
||||
String line = scan.next().trim();
|
||||
if (line.length() > 0)
|
||||
list.add(line);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error during ip read/parsing", e);
|
||||
log.error("Error during read/parse of file: ", file);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getRandomIP() {
|
||||
int rndInt = new Random().nextInt(ips.size());
|
||||
return ips.get(rndInt);
|
||||
private static <E> E getRandomFromList(List<E> list) {
|
||||
int index = new Random().nextInt(list.size());
|
||||
return list.get(index);
|
||||
}
|
||||
|
||||
private static <E> E getRandomFromArray(E[] array) {
|
||||
int index = new Random().nextInt(array.length);
|
||||
return array[index];
|
||||
}
|
||||
|
||||
private static String getRandomIP() {
|
||||
return getRandomFromList(ips);
|
||||
}
|
||||
|
||||
private static double getRandomPrice() {
|
||||
int min = 40;
|
||||
int max = 200;
|
||||
return min + new Random().nextInt(max - min) + 0.99;
|
||||
}
|
||||
|
||||
public static String getRandomSearch() {
|
||||
return getRandomSearch(new Random().nextInt(10));
|
||||
}
|
||||
|
||||
private static String getRandomSearch(int seed) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(getRandomIP());
|
||||
|
||||
sb.append(LOG_SEPARATOR);
|
||||
sb.append(getRandomFromArray(cat.values()));
|
||||
|
||||
sb.append(LOG_SEPARATOR);
|
||||
if (seed > 7) {
|
||||
sb.append(getRandomFromArray(matiere.values()));
|
||||
}
|
||||
|
||||
sb.append(LOG_SEPARATOR);
|
||||
if (seed > 4) {
|
||||
sb.append(getRandomFromArray(colors.values()));
|
||||
}
|
||||
|
||||
sb.append(LOG_SEPARATOR);
|
||||
if (seed > 3) {
|
||||
sb.append(getRandomFromArray(size.values()));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String getRandomSell() {
|
||||
return getRandomFromList(products);
|
||||
}
|
||||
|
||||
private static void initProducts(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
products.add((i + 1) + LOG_SEPARATOR + getRandomSearch(100) + LOG_SEPARATOR + getRandomPrice());
|
||||
}
|
||||
}
|
||||
|
||||
private enum size {XS, S, M, L, XL}
|
||||
|
||||
private enum colors {BLANC, NOIR, BLEU, VERT, ROSE, MARRON}
|
||||
|
||||
private enum cat {TSHIRT, DEBARDEUR, PULL, BOXER, CALCON, SLIP}
|
||||
|
||||
|
||||
private enum matiere {COTON, SOIE, SYNTHETIQUE}
|
||||
|
||||
static {
|
||||
readFromFile("ips.txt", ips);
|
||||
initProducts(100);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user