|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.benchmark.bitmap; |
| 20 | + |
| 21 | +import org.apache.paimon.benchmark.Benchmark; |
| 22 | +import org.apache.paimon.fs.local.LocalFileIO; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.io.TempDir; |
| 26 | +import org.roaringbitmap.RoaringBitmap; |
| 27 | + |
| 28 | +import java.io.DataInputStream; |
| 29 | +import java.io.DataOutputStream; |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileOutputStream; |
| 32 | +import java.io.IOException; |
| 33 | +import java.nio.ByteBuffer; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.util.Random; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** Benchmark for {@link RoaringBitmap}. */ |
| 40 | +public class RoaringBitmapBenchmark { |
| 41 | + |
| 42 | + public static final int ROW_COUNT = 10000000; |
| 43 | + |
| 44 | + @TempDir Path tempDir; |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testDeserialize() throws Exception { |
| 48 | + Random random = new Random(); |
| 49 | + RoaringBitmap bitmap = new RoaringBitmap(); |
| 50 | + for (int i = 0; i < ROW_COUNT; i++) { |
| 51 | + if (random.nextBoolean()) { |
| 52 | + bitmap.add(i); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + File file = new File(tempDir.toFile(), "bitmap32-deserialize-benchmark"); |
| 57 | + assertThat(file.createNewFile()).isTrue(); |
| 58 | + try (FileOutputStream output = new FileOutputStream(file); |
| 59 | + DataOutputStream dos = new DataOutputStream(output)) { |
| 60 | + bitmap.serialize(dos); |
| 61 | + } |
| 62 | + |
| 63 | + Benchmark benchmark = |
| 64 | + new Benchmark("bitmap32-deserialize-benchmark", 100) |
| 65 | + .setNumWarmupIters(1) |
| 66 | + .setOutputPerIteration(true); |
| 67 | + |
| 68 | + benchmark.addCase( |
| 69 | + "deserialize(DataInput)", |
| 70 | + 10, |
| 71 | + () -> { |
| 72 | + try (LocalFileIO.LocalSeekableInputStream seekableStream = |
| 73 | + new LocalFileIO.LocalSeekableInputStream(file); |
| 74 | + DataInputStream input = new DataInputStream(seekableStream)) { |
| 75 | + new RoaringBitmap().deserialize(input); |
| 76 | + } catch (IOException e) { |
| 77 | + throw new RuntimeException(e); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + benchmark.addCase( |
| 82 | + "deserialize(DataInput, byte[])", |
| 83 | + 10, |
| 84 | + () -> { |
| 85 | + try (LocalFileIO.LocalSeekableInputStream seekableStream = |
| 86 | + new LocalFileIO.LocalSeekableInputStream(file); |
| 87 | + DataInputStream input = new DataInputStream(seekableStream)) { |
| 88 | + new RoaringBitmap().deserialize(input, null); |
| 89 | + } catch (IOException e) { |
| 90 | + throw new RuntimeException(e); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + benchmark.addCase( |
| 95 | + "deserialize(ByteBuffer)", |
| 96 | + 10, |
| 97 | + () -> { |
| 98 | + try (LocalFileIO.LocalSeekableInputStream seekableStream = |
| 99 | + new LocalFileIO.LocalSeekableInputStream(file); |
| 100 | + DataInputStream input = new DataInputStream(seekableStream)) { |
| 101 | + byte[] bytes = new byte[(int) file.length()]; |
| 102 | + input.readFully(bytes); |
| 103 | + new RoaringBitmap().deserialize(ByteBuffer.wrap(bytes)); |
| 104 | + } catch (IOException e) { |
| 105 | + throw new RuntimeException(e); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + benchmark.run(); |
| 110 | + } |
| 111 | +} |
0 commit comments