コード例 #1
0
ファイル: LibraryTest.java プロジェクト: maxymkasyanov/bookss
 private Timings getTimings(long[] times) {
   Timings timings = new Timings();
   timings.avg = LongStream.of(times).average().getAsDouble();
   timings.min = LongStream.of(times).min().getAsLong();
   timings.max = LongStream.of(times).max().getAsLong();
   return timings;
 }
コード例 #2
0
  public static void main(String... args) {

    /*
    -------------------------------------------------------------------------
    From obj to ...
    -------------------------------------------------------------------------
    */
    Stream<String> streamOfStrings = Stream.of("un", "deux", "trois");
    Function<String, StringBuilder> function = StringBuilder::new;
    streamOfStrings.map(function) /*.forEach(System.out::println)*/;
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToIntFunction<String> toIntFunction = String::length;
    IntStream streamOfInts = streamOfStrings.mapToInt(toIntFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToDoubleFunction<String> toDoubleFunction = String::length;
    DoubleStream streamOfDoubles = streamOfStrings.mapToDouble(toDoubleFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToLongFunction<String> toLongFunction = String::length;
    LongStream streamOfLongs = streamOfStrings.mapToLong(toLongFunction);

    /*
    -------------------------------------------------------------------------
    From int to ...
    -------------------------------------------------------------------------
    */
    IntUnaryOperator plusplus = i -> i++;
    IntStream.of(1, 2, 3).map(plusplus)
    /*.forEach(x->System.out.println(x))*/ ;
    IntFunction<String> intFunction = i -> "" + i;
    IntStream.of(1, 2, 3).mapToObj(intFunction)
    /*.forEach(System.out::println)*/ ;
    IntToDoubleFunction itdf = i -> i;
    IntStream.of(1, 2, 3).mapToDouble(itdf)
    /*.forEach(System.out::println)*/ ;
    IntToLongFunction itlf = i -> i;
    IntStream.of(1, 2, 3).mapToLong(itlf)
    /*.forEach(System.out::println)*/ ;

    /*
    -------------------------------------------------------------------------
    From long to ...
    -------------------------------------------------------------------------
    */
    LongUnaryOperator times = l -> l * l;
    LongStream.of(1L, 2L, 3L).map(times)
    /*.forEach(System.out::println)*/ ;
    LongFunction<String> lf = l -> "toto";
    LongStream.of(1L, 2L, 3L).mapToObj(lf);

    /*
    -------------------------------------------------------------------------
    From double to ...
    -------------------------------------------------------------------------
    */
    DoubleToIntFunction dtif = d -> (int) d;
    DoubleStream.of(1.3, 1.5, 1.6).mapToInt(dtif).forEach(System.out::println);
  }
コード例 #3
0
  private <E extends RuntimeException> void assertLongToDoubleFunction(
      LongToDoubleFunction test, Class<E> type) {
    assertNotNull(test);
    try {
      test.applyAsDouble(0L);
      fail();
    } catch (RuntimeException e) {
      assertException(type, e, "0");
    }

    try {
      LongStream.of(1L, 2L, 3L).mapToDouble(test);
    } catch (RuntimeException e) {
      assertException(type, e, "1");
    }
  }
コード例 #4
0
 private long executeSummarize(long[] values) {
   return LongStream.of(values).sum();
 }
コード例 #5
0
 @Override
 protected Long compute() {
   return LongStream.of(values).sum();
 }
コード例 #6
0
  public void run(final int numJobs, final int payloadSizeBytes, final int numConsumers) {

    luaQ.clear();

    final ExecutorService consumerExecutor = Executors.newFixedThreadPool(numConsumers);
    final List<Future<?>> consumerFutures = new ArrayList<>(numConsumers);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final AtomicBoolean donePublishing = new AtomicBoolean(false);
    final Map<Integer, long[]> publishClaimedStamps = new HashMap<>(MapUtils.capacity(numJobs));
    for (int i = 0; i < numJobs; i++) {
      publishClaimedStamps.put(i, new long[2]);
    }

    for (int i = 0; i < numConsumers; i++) {

      if (jedisPublisherAndPrototype == null) {
        consumerFutures.add(
            consumerExecutor.submit(
                new Consumer(luaQ, startLatch, donePublishing, publishClaimedStamps, 1)));
        continue;
      }

      final LuaQ directConsumerQ =
          new LuaQ(
              new DirectJedisExecutor(
                  new Jedis(
                      jedisPublisherAndPrototype.getClient().getHost(),
                      jedisPublisherAndPrototype.getClient().getPort())),
              ThroughputBenchmark.class.getSimpleName());

      consumerFutures.add(
          consumerExecutor.submit(
              new Consumer(directConsumerQ, startLatch, donePublishing, publishClaimedStamps, 1)));
    }

    final byte[] payload = new byte[payloadSizeBytes];
    Arrays.fill(payload, (byte) 1);

    startLatch.countDown();

    for (int i = 0; i < numJobs; i++) {
      final byte[] id = String.valueOf(i).getBytes(StandardCharsets.UTF_8);
      final long publishClaimedStamp = System.nanoTime();
      luaQ.publish(id, payload);
      publishClaimedStamps.get(i)[0] = publishClaimedStamp;
    }

    donePublishing.set(true);
    for (final Future<?> consumerFuture : consumerFutures) {
      Futures.getUnchecked(consumerFuture);
    }

    consumerExecutor.shutdown();

    final long[] latencyNanos =
        publishClaimedStamps
            .values()
            .stream()
            .mapToLong(publishClaimedStamp -> publishClaimedStamp[1] - publishClaimedStamp[0])
            .sorted()
            .toArray();

    System.out.printf("Min latency %.2f (ms)%n", latencyNanos[0] / 1000000.0);
    System.out.printf("Max latency %.2f (ms)%n", latencyNanos[latencyNanos.length - 1] / 1000000.0);

    final long medianNanos = latencyNanos[latencyNanos.length / 2];
    System.out.printf("Median latency %.2f (ms)%n", medianNanos / 1000000.0);

    final double avgNanos = LongStream.of(latencyNanos).average().orElse(-1);
    System.out.printf("Average latency %.2f (ms)%n", avgNanos / 1000000);
  }
コード例 #7
0
  @Override
  public NavigableMap<Long, Long> findEqualIds(Dictionary<Long> otherDict) {
    NavigableMap<Long, Long> res = new TreeMap<>();

    if (otherDict instanceof ArrayCompressedLongDictionary) {
      CompressedLongArray<?> otherSortedValues =
          ((ArrayCompressedLongDictionary) otherDict).sortedValues;
      if (sortedValues.size() == 0 || otherSortedValues.size() == 0) return res;

      // good case: we can traverse the two arrays simultaneously and only need to store O(1) in
      // memory.
      int posThis = 0;
      int posOther = 0;
      long decompressedThis = sortedValues.get(posThis);
      long decompressedOther = otherSortedValues.get(posOther);
      while (posThis < sortedValues.size() && posOther < otherSortedValues.size()) {
        // move 'posThis' right until decompressedThis is >= decompressedOther
        while (posThis < sortedValues.size() - 1 && decompressedThis < decompressedOther)
          decompressedThis = sortedValues.get(++posThis);

        // move 'posOther' right until decompressedOther is >= decompressedThis
        while (posOther < otherSortedValues.size() - 1 && decompressedOther < decompressedThis)
          decompressedOther = otherSortedValues.get(++posOther);

        // validate if we have a match
        if (decompressedThis == decompressedOther) {
          res.put((long) posThis++, (long) posOther++);
          if (posThis < sortedValues.size() && posOther < otherSortedValues.size()) {
            decompressedThis = sortedValues.get(posThis);
            decompressedOther = otherSortedValues.get(posOther);
          }
        } else if ((posThis == sortedValues.size() - 1 && decompressedThis < decompressedOther)
            || (posThis == sortedValues.size() - 1 && posOther == otherSortedValues.size() - 1))
          break;
      }
    } else if (otherDict instanceof ConstantLongDictionary) {
      long otherId = ((ConstantLongDictionary) otherDict).getId();
      long otherValue = ((ConstantLongDictionary) otherDict).getDecompressedValue();

      try {
        long ourId = findIdOfValue(otherValue);
        res.put(ourId, otherId);
      } catch (IllegalArgumentException e) {
        // swallow, return empty dict.
      }
    } else if (otherDict instanceof EmptyLongDictionary) {
      // noop.
    } else {
      // Bad case: decompress whole array (should not happen, though)
      long[] decompressedValues = sortedValues.decompressedArray();
      Long[] otherIds =
          otherDict.findIdsOfValues(
              LongStream.of(decompressedValues).mapToObj(Long::valueOf).toArray(l -> new Long[l]));
      for (int i = 0; i < decompressedValues.length; i++) {
        Long otherId = otherIds[i];
        if (otherId != -1L) res.put((long) i, otherId);
      }
    }

    return res;
  }