Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] add row format benchmark #411

Merged
merged 1 commit into from
May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions java/fury-benchmark/src/main/java/io/fury/benchmark/RowSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright 2023 The Fury authors
* 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 io.fury.benchmark;

import io.fury.format.encoder.Encoder;
import io.fury.format.encoder.Encoders;
import io.fury.util.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.reflect.ReflectDatumReader;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.Benchmark;
import org.slf4j.Logger;

public class RowSuite {
private static final Logger LOG = LoggerFactory.getLogger(RowSuite.class);

public static final class TestStruct implements Serializable {
private short f1;
private int f2;
private long f3;
private float f4;
private double f5;
private double f6;
private long f7;
private short f11;
private int f12;
private long f13;
private float f14;
private double f15;
private double f16;
private long f17;
private List<Integer> intList;
}

public static TestStruct createBeanB(int arrSize) {
Random rnd = new Random(37);
TestStruct testStruct = new TestStruct();
testStruct.f1 = (short) rnd.nextInt();
testStruct.f2 = rnd.nextInt();
testStruct.f3 = rnd.nextLong();
testStruct.f4 = rnd.nextFloat();
testStruct.f5 = rnd.nextDouble();
testStruct.f6 = rnd.nextDouble();
testStruct.f7 = rnd.nextLong();
testStruct.f11 = (short) rnd.nextInt();
testStruct.f12 = rnd.nextInt();
testStruct.f13 = rnd.nextLong();
testStruct.f14 = rnd.nextFloat();
testStruct.f15 = rnd.nextDouble();
testStruct.f16 = rnd.nextDouble();
testStruct.f17 = rnd.nextLong();
List<Integer> integers = new ArrayList<>();
for (int i = 0; i < arrSize; i++) {
integers.add(rnd.nextInt());
}
testStruct.intList = integers;
return testStruct;
}

private static TestStruct object = createBeanB(10);

private static DatumWriter<TestStruct> avroWriter = new ReflectDatumWriter<>(TestStruct.class);
private static ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private static BinaryEncoder avroEncoder;
private static final byte[] avroData;
private static BinaryDecoder avroDecoder =
DecoderFactory.get().binaryDecoder(outputStream.toByteArray(), null);
private static DatumReader<TestStruct> avroReader = new ReflectDatumReader<>(TestStruct.class);

private static Encoder<TestStruct> furyEncoder = Encoders.bean(TestStruct.class);
private static final byte[] furyData;

static {
// create a file of packets
DatumWriter<TestStruct> writer = new ReflectDatumWriter<>(TestStruct.class);
avroEncoder = EncoderFactory.get().binaryEncoder(outputStream, null);
outputStream.reset();
try {
writer.write(object, avroEncoder);
avroEncoder.flush();
avroData = outputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
furyData = furyEncoder.encode(object);
}

@Benchmark
public Object furySerialize() throws IOException {
return furyEncoder.encode(object);
}

@Benchmark
public Object furyDeserialize() throws IOException {
return furyEncoder.decode(furyData);
}

@Benchmark
public Object avroSerialize() throws IOException {
outputStream.reset();
avroWriter.write(object, avroEncoder);
avroEncoder.flush();
return outputStream.toByteArray();
}

@Benchmark
public Object avroDeserialize() throws IOException {
avroDecoder = DecoderFactory.get().binaryDecoder(avroData, avroDecoder);
return avroReader.read(null, avroDecoder);
}

public static void main(String[] args) throws Exception {
if (args.length == 0) {
String commandLine = "io.*RowSuite.* -f 3 -wi 3 -i 3 -t 1 -w 2s -r 2s -rf csv";
args = commandLine.split(" ");
}
LOG.info("command line: {}", Arrays.toString(args));
Main.main(args);
}
}