|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.pekko.util |
| 19 | + |
| 20 | +import java.io.{ ByteArrayInputStream, InputStream } |
| 21 | +import java.util.concurrent.TimeUnit |
| 22 | + |
| 23 | +import org.openjdk.jmh.annotations._ |
| 24 | +import org.openjdk.jmh.infra.Blackhole |
| 25 | + |
| 26 | +/** |
| 27 | + * Compares ByteString.asInputStream and new ByteStreamArray(ByteString.toArray). |
| 28 | + */ |
| 29 | +@State(Scope.Benchmark) |
| 30 | +@Measurement(timeUnit = TimeUnit.MILLISECONDS) |
| 31 | +class ByteString_asInputStream_Benchmark { |
| 32 | + |
| 33 | + var bs: ByteString = _ |
| 34 | + |
| 35 | + var composed: ByteString = _ |
| 36 | + |
| 37 | + @Param(Array("10", "100", "1000")) |
| 38 | + var kb = 0 |
| 39 | + |
| 40 | + /* |
| 41 | + bench-jmh/jmh:run -f 1 -wi 3 -i 3 .*ByteString_asInputStream_Benchmark.* |
| 42 | +
|
| 43 | + [info] Benchmark (kb) Mode Cnt Score Error Units |
| 44 | + [info] ByteString_asInputStream_Benchmark.composed_bs_as_input_stream 10 thrpt 3 32398.229 ± 26714.266 ops/s |
| 45 | + [info] ByteString_asInputStream_Benchmark.composed_bs_as_input_stream 100 thrpt 3 3642.487 ± 576.459 ops/s |
| 46 | + [info] ByteString_asInputStream_Benchmark.composed_bs_as_input_stream 1000 thrpt 3 285.910 ± 40.463 ops/s |
| 47 | + [info] ByteString_asInputStream_Benchmark.composed_bs_bytes_to_input_stream 10 thrpt 3 6182.509 ± 933.899 ops/s |
| 48 | + [info] ByteString_asInputStream_Benchmark.composed_bs_bytes_to_input_stream 100 thrpt 3 474.634 ± 84.763 ops/s |
| 49 | + [info] ByteString_asInputStream_Benchmark.composed_bs_bytes_to_input_stream 1000 thrpt 3 38.764 ± 49.698 ops/s |
| 50 | + [info] ByteString_asInputStream_Benchmark.single_bs_as_input_stream 10 thrpt 3 2436952.866 ± 1253216.244 ops/s |
| 51 | + [info] ByteString_asInputStream_Benchmark.single_bs_as_input_stream 100 thrpt 3 339116.689 ± 297756.892 ops/s |
| 52 | + [info] ByteString_asInputStream_Benchmark.single_bs_as_input_stream 1000 thrpt 3 32592.451 ± 12465.507 ops/s |
| 53 | + [info] ByteString_asInputStream_Benchmark.single_bs_bytes_to_input_stream 10 thrpt 3 619077.237 ± 200242.708 ops/s |
| 54 | + [info] ByteString_asInputStream_Benchmark.single_bs_bytes_to_input_stream 100 thrpt 3 50481.984 ± 78485.741 ops/s |
| 55 | + [info] ByteString_asInputStream_Benchmark.single_bs_bytes_to_input_stream 1000 thrpt 3 4271.984 ± 1061.978 ops/s |
| 56 | + */ |
| 57 | + |
| 58 | + @Setup |
| 59 | + def setup(): Unit = { |
| 60 | + val bytes = Array.ofDim[Byte](1024 * kb) |
| 61 | + bs = ByteString(bytes) |
| 62 | + composed = ByteString.empty |
| 63 | + for (_ <- 0 to 100) { |
| 64 | + composed = composed ++ bs |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Benchmark |
| 69 | + def single_bs_bytes_to_input_stream(blackhole: Blackhole): Unit = { |
| 70 | + blackhole.consume(countBytes(new ByteArrayInputStream(bs.toArray))) |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + def composed_bs_bytes_to_input_stream(blackhole: Blackhole): Unit = { |
| 75 | + blackhole.consume(countBytes(new ByteArrayInputStream(composed.toArray))) |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + def single_bs_as_input_stream(blackhole: Blackhole): Unit = { |
| 80 | + blackhole.consume(countBytes(bs.asInputStream)) |
| 81 | + } |
| 82 | + |
| 83 | + @Benchmark |
| 84 | + def composed_bs_as_input_stream(blackhole: Blackhole): Unit = { |
| 85 | + blackhole.consume(countBytes(composed.asInputStream)) |
| 86 | + } |
| 87 | + |
| 88 | + private def countBytes(stream: InputStream): Int = { |
| 89 | + val buffer = new Array[Byte](1024) |
| 90 | + var count = 0 |
| 91 | + var read = stream.read(buffer) |
| 92 | + while (read != -1) { |
| 93 | + count += read |
| 94 | + read = stream.read(buffer) |
| 95 | + } |
| 96 | + count |
| 97 | + } |
| 98 | +} |
0 commit comments