forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the performance of TextSource by reducing how many byte[]s ar…
…e copied (fixes apache#23193) This makes TextSource take about 2.3x less CPU resources during decoding. Before this change: ``` TextSourceBenchmark.benchmarkTextSource thrpt 5 0.248 ± 0.029 ops/s ``` After this change: ``` TextSourceBenchmark.benchmarkHadoopLineReader thrpt 5 0.465 ± 0.064 ops/s TextSourceBenchmark.benchmarkTextSource thrpt 5 0.575 ± 0.059 ops/s ```
- Loading branch information
Showing
6 changed files
with
430 additions
and
133 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
116 changes: 116 additions & 0 deletions
116
sdks/java/core/jmh/src/main/java/org/apache/beam/sdk/jmh/io/TextSourceBenchmark.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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.beam.sdk.jmh.io; | ||
|
||
import java.io.FileInputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import org.apache.beam.sdk.io.FileBasedSource; | ||
import org.apache.beam.sdk.io.Source; | ||
import org.apache.beam.sdk.io.TextIOReadTest; | ||
import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
import org.apache.hadoop.io.Text; | ||
import org.apache.hadoop.util.LineReader; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
|
||
public class TextSourceBenchmark { | ||
private static final int NUM_LINES = 10_000_000; | ||
private static char[] data = new char[120]; | ||
|
||
static { | ||
Arrays.fill(data, 'a'); | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
public static class Data { | ||
public Path path; | ||
public String pathString; | ||
public int length; | ||
|
||
/** Generates a random file with {@code NUM_LINES} between 60 and 120 characters each. */ | ||
@Setup | ||
public void createFile() throws Exception { | ||
path = Files.createTempFile("benchmark", null).toAbsolutePath(); | ||
StringBuilder value = new StringBuilder(120 * NUM_LINES); | ||
for (int i = 0; i < NUM_LINES; ++i) { | ||
String valueToAppend = | ||
String.valueOf(data, 0, ThreadLocalRandom.current().nextInt(60, 120)); | ||
length += valueToAppend.length(); | ||
value.append(valueToAppend); | ||
value.append('\n'); | ||
} | ||
Files.write(path, value.toString().getBytes(StandardCharsets.UTF_8)); | ||
pathString = path.toString(); | ||
} | ||
|
||
@TearDown | ||
public void deleteFile() throws Exception { | ||
Files.deleteIfExists(path); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void benchmarkTextSource(Data data) throws Exception { | ||
Source.Reader<String> reader = | ||
((FileBasedSource<String>) TextIOReadTest.getTextSource(data.pathString, null)) | ||
.createReader(PipelineOptionsFactory.create()); | ||
int length = 0; | ||
int linesRead = 0; | ||
if (reader.start()) { | ||
linesRead += 1; | ||
length += reader.getCurrent().length(); | ||
} | ||
while (reader.advance()) { | ||
linesRead += 1; | ||
length += reader.getCurrent().length(); | ||
} | ||
if (linesRead != NUM_LINES) { | ||
throw new IllegalStateException(); | ||
} | ||
if (length != data.length) { | ||
throw new IllegalStateException(); | ||
} | ||
reader.close(); | ||
} | ||
|
||
@Benchmark | ||
public void benchmarkHadoopLineReader(Data data) throws Exception { | ||
LineReader reader = new LineReader(new FileInputStream(data.pathString)); | ||
int length = 0; | ||
int linesRead = 0; | ||
do { | ||
Text text = new Text(); | ||
reader.readLine(text); | ||
// It is important to convert toString() here so that we force the decoding to UTF8 otherwise | ||
// Text keeps the encoded byte[] version in memory. | ||
length += text.toString().length(); | ||
linesRead += 1; | ||
} while (length < data.length); | ||
if (linesRead != NUM_LINES) { | ||
throw new IllegalStateException(); | ||
} | ||
reader.close(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
sdks/java/core/jmh/src/main/java/org/apache/beam/sdk/jmh/io/package-info.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** Benchmarks for IO. */ | ||
package org.apache.beam.sdk.jmh.io; |
Oops, something went wrong.