Last active
November 30, 2021 16:33
-
-
Save matriv/442719776e2b076ad54b2475b178c43a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.flink.benchmark; | |
import org.apache.flink.table.data.binary.BinaryStringData; | |
import org.apache.flink.table.data.binary.BinaryStringDataUtil; | |
import org.apache.flink.table.runtime.util.SegmentsUtil; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import org.openjdk.jmh.runner.options.VerboseMode; | |
public class BinaryStringDataBenchmark extends BenchmarkBase { | |
public static void main(String[] args) throws RunnerException { | |
Options options = | |
new OptionsBuilder() | |
.verbosity(VerboseMode.NORMAL) | |
.include(".*" + BinaryStringDataBenchmark.class.getCanonicalName() + ".*") | |
.build(); | |
new Runner(options).run(); | |
} | |
private BinaryStringData str; | |
private static int sizeToPad = 1000; | |
@Setup | |
public void setup() { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < sizeToPad; i++) { | |
sb.append(i); | |
} | |
str = BinaryStringData.fromString(sb.toString()); | |
} | |
@Benchmark | |
public void useString(Blackhole blackhole) throws Exception { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < sizeToPad; i++) { | |
sb.append(' '); | |
} | |
str.ensureMaterialized(); | |
blackhole.consume(BinaryStringData.fromString(str.getJavaObject() + sb.toString())); | |
} | |
@Benchmark | |
public void useBytes(Blackhole blackhole) throws Exception { | |
int srcSizeInBytes = str.getSizeInBytes(); | |
final byte[] newString = new byte[srcSizeInBytes + sizeToPad]; | |
for (int j = srcSizeInBytes; j < newString.length; j++) { | |
newString[j] = (byte) 32; // space | |
} | |
System.arraycopy(str.toBytes(), 0, newString, 0, srcSizeInBytes); | |
blackhole.consume(BinaryStringData.fromBytes(newString)); | |
} | |
@Benchmark | |
public void useBytesWithSegmentUtil(Blackhole blackhole) throws Exception { | |
int srcSizeInBytes = str.getSizeInBytes(); | |
final byte[] newString = new byte[srcSizeInBytes + sizeToPad]; | |
for (int j = srcSizeInBytes; j < newString.length; j++) { | |
newString[j] = (byte) 32; // space | |
} | |
SegmentsUtil.copyToBytes(str.getSegments(), 0, newString, 0, srcSizeInBytes); | |
blackhole.consume(BinaryStringData.fromBytes(newString)); | |
} | |
@Benchmark | |
public void useBinaryStringDataUtils(Blackhole blackhole) throws Exception { | |
blackhole.consume(BinaryStringDataUtil.concat(str, BinaryStringData.blankString(sizeToPad))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment