-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Optimize BSON codec lookup. #1632
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
Changes from 7 commits
5baaab2
17d4b72
51d413d
069f7e8
84597f2
82cbaf2
4136725
56acb80
dfce11b
a03d11e
eb8c355
ae19079
b65f6ad
4ff5da4
2f4a370
626531b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,16 @@ dependencies { | |
api project(':driver-sync') | ||
api project(':mongodb-crypt') | ||
implementation "ch.qos.logback:logback-classic:$logbackVersion" | ||
|
||
implementation 'org.openjdk.jmh:jmh-core:1.37' | ||
annotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.37' | ||
} | ||
|
||
tasks.register("jmh", JavaExec) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this get run via evergreen in the same way the other benchmarks do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, no. I initially considered adding it to Evergreen, but it would expand this PR and would require some time to test it. This task is mainly for local benchmarking to quickly assess the relative performance impact of small components. However, I think we should consider adding it to Evergreen in the future, as it could provide more insight into performance changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case in the future it may be worth splitting jmh benchmarks into their own project. For example reorganize both into a benchmarks folder but have separate configuration for each:
Nothing to do now. |
||
group = 'benchmark' | ||
description = 'Run JMH benchmarks.' | ||
mainClass = 'org.openjdk.jmh.Main' | ||
classpath = sourceSets.main.runtimeClasspath | ||
} | ||
|
||
javadoc { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2016-present MongoDB, Inc. | ||
* | ||
* Licensed 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 com.mongodb.benchmark.jmh.codec; | ||
|
||
import com.mongodb.internal.connection.ByteBufferBsonOutput; | ||
import com.mongodb.internal.connection.PowerOfTwoBufferPool; | ||
import org.bson.BsonArray; | ||
import org.bson.BsonBinaryReader; | ||
import org.bson.BsonBinaryWriter; | ||
import org.bson.BsonDocument; | ||
import org.bson.BsonDouble; | ||
import org.bson.codecs.Codec; | ||
import org.bson.codecs.DecoderContext; | ||
import org.bson.codecs.EncoderContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry; | ||
vbabanin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import static com.mongodb.benchmark.jmh.codec.BsonUtils.getDocumentAsBuffer; | ||
|
||
@BenchmarkMode(Mode.Throughput) | ||
@Warmup(iterations = 20, time = 2, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 20, time = 2, timeUnit = TimeUnit.SECONDS) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
@Fork(3) | ||
public class BsonArrayCodecBenchmark { | ||
|
||
@State(Scope.Benchmark) | ||
public static class Input { | ||
protected final PowerOfTwoBufferPool bufferPool = PowerOfTwoBufferPool.DEFAULT; | ||
protected final Codec<BsonArray> bsonArrayCodec = getDefaultCodecRegistry().get(BsonArray.class); | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
protected BsonDocument document; | ||
protected byte[] documentBytes; | ||
private BsonBinaryReader reader; | ||
private BsonBinaryWriter writer; | ||
private BsonArray bsonValues; | ||
|
||
@Setup | ||
public void setup() throws IOException { | ||
bsonValues = new BsonArray(); | ||
document = new BsonDocument("array", bsonValues); | ||
|
||
for (int i = 0; i < 1000; i++) { | ||
bsonValues.add(new BsonDouble(i)); | ||
} | ||
|
||
documentBytes = getDocumentAsBuffer(document); | ||
} | ||
|
||
@Setup(Level.Invocation) | ||
public void beforeIteration() { | ||
reader = new BsonBinaryReader(ByteBuffer.wrap(documentBytes)); | ||
writer = new BsonBinaryWriter(new ByteBufferBsonOutput(bufferPool)); | ||
|
||
reader.readStartDocument(); | ||
writer.writeStartDocument(); | ||
writer.writeName("array"); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void decode(@NotNull Input input, @NotNull Blackhole blackhole) { | ||
blackhole.consume(input.bsonArrayCodec.decode(input.reader, DecoderContext.builder().build())); | ||
} | ||
|
||
@Benchmark | ||
public void encode(@NotNull Input input, @NotNull Blackhole blackhole) { | ||
input.bsonArrayCodec.encode(input.writer, input.bsonValues, EncoderContext.builder().build()); | ||
blackhole.consume(input); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.