jakubzytka commented on code in PR #2464:
URL: https://github.com/apache/cassandra/pull/2464#discussion_r1262623166


##########
test/microbench/org/apache/cassandra/test/microbench/DeletionTimeDeSerBench.java:
##########
@@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.test.microbench;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.NoSuchFileException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.cassandra.db.DeletionTime;
+import org.apache.cassandra.db.DeletionTime.Serializer;
+import org.apache.cassandra.io.util.DataInputBuffer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputBuffer;
+import org.apache.cassandra.io.util.DataOutputStreamPlus;
+import org.apache.cassandra.io.util.FileInputStreamPlus;
+import org.apache.cassandra.io.util.FileOutputStreamPlus;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
+@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
+@Fork(value = 3, jvmArgsAppend = "-Xmx512M")
+@Threads(1)
+@State(Scope.Benchmark)
+public class DeletionTimeDeSerBench
+{
+    final static int BUFFER_SIZE = 1024 * 1024;
+    final static int DT_STD_SIZE = 8 + 4; // Long + Int
+
+    final static Random random = new Random(100);
+    final static List<DeletionTime> TEST_DTS_70PC_LIVE = generateDTs(7);
+    final static List<DeletionTime> TEST_DTS_30PC_LIVE = generateDTs(3);
+
+    // Parameters
+    final static String ncSstableParam = "NC";
+    final static String oaSstableParam = "OA";
+    @Param({ ncSstableParam, oaSstableParam })
+    String sstableParam;
+
+    final static String live70PcParam = "70PcLive";
+    final static String live30PcParam = "30PcLive";
+    @Param({ live70PcParam, live30PcParam })
+    String liveDTPcParam;
+
+    final static String RAMParam = "RAM";
+    final static String diskParam = "Disk";
+    @Param({ RAMParam,  diskParam })
+    String diskRAMParam;
+
+    // Files
+    File serMMapedFile = new File(DeletionTimeDeSerBench.class + "_Sermmap");
+    ByteBuffer serMMap = allocateMmapedByteBuffer(serMMapedFile, BUFFER_SIZE);
+    File deserMMapedFile = new File(DeletionTimeDeSerBench.class + 
"_Desermmap");
+    ByteBuffer deserMMap = allocateMmapedByteBuffer(deserMMapedFile, 
BUFFER_SIZE);
+    File diskFile = new File(DeletionTimeDeSerBench.class + "_DTBenchTest");
+
+    @TearDown
+    public void tearDown()
+    {
+        serMMapedFile.delete();
+        deserMMapedFile.delete();
+        diskFile.delete();
+    }
+
+    @Benchmark
+    public void testE2ESerializeDT(final Blackhole bh) throws IOException
+    {
+        ByteBuffer buffer = serMMap;
+        Serializer serializer = getSerializer(sstableParam);
+        List<DeletionTime> dts = getDTs(liveDTPcParam);
+        
+        try(DataOutputStreamPlus out = getSerOut(diskRAMParam, serMMap))
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+                serializer.serialize(dts.get(i), out);
+            bh.consume(out);
+        }
+
+        bh.consume(buffer);
+        bh.consume(serializer);
+        bh.consume(dts);
+        buffer.clear();
+    }
+
+    @Benchmark
+    public void testE2EDeSerializeDT(final Blackhole bh) throws IOException
+    {
+        ByteBuffer buffer = deserMMap;
+        Serializer serializer = getSerializer(sstableParam);
+        List<DeletionTime> dts = getDTs(liveDTPcParam);
+        
+        try(DataOutputStreamPlus out = getSerOut(diskRAMParam, deserMMap))
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+                serializer.serialize(dts.get(i), out);
+            bh.consume(out);
+        }
+
+        DataInputPlus in = getDeSerIn(diskRAMParam, deserMMap);
+        for (int i = 0, m = dts.size(); i < m; i++)
+            serializer.deserialize(in);
+
+        bh.consume(buffer);
+        bh.consume(serializer);
+        bh.consume(dts);
+        bh.consume(in);
+        buffer.clear();
+    }
+
+    /**
+     * Test the impact of the different bit ops and write calls. Against 
memory only.
+     */
+    @Benchmark
+    public void testRawAlgWrites(final Blackhole bh)
+    {
+        if (diskRAMParam != RAMParam)
+            return;
+
+        List<DeletionTime> dts = getDTs(liveDTPcParam);
+        ByteBuffer buffer = ByteBuffer.allocate(DT_STD_SIZE);
+
+        if (sstableParam == oaSstableParam)
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+            {
+                buffer.clear();
+                if (dts.get(i).equals(DeletionTime.LIVE))
+                    buffer.put((byte) ('L' & 1));
+                else
+                {
+                    buffer.putLong(Math.abs(random.nextLong()));
+                    buffer.putInt(Math.abs(random.nextInt()));
+                }
+            }
+        }
+        else
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+            {
+                buffer.clear();
+                buffer.putLong(Math.abs(random.nextLong()));
+                buffer.putInt(Math.abs(random.nextInt()));
+            }
+        }
+
+        bh.consume(dts);
+        bh.consume(buffer);
+    }
+
+    /**
+     * Test the impact of the different bit ops and read calls. Against memory 
only.
+     */
+    @Benchmark
+    public void testRawAlgReads(final Blackhole bh)
+    {
+        if (diskRAMParam != RAMParam)
+            return;
+
+        List<DeletionTime> dts = getDTs(liveDTPcParam);
+        ByteBuffer buffer = ByteBuffer.allocate(DT_STD_SIZE);
+        buffer.putLong(Math.abs(random.nextLong()));
+        buffer.putInt(Math.abs(random.nextInt()));
+
+        if (sstableParam == oaSstableParam)
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+            {
+                buffer.clear();
+                if (dts.get(i).equals(DeletionTime.LIVE))
+                {
+                    byte b = buffer.get();
+                    b = (byte) (b & 1);
+                    bh.consume(b);
+                }
+                else
+                {
+                    // The flag
+                    byte flag = buffer.get();
+
+                    // 7 bytes mfda
+                    int bytes4 = buffer.getInt();
+                    int bytes2 = buffer.getShort();
+                    int bytes1 = buffer.get();
+
+                    long mfda = bytes4 & 0xFFFFFFFFL;
+                    mfda = (mfda << 16) + (bytes2 & 0xffff);
+                    mfda = (mfda << 8) + (bytes1 & 0xff);
+
+                    // The ldt
+                    int ldt = buffer.getInt();
+
+                    bh.consume(flag);
+                    bh.consume(bytes4);
+                    bh.consume(bytes2);
+                    bh.consume(bytes1);
+                    bh.consume(mfda);
+                    bh.consume(ldt);
+                }
+            }
+        }
+        else
+        {
+            for (int i = 0, m = dts.size(); i < m; i++)
+            {
+                buffer.clear();
+                long mfda = buffer.getLong();
+                int ldt = buffer.getInt();
+
+                bh.consume(mfda);
+                bh.consume(ldt);
+            }
+        }
+
+        bh.consume(dts);
+        bh.consume(buffer);
+    }
+
+    private DataInputPlus getDeSerIn(String value, ByteBuffer maybeBackingBB)
+    {
+        switch (value)
+        {
+            case RAMParam:
+                maybeBackingBB.rewind();
+                return new DataInputBuffer(maybeBackingBB, false);
+
+            case diskParam:
+                try
+                {
+                    return new FileInputStreamPlus(new 
org.apache.cassandra.io.util.File(diskFile));
+                }
+                catch(NoSuchFileException e)
+                {
+                    throw new RuntimeException(e);
+                }
+
+            default:
+                throw new IllegalStateException("Unknown backing media for 
deser.");
+        }
+    }
+
+    private DataOutputStreamPlus getSerOut(String value, ByteBuffer 
maybeBackingBB)
+    {
+        switch (value)
+        {
+            case RAMParam:
+                maybeBackingBB.rewind();
+                return new DataOutputBuffer(maybeBackingBB);
+
+            case diskParam:
+                try
+                {
+                    return new FileOutputStreamPlus(new 
org.apache.cassandra.io.util.File(diskFile));
+                }
+                catch(NoSuchFileException e)
+                {
+                    throw new RuntimeException(e);
+                }
+
+            default:
+                throw new IllegalStateException("Unknown backing media for 
ser.");
+        }
+    }
+
+    private static List<DeletionTime> generateDTs(int livePC)

Review Comment:
   could we make it `livePercentage` and use it like percentage (i.e. `70` 
instead of `7` etc)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to