public static void ensureSidesAddUp(Map<GroupedSearchResultRow, Long> result) { Map<String, LongSummaryStatistics> sideTotals = result .entrySet() .stream() .collect( Collectors.groupingBy( e -> e.getKey().getFieldName(), Collectors.summarizingLong(e -> e.getValue().longValue()))); int numDistinctValues = sideTotals .values() .stream() .map(LongSummaryStatistics::getSum) .distinct() .collect(Collectors.toList()) .size(); if (numDistinctValues != 1) { log.error("Sides do not add up"); sideTotals.entrySet().forEach(e -> log.info("{}: {}", e.getKey(), e.getValue().getSum())); } assertEquals(1, numDistinctValues); }
@Override public String recommendations() { List<Metric> metrics = data.entrySet().stream() .map(entry -> entry.getValue().entrySet().stream() .map(data -> new Metric(entry.getKey(), data.getKey(), data.getValue())) .collect(Collectors.toList())) .collect(ArrayList::new, List::addAll, List::addAll); Map<String, List<Metric>> runaways = new HashMap<>(); metrics.stream().collect(Collectors.groupingBy(Metric::name)).entrySet().forEach(entry -> { LongSummaryStatistics statistics = entry.getValue().stream() .collect(Collectors.summarizingLong(Metric::value)); runaways.put(entry.getKey(), entry.getValue().stream().filter(metric -> metric.value() <= statistics.getAverage()) .sorted((m1, m2) -> (int) (m1.value() - m2.value())).collect(Collectors.toList())); }); return this.buildRunaways(runaways); }
public void testLongStatistics() { List<LongSummaryStatistics> instances = new ArrayList<>(); instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i))); instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics()); instances.add(countTo(1000).stream().mapToLong(i -> i).collect(LongSummaryStatistics::new, LongSummaryStatistics::accept, LongSummaryStatistics::combine)); instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i))); instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics()); instances.add(countTo(1000).parallelStream().mapToLong(i -> i).collect(LongSummaryStatistics::new, LongSummaryStatistics::accept, LongSummaryStatistics::combine)); for (LongSummaryStatistics stats : instances) { assertEquals(stats.getCount(), 1000); assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum()); assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount()); assertEquals(stats.getMax(), 1000L); assertEquals(stats.getMin(), 1L); } }
public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("bootstrap.servers", args[0]); props.setProperty("group.id", UUID.randomUUID().toString()); props.setProperty("key.deserializer", LongDeserializer.class.getName()); props.setProperty("value.deserializer", TradeDeserializer.class.getName()); props.setProperty("auto.offset.reset", "earliest"); KafkaConsumer<Long, Trade> consumer = new KafkaConsumer<>(props); List<String> topics = Arrays.asList(args[1]); consumer.subscribe(topics); System.out.println("Subscribed to topics " + topics); long count = 0; long start = System.nanoTime(); while (true) { ConsumerRecords<Long, Trade> poll = consumer.poll(5000); System.out.println("Partitions in batch: " + poll.partitions()); LongSummaryStatistics stats = StreamSupport.stream(poll.spliterator(), false) .mapToLong(r -> r.value().getTime()).summaryStatistics(); System.out.println("Oldest record time: " + stats.getMin() + ", newest record: " + stats.getMax()); count += poll.count(); long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); long rate = (long) ((double) count / elapsed * 1000); System.out.printf("Total count: %,d in %,dms. Average rate: %,d records/s %n", count, elapsed, rate); } }
public static long minMaxDiff(String path) throws IOException { LongSummaryStatistics stats = Files.list(Paths.get(path)) .flatMap(f -> uncheckCall(() -> Files.lines(f))) .mapToLong(l -> { try { String ts = l.split(",")[3]; if (ts.length() != 13) { throw new IllegalArgumentException(); } return Long.valueOf(ts); } catch (Exception ignored) { System.out.println("Malformed line: " + l); return Long.MIN_VALUE; } }).filter(l -> l > Long.MIN_VALUE) .summaryStatistics(); return stats.getMax() - stats.getMin(); }
public static void main(String[] args) throws IOException { List<Salary> list = SalaryFileUtils.readFromFile(); long start = System.currentTimeMillis(); HashBiMap<String, LongSummaryStatistics> groupMap = list.parallelStream() .filter(s -> s.getTotalIncome() > 100000) .collect( Collectors.groupingBy( Salary::namePrefix, () -> HashBiMap.create(), Collectors.summarizingLong(Salary::getTotalIncome) ) ); groupMap.values() .parallelStream() .sorted(Comparator.comparingLong(LongSummaryStatistics::getSum).reversed()) // 默认是从小到大排序 .limit(10) .forEachOrdered(ls -> { System.out.format("[%s], count: %s, sum: %s \n", groupMap.inverse().get(ls), ls.getCount(), ls.getSum()); }); System.out.println("elapsed time : " + (System.currentTimeMillis() - start)); }
@Override public String getSummary() { StringBuilder retval = new StringBuilder("PZXPilotBlock:\n"); LongSummaryStatistics stats = pulses.getPulseLengths() .subList(0, pulses.getPulseLengths().size() - 2) .stream().mapToLong(x -> x).summaryStatistics(); retval.append("Average pilot pulse:").append(Math.round(stats.getAverage())).append(" tstates, ") .append(String.format("%.2f", stats.getAverage()/ PILOT_LENGTH*100.0)).append("% of expected\n"); retval.append("Sync1 pulse:").append(sync1Length).append(" tstates, ") .append(String.format("%.2f", (double)sync1Length/SYNC1*100.0)).append("% of expected\n"); retval.append("Sync2 pulse:").append(sync2Length).append(" tstates, ") .append(String.format("%.2f", (double)sync2Length/SYNC2*100.0)).append("% of expected\n"); retval.append(pulses.toString()); return retval.toString(); }
/** * * @param type the value of type * @param standardPulse the value of standardPulse * @param stats the value of stats * @return the String */ private String getSummaryText(String type, int standardPulse, LongSummaryStatistics stats) { StringBuilder retval = new StringBuilder(); long low = stats.getMin(); long high = stats.getMax(); double average = stats.getAverage(); retval.append("shortest ").append(type).append(" pulse:").append(low) .append(" tstates, longest ").append(type).append(" pulse:") .append(high).append(" tstates\n"); retval.append("shortest ").append(type).append(" pulse ") .append(String.format("%.2f", (double)low/standardPulse*100.0)) .append("% of expected, longest ").append(type) .append(" pulse ").append(String.format("%.2f", (double)high/standardPulse*100.0)) .append("% of expected\n"); retval.append("average ").append(type).append(" pulse:").append(String.format("%.2f", average)) .append("\n"); return retval.toString(); }
@Test public void testSummarizing() { withRandom(r -> { long[] data = LongStreamEx.of(r, 1000, 1, Long.MAX_VALUE).toArray(); LongSummaryStatistics expected = LongStream.of(data).summaryStatistics(); LongSummaryStatistics statistics = LongStreamEx.of(data).collect(LongCollector.summarizing()); assertEquals(expected.getCount(), statistics.getCount()); assertEquals(expected.getSum(), statistics.getSum()); assertEquals(expected.getMax(), statistics.getMax()); assertEquals(expected.getMin(), statistics.getMin()); statistics = LongStreamEx.of(data).parallel().collect(LongCollector.summarizing()); assertEquals(expected.getCount(), statistics.getCount()); assertEquals(expected.getSum(), statistics.getSum()); assertEquals(expected.getMax(), statistics.getMax()); assertEquals(expected.getMin(), statistics.getMin()); }); }
@Test public void counters() { Registry r = newRegistry(true, 10000); r.counter("foo").increment(); r.counter("foo", "a", "1", "b", "2").increment(); r.counter("foo", "a", "1", "b", "3").increment(13L); r.counter("foo", "a", "1", "b", "2").increment(); r.counter("bar", "a", "1", "b", "2").increment(); Assert.assertEquals(4, r.counters().count()); final LongSummaryStatistics summary = r.counters() .filter(Functions.nameEquals("foo")) .collect(Collectors.summarizingLong(Counter::count)); Assert.assertEquals(3L, summary.getCount()); Assert.assertEquals(16L, summary.getSum()); Assert.assertEquals(13L, summary.getMax()); }
@Test public void timers() { Registry r = newRegistry(true, 10000); r.timer("foo").record(1L, TimeUnit.NANOSECONDS); r.timer("foo", "a", "1", "b", "2").record(1L, TimeUnit.NANOSECONDS); r.timer("foo", "a", "1", "b", "3").record(13L, TimeUnit.NANOSECONDS); r.timer("foo", "a", "1", "b", "2").record(1L, TimeUnit.NANOSECONDS); r.timer("bar", "a", "1", "b", "2").record(1L, TimeUnit.NANOSECONDS); Assert.assertEquals(4, r.timers().count()); final LongSummaryStatistics countSummary = r.timers() .filter(Functions.nameEquals("foo")) .collect(Collectors.summarizingLong(Timer::count)); Assert.assertEquals(3L, countSummary.getCount()); Assert.assertEquals(4L, countSummary.getSum()); Assert.assertEquals(2L, countSummary.getMax()); final LongSummaryStatistics totalSummary = r.timers() .filter(Functions.nameEquals("foo")) .collect(Collectors.summarizingLong(Timer::totalTime)); Assert.assertEquals(3L, totalSummary.getCount()); Assert.assertEquals(16L, totalSummary.getSum()); Assert.assertEquals(13L, totalSummary.getMax()); }
@Test public void distributionSummaries() { Registry r = newRegistry(true, 10000); r.distributionSummary("foo").record(1L); r.distributionSummary("foo", "a", "1", "b", "2").record(1L); r.distributionSummary("foo", "a", "1", "b", "3").record(13L); r.distributionSummary("foo", "a", "1", "b", "2").record(1L); r.distributionSummary("bar", "a", "1", "b", "2").record(1L); Assert.assertEquals(4, r.distributionSummaries().count()); final LongSummaryStatistics countSummary = r.distributionSummaries() .filter(Functions.nameEquals("foo")) .collect(Collectors.summarizingLong(DistributionSummary::count)); Assert.assertEquals(3L, countSummary.getCount()); Assert.assertEquals(4L, countSummary.getSum()); Assert.assertEquals(2L, countSummary.getMax()); final LongSummaryStatistics totalSummary = r.distributionSummaries() .filter(Functions.nameEquals("foo")) .collect(Collectors.summarizingLong(DistributionSummary::totalAmount)); Assert.assertEquals(3L, totalSummary.getCount()); Assert.assertEquals(16L, totalSummary.getSum()); Assert.assertEquals(13L, totalSummary.getMax()); }
@Test public void long_summary_stats_with_stream() { LongSummaryStatistics stats = shipments.stream() .mapToLong((x) -> x.getCost()).summaryStatistics(); // average assertEquals(310.25, stats.getAverage(), 0); // count assertEquals(4, stats.getCount(), 0); // max assertEquals(901.0, stats.getMax(), 0); // min assertEquals(45.0, stats.getMin(), 0); // sum assertEquals(1241.0, stats.getSum(), 0); }
@Test public void long_summary_stats_stream_reduction_target() { LongSummaryStatistics stats = shipments.stream().collect( Collectors.summarizingLong(Shipment::getCost)); // average assertEquals(310.25, stats.getAverage(), 0); // count assertEquals(4, stats.getCount(), 0); // max assertEquals(901.0, stats.getMax(), 0); // min assertEquals(45.0, stats.getMin(), 0); // sum assertEquals(1241.0, stats.getSum(), 0); }
public String toString() { if (process_durations.isEmpty()) { return null; } TableList list = new TableList(); LongSummaryStatistics stats = getStats(); list.addRow("entries", stats.getCount(), 1, 0, "measured (" + max_entry_count + " max)", Locale.US); list.addRow("total time", stats.getSum(), 1000, 3, "sec", Locale.US); list.addRow("min", stats.getMin(), 1000, 3, "sec", Locale.US); list.addRow("average", stats.getAverage(), 1000, 3, "sec", Locale.US); list.addRow("max", stats.getMax(), 1000, 3, "sec", Locale.US); if (stats.getMax() != slow_duration) { System.err.println(slow_duration); } if (slow_ressource_name != null) { list.addRow("slower", slow_ressource_name); } return list.toString(); }
@GetMapping public ObjectNode get() { LongSummaryStatistics statistics = monitor.getStatistics(); return JsonNodeFactory.instance.objectNode(). put("average-duration", statistics.getAverage()). put("invocation-count", statistics.getCount()). put("min-duration", statistics.getMin()). put("max-duration", statistics.getMax()); }
private static void displayStatistics(LongSummaryStatistics stats) { System.out.println("Statistics"); System.out.printf(" count: %,d%n", stats.getCount()); System.out.printf(" sum: %,d%n", stats.getSum()); System.out.printf(" min: %,d%n", stats.getMin()); System.out.printf(" max: %,d%n", stats.getMax()); System.out.printf(" average: %f%n", stats.getAverage()); }
public void testLongStatistics() { List<LongSummaryStatistics> instances = new ArrayList<>(); instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i))); instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics()); instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i))); instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics()); for (LongSummaryStatistics stats : instances) { assertEquals(stats.getCount(), 1000); assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum()); assertEquals(stats.getMax(), 1000L); assertEquals(stats.getMin(), 1L); } }
public void testLongCollectNull() { checkNPE(() -> LongStream.of(1).collect(null, LongSummaryStatistics::accept, LongSummaryStatistics::combine)); checkNPE(() -> LongStream.of(1).collect(LongSummaryStatistics::new, null, LongSummaryStatistics::combine)); checkNPE(() -> LongStream.of(1).collect(LongSummaryStatistics::new, LongSummaryStatistics::accept, null)); }
private static void gatherStatistics(final LongSummaryStatistics stats, final Message message) { try { final long duration = System.currentTimeMillis() - message.getJMSTimestamp(); stats.accept(duration); } catch (final JMSException e) { LOG.error("Failed to get timestamp from message: {}", e.getMessage()); } }
@Override public void completePulseBlock(boolean isPilot) { PZXBlock newBlock; if(pulseLengths.isEmpty()) { newBlock = new PZXNullBlock(); loaderResult.add(newBlock); Logger.getLogger(LoaderContextImpl.class.getName()).log(Level.INFO, newBlock.getSummary()); return; } if(isPilot) { newBlock = new PZXPilotBlock(getPulseListForCurrentPulses()); LongSummaryStatistics stats = getSummaryStats(pilotPulses); Logger.getLogger(LoaderContextImpl.class.getName()).log(Level.INFO, getSummaryText("pilot", PILOT_LENGTH, stats)); // if average PILOT_LENGTH pulse length is not plausibly the same as standard, record this as a non-pilot block if(!PulseUtils.equalWithinResolution(PILOT_LENGTH, stats.getAverage(), resolution)) { completePulseBlock(false); return; } } else { newBlock = new PZXPulseBlock(getPulseListForCurrentPulses()); } Logger.getLogger(LoaderContextImpl.class.getName()).log(Level.FINE, newBlock.getSummary()); loaderResult.add(newBlock); pulseLengths.clear(); resetBlock(); }
@Override public void completeDataBlock() { if(pulseLengths.isEmpty()) { return; } ImmutableList<Byte> data = dataBuilder.getData(); int numBitsInLastByte = dataBuilder.getNumBitsInCurrentByte(); LongSummaryStatistics zeroStats = getSummaryStats(zeroPulses); Logger.getLogger(LoaderContextImpl.class.getName()).log(Level.INFO, getSummaryText("zero", ZERO, zeroStats)); LongSummaryStatistics oneStats = getSummaryStats (onePulses); Logger.getLogger(LoaderContextImpl.class.getName()).log(Level.INFO, getSummaryText("one", ONE, oneStats)); // TODO: use average ZERO pulse length unless idealised, actually - only create data block if average zero pulse // credibly resembles the standard zero pulse, the recognition routines seem to be close to handling standard // speed loaders where the standard routines just have shorter timing constants than the standard ROM routines if(data.isEmpty() || (!PulseUtils.equalWithinResolution(ZERO, zeroStats.getAverage(), resolution) && zeroStats.getCount() != 0) || // TODO: use average ONE pulse length unless idealised, actually - only create data block if average one pulse // credibly resembles the standard one pulse, the recognition routines seem to be close to handling standard // speed loaders where the standard routines just have shorter timing constants than the standard ROM routines (!PulseUtils.equalWithinResolution(ONE, oneStats.getAverage(), resolution) && oneStats.getCount() != 0)) { // Something was wrong with this block as a data block, try again to record it as a plain pulse block completePulseBlock(false); return; } PZXDataBlock newBlock = new PZXDataBlock(getPulseListForCurrentPulses(), numBitsInLastByte, data); Logger.getLogger(LoaderContextImpl.class.getName()).log(Level.INFO, newBlock.getSummary()); loaderResult.add(newBlock); pulseLengths.clear(); resetBlock(); }
@Test public void summaryStatistics() { LongSummaryStatistics result = this.parallelStreamSupportMock.summaryStatistics(); verify(this.delegateMock).summaryStatistics(); assertEquals(this.summaryStatistics, result); }
/** * {@code Serializable} variant of {@link * Collectors#summarizingLong(ToLongFunction) * java.util.stream.Collectors#summarizingLong(ToLongFunction)} */ public static <T> DistributedCollector<T, ?, LongSummaryStatistics> summarizingLong( DistributedToLongFunction<? super T> mapper ) { return new DistributedCollectorImpl<>( DistributedLongSummaryStatistics::new, (a, t) -> a.accept(mapper.applyAsLong(t)), (s1, s2) -> { s1.combine(s2); return s1; }); }
@Test public void summaryStatistics() { LongSummaryStatistics longSummaryStatistics = stream.summaryStatistics(); assertEquals(COUNT, longSummaryStatistics.getCount()); assertEquals(COUNT - 1, longSummaryStatistics.getMax()); assertEquals(0, longSummaryStatistics.getMin()); assertEquals(COUNT * (COUNT - 1) / 2, longSummaryStatistics.getSum()); assertEquals((COUNT - 1) / 2.0, longSummaryStatistics.getAverage(), 0.0); }
@Test public void list_summarizingLong() { LongSummaryStatistics summary = streamList().collect(DistributedCollectors.summarizingLong(m -> (long) m)); assertEquals(COUNT, summary.getCount()); assertEquals(COUNT - 1, summary.getMax()); assertEquals(0, summary.getMin()); assertEquals(COUNT * (COUNT - 1) / 2, summary.getSum()); assertEquals((COUNT - 1) / 2d, summary.getAverage(), 0d); }
@Test public void testSendOrderSingleThreadWithoutPromises() throws Exception { server = createTransport(); int iterationNum = 11; // +1 warm up iteration int sentPerIteration = 1000; long[] iterationTimeSeries = new long[iterationNum - 1]; for (int i = 0; i < iterationNum; i++) { LOGGER.info("####### {} : iteration = {}", testName.getMethodName(), i); client = createTransport(); final List<Message> received = new ArrayList<>(); final CountDownLatch latch = new CountDownLatch(sentPerIteration); Subscriber<Message> serverSubscriber = Subscribers.create(message -> { received.add(message); latch.countDown(); }); server.listen().subscribe(serverSubscriber); long startAt = System.currentTimeMillis(); for (int j = 0; j < sentPerIteration; j++) { client.send(server.address(), Message.fromQualifier("q" + j)); } latch.await(20, TimeUnit.SECONDS); long iterationTime = System.currentTimeMillis() - startAt; if (i > 0) { // exclude warm up iteration iterationTimeSeries[i - 1] = iterationTime; } assertSendOrder(sentPerIteration, received); LOGGER.info("Iteration time: {} ms", iterationTime); serverSubscriber.unsubscribe(); destroyTransport(client); } LongSummaryStatistics iterationTimeStats = LongStream.of(iterationTimeSeries).summaryStatistics(); LOGGER.info("Iteration time stats (ms): {}", iterationTimeStats); }
private LongSummaryStatistics computeMessageSentStats(List<GossipProtocolImpl> gossipProtocols) { List<Long> messageSentPerNode = new ArrayList<>(gossipProtocols.size()); for (GossipProtocolImpl gossipProtocol : gossipProtocols) { Transport transport = gossipProtocol.getTransport(); messageSentPerNode.add(transport.networkEmulator().totalMessageSentCount()); } return messageSentPerNode.stream().mapToLong(v -> v).summaryStatistics(); }
private LongSummaryStatistics computeMessageLostStats(List<GossipProtocolImpl> gossipProtocols) { List<Long> messageLostPerNode = new ArrayList<>(gossipProtocols.size()); for (GossipProtocolImpl gossipProtocol : gossipProtocols) { Transport transport = gossipProtocol.getTransport(); messageLostPerNode.add(transport.networkEmulator().totalMessageLostCount()); } return messageLostPerNode.stream().mapToLong(v -> v).summaryStatistics(); }
public static <T, R> void printLongStats( Stream<T> stream, ToLongFunction<T> summarizingFunction ) { LongSummaryStatistics summary = stream.collect(Collectors.summarizingLong(summarizingFunction)); System.out.println(summary.getCount()); System.out.println(summary.getSum()); System.out.println(summary.getMin()); System.out.println(summary.getMax()); System.out.println(summary.getAverage()); }