/*
 * 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.paimon.benchmark.arrow;

import org.apache.paimon.arrow.vector.ArrowFormatWriter;
import org.apache.paimon.benchmark.Benchmark;
import org.apache.paimon.data.BinaryString;
import org.apache.paimon.data.GenericRow;
import org.apache.paimon.types.DataTypes;
import org.apache.paimon.types.RowType;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ThreadLocalRandom;

/** Benchmark for arrow write. */
public class ArrowWriteBenchmark {

    @Test
    public void testWrite() {
        int batch = 1024 * 20;
        Benchmark benchmark =
                new Benchmark("write", batch * batch)
                        .setNumWarmupIters(1)
                        .setOutputPerIteration(true);
        RowType rowType =
                RowType.of(
                        DataTypes.STRING(),
                        DataTypes.STRING(),
                        DataTypes.STRING(),
                        DataTypes.STRING());
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        byte[] bytes = new byte[100];
        rnd.nextBytes(bytes);
        BinaryString binaryString = BinaryString.fromBytes(bytes, 1, 99);
        GenericRow row = GenericRow.of(binaryString, binaryString, binaryString, binaryString);
        benchmark.addCase(
                "write",
                1,
                () -> {
                    try (ArrowFormatWriter writer = new ArrowFormatWriter(rowType, batch, true)) {
                        for (int i = 0; i < batch; i++) {
                            writer.reset();
                            for (int j = 0; j < batch; j++) {
                                writer.write(row);
                            }
                        }
                    }
                });
        benchmark.run();
    }
}
