forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoCryptBenchmarkRunner.java
More file actions
224 lines (203 loc) · 10.5 KB
/
MongoCryptBenchmarkRunner.java
File metadata and controls
224 lines (203 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.mongodb.benchmark.framework;
/*
* Copyright 2023-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.
*
*/
import com.mongodb.internal.crypt.capi.CAPI;
import com.mongodb.internal.crypt.capi.MongoCrypt;
import com.mongodb.internal.crypt.capi.MongoCryptContext;
import com.mongodb.internal.crypt.capi.MongoCryptOptions;
import com.mongodb.internal.crypt.capi.MongoCrypts;
import com.mongodb.internal.crypt.capi.MongoExplicitEncryptOptions;
import com.mongodb.internal.crypt.capi.MongoLocalKmsProviderOptions;
import org.bson.BsonBinary;
import org.bson.BsonBinarySubType;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.RawBsonDocument;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MongoCryptBenchmarkRunner {
static final int NUM_FIELDS = 1500;
static final int NUM_WARMUP_SECS = 2;
static final int NUM_SECS = 10;
static final byte[] LOCAL_MASTER_KEY = new byte[]{
-99, -108, 75, 13, -109, -48, -59, 68, -91, 114, -3, 50, 27, -108, 48, -112, 35, 53,
115, 124, -16, -10, -62, -12, -38, 35, 86, -25, -113, 4, -52, -6, -34, 117, -76, 81,
-121, -13, -117, -105, -41, 75, 68, 59, -84, 57, -94, -58, 77, -111, 0, 62, -47, -6, 74,
48, -63, -46, -58, 94, -5, -84, 65, -14, 72, 19, 60, -101, 80, -4, -89, 36, 122, 46, 2,
99, -93, -58, 22, 37, 81, 80, 120, 62, 15, -40, 110, -124, -90, -20, -115, 45, 36, 71,
-27, -81
};
private static String getFileAsString(final String fileName) {
try {
URL resource = BenchmarkRunner.class.getResource("/" + fileName);
if (resource == null) {
throw new RuntimeException("Could not find file " + fileName);
}
return new String(Files.readAllBytes(Paths.get(resource.toURI())));
} catch (Throwable t) {
throw new RuntimeException("Could not parse file " + fileName, t);
}
}
private static BsonDocument getResourceAsDocument(final String fileName) {
return BsonDocument.parse(getFileAsString(fileName));
}
private static MongoCrypt createMongoCrypt() {
return MongoCrypts.create(MongoCryptOptions
.builder()
.localKmsProviderOptions(MongoLocalKmsProviderOptions.builder()
.localMasterKey(ByteBuffer.wrap(LOCAL_MASTER_KEY))
.build())
.build());
}
// DecryptTask decrypts a document repeatedly for a specified number of seconds and records ops/sec.
private static class DecryptTask implements Runnable {
public DecryptTask(MongoCrypt mongoCrypt, BsonDocument toDecrypt, int numSecs, CountDownLatch doneSignal) {
this.mongoCrypt = mongoCrypt;
this.toDecrypt = toDecrypt;
this.opsPerSecs = new ArrayList<Long>(numSecs);
this.numSecs = numSecs;
this.doneSignal = doneSignal;
}
public void run() {
for (int i = 0; i < numSecs; i++) {
long opsPerSec = 0;
long start = System.nanoTime();
// Run for one second.
while (System.nanoTime() - start < 1_000_000_000) {
try (MongoCryptContext ctx = mongoCrypt.createDecryptionContext(toDecrypt)) {
assert ctx.getState() == MongoCryptContext.State.READY;
ctx.finish();
opsPerSec++;
}
}
opsPerSecs.add(opsPerSec);
}
doneSignal.countDown();
}
public long getMedianOpsPerSecs() {
if (opsPerSecs.size() == 0) {
throw new IllegalStateException("opsPerSecs is empty. Was `run` called?");
}
Collections.sort(opsPerSecs);
return opsPerSecs.get(numSecs / 2);
}
private MongoCrypt mongoCrypt;
private BsonDocument toDecrypt;
private ArrayList<Long> opsPerSecs;
private int numSecs;
private CountDownLatch doneSignal;
}
public List<MongocryptBecnhmarkResult> run() throws InterruptedException {
System.out.printf("BenchmarkRunner is using libmongocrypt version=%s, NUM_WARMUP_SECS=%d, NUM_SECS=%d%n",
CAPI.mongocrypt_version(null).toString(), NUM_WARMUP_SECS, NUM_SECS);
// `keyDocument` is a Data Encryption Key (DEK) encrypted with the Key Encryption Key (KEK) `LOCAL_MASTER_KEY`.
BsonDocument keyDocument = getResourceAsDocument("keyDocument.json");
try (MongoCrypt mongoCrypt = createMongoCrypt()) {
// `encrypted` will contain encrypted fields.
BsonDocument encrypted = new BsonDocument();
{
for (int i = 0; i < NUM_FIELDS; i++) {
MongoExplicitEncryptOptions options = MongoExplicitEncryptOptions.builder()
.keyId(new BsonBinary(BsonBinarySubType.UUID_STANDARD, Base64.getDecoder().decode("YWFhYWFhYWFhYWFhYWFhYQ==")))
.algorithm("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
.build();
BsonDocument toEncrypt = new BsonDocument("v", new BsonString(String.format("value %04d", i)));
try (MongoCryptContext ctx = mongoCrypt.createExplicitEncryptionContext(toEncrypt, options)) {
// If mongocrypt_t has not yet cached the DEK, supply it.
if (MongoCryptContext.State.NEED_MONGO_KEYS == ctx.getState()) {
ctx.addMongoOperationResult(keyDocument);
ctx.completeMongoOperation();
}
assert ctx.getState() == MongoCryptContext.State.READY;
RawBsonDocument result = ctx.finish();
BsonValue encryptedValue = result.get("v");
String key = String.format("key%04d", i);
encrypted.append(key, encryptedValue);
}
}
}
// Warm up benchmark and discard the result.
DecryptTask warmup = new DecryptTask(mongoCrypt, encrypted, NUM_WARMUP_SECS, new CountDownLatch(1));
warmup.run();
// Decrypt `encrypted` and measure ops/sec.
// Check with varying thread counts to measure impact of a shared pool of Cipher instances.
int[] threadCounts = {1, 2, 8, 64};
ArrayList<Long> totalMedianOpsPerSecs = new ArrayList<Long>(threadCounts.length);
ArrayList<String> createdAts = new ArrayList<String>(threadCounts.length);
ArrayList<String> completedAts = new ArrayList<String>(threadCounts.length);
for (int threadCount : threadCounts) {
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch doneSignal = new CountDownLatch(threadCount);
ArrayList<DecryptTask> decryptTasks = new ArrayList<DecryptTask>(threadCount);
createdAts.add(ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
for (int i = 0; i < threadCount; i++) {
DecryptTask decryptTask = new DecryptTask(mongoCrypt, encrypted, NUM_SECS, doneSignal);
decryptTasks.add(decryptTask);
executorService.execute(decryptTask);
}
// Await completion of all tasks. Tasks are expected to complete shortly after NUM_SECS. Time out `await` if time exceeds 2 * NUM_SECS.
boolean ok = doneSignal.await(NUM_SECS * 2, TimeUnit.SECONDS);
assert ok;
completedAts.add(ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
// Sum the median ops/secs of all tasks to get total throughput.
long totalMedianOpsPerSec = 0;
for (DecryptTask decryptTask : decryptTasks) {
totalMedianOpsPerSec += decryptTask.getMedianOpsPerSecs();
}
System.out.printf("threadCount=%d. Decrypting 1500 fields median ops/sec : %d%n", threadCount, totalMedianOpsPerSec);
totalMedianOpsPerSecs.add(totalMedianOpsPerSec);
executorService.shutdown();
ok = executorService.awaitTermination(NUM_SECS * 2, TimeUnit.SECONDS);
assert ok;
}
// Print the results in JSON that can be accepted by the `perf.send` command.
// See https://docs.devprod.prod.corp.mongodb.com/evergreen/Project-Configuration/Project-Commands#perfsend for the expected `perf.send` input.
List<MongocryptBecnhmarkResult> results = new ArrayList<>(threadCounts.length);
for (int i = 0; i < threadCounts.length; i++) {
int threadCount = threadCounts[i];
long totalMedianOpsPerSec = totalMedianOpsPerSecs.get(i);
String createdAt = createdAts.get(i);
String completedAt = completedAts.get(i);
MongocryptBecnhmarkResult result = new MongocryptBecnhmarkResult(
"java_decrypt_1500",
threadCount,
totalMedianOpsPerSec,
createdAt,
completedAt,
"medianOpsPerSec",
"THROUGHPUT");
results.add(result);
}
System.out.println("Results: " + results);
return results;
}
}
}