Search in sources :

Example 1 with EPSGeneratedMonitor

use of org.apache.metron.performance.load.monitor.EPSGeneratedMonitor in project metron by apache.

the class LoadGenerator method main.

public static void main(String[] args) throws Exception {
    CommandLine cli = LoadOptions.parse(new PosixParser(), args);
    EnumMap<LoadOptions, Optional<Object>> evaluatedArgs = LoadOptions.createConfig(cli);
    Map<String, Object> kafkaConfig = new HashMap<>();
    kafkaConfig.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    kafkaConfig.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    kafkaConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    kafkaConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    if (LoadOptions.ZK.has(cli)) {
        String zkQuorum = (String) evaluatedArgs.get(LoadOptions.ZK).get();
        kafkaConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Joiner.on(",").join(KafkaUtils.INSTANCE.getBrokersFromZookeeper(zkQuorum)));
    }
    String groupId = evaluatedArgs.get(LoadOptions.CONSUMER_GROUP).get().toString();
    System.out.println("Consumer Group: " + groupId);
    kafkaConfig.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
    if (LoadOptions.KAFKA_CONFIG.has(cli)) {
        kafkaConfig.putAll((Map<String, Object>) evaluatedArgs.get(LoadOptions.KAFKA_CONFIG).get());
    }
    kafkaProducer = ThreadLocal.withInitial(() -> new KafkaProducer<>(kafkaConfig));
    int numThreads = (int) evaluatedArgs.get(LoadOptions.NUM_THREADS).get();
    System.out.println("Thread pool size: " + numThreads);
    pool = Executors.newFixedThreadPool(numThreads);
    Optional<Object> eps = evaluatedArgs.get(LoadOptions.EPS);
    Optional<Object> outputTopic = evaluatedArgs.get(LoadOptions.OUTPUT_TOPIC);
    Optional<Object> monitorTopic = evaluatedArgs.get(LoadOptions.MONITOR_TOPIC);
    long sendDelta = (long) evaluatedArgs.get(LoadOptions.SEND_DELTA).get();
    long monitorDelta = (long) evaluatedArgs.get(LoadOptions.MONITOR_DELTA).get();
    if ((eps.isPresent() && outputTopic.isPresent()) || monitorTopic.isPresent()) {
        Timer timer = new Timer(false);
        long startTimeMs = System.currentTimeMillis();
        if (outputTopic.isPresent() && eps.isPresent()) {
            List<String> templates = (List<String>) evaluatedArgs.get(LoadOptions.TEMPLATE).get();
            if (templates.isEmpty()) {
                System.out.println("Empty templates, so nothing to do.");
                return;
            }
            Optional<Object> biases = evaluatedArgs.get(LoadOptions.BIASED_SAMPLE);
            Sampler sampler = new UnbiasedSampler();
            if (biases.isPresent()) {
                sampler = new BiasedSampler((List<Map.Entry<Integer, Integer>>) biases.get(), templates.size());
            }
            MessageGenerator generator = new MessageGenerator(templates, sampler);
            Long targetLoad = (Long) eps.get();
            int periodsPerSecond = (int) (1000 / sendDelta);
            long messagesPerPeriod = targetLoad / periodsPerSecond;
            String outputTopicStr = (String) outputTopic.get();
            System.out.println("Generating data to " + outputTopicStr + " at " + targetLoad + " events per second");
            System.out.println("Sending " + messagesPerPeriod + " messages to " + outputTopicStr + " every " + sendDelta + "ms");
            timer.scheduleAtFixedRate(new SendToKafka(outputTopicStr, messagesPerPeriod, numThreads, generator, pool, numSent, kafkaProducer), 0, sendDelta);
        }
        List<AbstractMonitor> monitors = new ArrayList<>();
        if (outputTopic.isPresent() && monitorTopic.isPresent()) {
            System.out.println("Monitoring " + monitorTopic.get() + " every " + monitorDelta + " ms");
            monitors.add(new EPSGeneratedMonitor(outputTopic, numSent));
            monitors.add(new EPSThroughputWrittenMonitor(monitorTopic, kafkaConfig));
        } else if (outputTopic.isPresent() && !monitorTopic.isPresent()) {
            System.out.println("Monitoring " + outputTopic.get() + " every " + monitorDelta + " ms");
            monitors.add(new EPSGeneratedMonitor(outputTopic, numSent));
            monitors.add(new EPSThroughputWrittenMonitor(outputTopic, kafkaConfig));
        } else if (!outputTopic.isPresent() && monitorTopic.isPresent()) {
            System.out.println("Monitoring " + monitorTopic.get() + " every " + monitorDelta + " ms");
            monitors.add(new EPSThroughputWrittenMonitor(monitorTopic, kafkaConfig));
        } else if (!outputTopic.isPresent() && !monitorTopic.isPresent()) {
            System.out.println("You have not specified an output topic or a monitoring topic, so I have nothing to do here.");
        }
        int lookback = (int) evaluatedArgs.get(LoadOptions.SUMMARY_LOOKBACK).get();
        if (lookback > 0) {
            System.out.println("Summarizing over the last " + lookback + " monitoring periods (" + lookback * monitorDelta + "ms)");
        } else {
            System.out.println("Turning off summarization.");
        }
        final CSVWriter csvWriter = new CSVWriter((File) evaluatedArgs.get(LoadOptions.CSV).orElse(null));
        Writer writer = new Writer(monitors, lookback, new ArrayList<Consumer<Writable>>() {

            {
                add(new ConsoleWriter());
                add(csvWriter);
            }
        });
        timer.scheduleAtFixedRate(new MonitorTask(writer), 0, monitorDelta);
        Optional<Object> timeLimit = evaluatedArgs.get(LoadOptions.TIME_LIMIT);
        if (timeLimit.isPresent()) {
            System.out.println("Ending in " + timeLimit.get() + " ms.");
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    timer.cancel();
                    long durationS = (System.currentTimeMillis() - startTimeMs) / 1000;
                    System.out.println("\nGenerated " + numSent.get() + " in " + durationS + " seconds.");
                    csvWriter.close();
                    System.exit(0);
                }
            }, (Long) timeLimit.get());
        }
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) HashMap(java.util.HashMap) PosixParser(org.apache.commons.cli.PosixParser) ArrayList(java.util.ArrayList) ConsoleWriter(org.apache.metron.performance.load.monitor.writers.ConsoleWriter) CSVWriter(org.apache.metron.performance.load.monitor.writers.CSVWriter) MonitorTask(org.apache.metron.performance.load.monitor.MonitorTask) UnbiasedSampler(org.apache.metron.performance.sampler.UnbiasedSampler) Consumer(java.util.function.Consumer) TimerTask(java.util.TimerTask) ArrayList(java.util.ArrayList) List(java.util.List) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Optional(java.util.Optional) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) BiasedSampler(org.apache.metron.performance.sampler.BiasedSampler) EPSGeneratedMonitor(org.apache.metron.performance.load.monitor.EPSGeneratedMonitor) CommandLine(org.apache.commons.cli.CommandLine) Timer(java.util.Timer) AbstractMonitor(org.apache.metron.performance.load.monitor.AbstractMonitor) BiasedSampler(org.apache.metron.performance.sampler.BiasedSampler) Sampler(org.apache.metron.performance.sampler.Sampler) UnbiasedSampler(org.apache.metron.performance.sampler.UnbiasedSampler) AtomicLong(java.util.concurrent.atomic.AtomicLong) EPSThroughputWrittenMonitor(org.apache.metron.performance.load.monitor.EPSThroughputWrittenMonitor) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) Writer(org.apache.metron.performance.load.monitor.writers.Writer) ConsoleWriter(org.apache.metron.performance.load.monitor.writers.ConsoleWriter) CSVWriter(org.apache.metron.performance.load.monitor.writers.CSVWriter)

Aggregations

ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1 CommandLine (org.apache.commons.cli.CommandLine)1 PosixParser (org.apache.commons.cli.PosixParser)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 AbstractMonitor (org.apache.metron.performance.load.monitor.AbstractMonitor)1 EPSGeneratedMonitor (org.apache.metron.performance.load.monitor.EPSGeneratedMonitor)1 EPSThroughputWrittenMonitor (org.apache.metron.performance.load.monitor.EPSThroughputWrittenMonitor)1 MonitorTask (org.apache.metron.performance.load.monitor.MonitorTask)1 CSVWriter (org.apache.metron.performance.load.monitor.writers.CSVWriter)1