Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
Standalone works, SaaS shield is failing to reach the local TSP
for me
  • Loading branch information
skeet70 committed Aug 14, 2024
1 parent de23bd7 commit beb2fa9
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 241 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ uniffi = { git = "https://github.com/mozilla/uniffi-rs", branch = "main", featur
"tokio",
"cli",
] }
uniffi-bindgen-java = { git = "https://github.com/IronCoreLabs/uniffi-bindgen-java", branch = "polish-before-alpha" }
uniffi-bindgen-java = { git = "https://github.com/IronCoreLabs/uniffi-bindgen-java", branch = "main" }
z85 = "3.0.5"

[dev-dependencies]
Expand Down
128 changes: 0 additions & 128 deletions java/benchmarks/src/SaasShieldBenchmark.kt

This file was deleted.

88 changes: 0 additions & 88 deletions java/benchmarks/src/StandaloneBenchmark.kt

This file was deleted.

23 changes: 0 additions & 23 deletions java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,11 @@ plugins {
id("me.champeau.jmh") version "0.7.2"
}

val benchmarks = "benchmarks"
sourceSets {
create(benchmarks)
}

sourceSets {
all {
java.setSrcDirs(listOf("$name/src"))
resources.setSrcDirs(listOf("$name/../src/main/resources"))
}

main {
java.setSrcDirs(listOf("src/main/java"))
}

test {
java.setSrcDirs(listOf("src/test/java"))
}
}

val benchmarksImplementation by configurations

dependencies {
// Use the JUnit 5 integration.
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("net.java.dev.jna:jna:5.14.0")
benchmarksImplementation(sourceSets.main.get().output + sourceSets.main.get().runtimeClasspath)
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ The benchmark is using a single tenant, and (depending on your machine and bench

## Other Languages

There are also benchmarks available in [Rust](https://github.com/IronCoreLabs/ironcore-alloy/tree/main/benches) and [Python](https://github.com/IronCoreLabs/ironcore-alloy/blob/main/python/ironcore-alloy/bench.py).
There are also benchmarks available in [Rust](https://github.com/IronCoreLabs/ironcore-alloy/tree/main/benches), [Kotlin](https://github.com/IronCoreLabs/ironcore-alloy/tree/main/kotlin/benchmarks) and [Python](https://github.com/IronCoreLabs/ironcore-alloy/blob/main/python/ironcore-alloy/bench.py).
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package test;

import com.ironcorelabs.ironcore_alloy_java.*;
import org.openjdk.jmh.annotations.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@Fork(1)
@Warmup(iterations = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
public class SaasShieldBenchmark {

private PlaintextDocument smallPlaintext;
private PlaintextDocument mediumPlaintext;
private PlaintextDocument largePlaintext;
private PlaintextDocument extraLargePlaintext;
private EncryptedDocument smallEncrypted;
private EncryptedDocument mediumEncrypted;
private EncryptedDocument largeEncrypted;
private EncryptedDocument extraLargeEncrypted;
private PlaintextDocuments batchPlaintexts;

private final float approximationFactor = 1.1f;
private final String tspUri = System.getenv().getOrDefault("TSP_ADDRESS", "http://localhost");
private final String tspPort = System.getenv().getOrDefault("TSP_PORT", "32804");
private final TenantId tenantId = new TenantId(System.getenv().getOrDefault("TENANT_ID", "tenant-gcp-l"));
private final String apiKey = System.getenv().getOrDefault("API_KEY", "0WUaXesNgbTAuLwn");

private SaasShieldConfiguration saasShieldConfig;
private SaasShield saasShieldSdk;
private final AlloyMetadata metadata = AlloyMetadata.newSimple(tenantId);

@Setup
public void setUp() throws Exception {
saasShieldConfig = new SaasShieldConfiguration(tspUri + ":" + tspPort, apiKey, true, approximationFactor);
saasShieldSdk = new SaasShield(saasShieldConfig);
smallPlaintext = generatePlaintextDocument(1, 1);
mediumPlaintext = generatePlaintextDocument(100, 1);
largePlaintext = generatePlaintextDocument(10_000, 1);
extraLargePlaintext = generatePlaintextDocument(1_000_000, 1);

batchPlaintexts = new PlaintextDocuments(new HashMap<>());
int numDocuments = 10;
int numFields = 10;
int fieldSize = 10;

for (int i = 1; i <= numDocuments; i++) {
batchPlaintexts.value().put(new DocumentId("doc" + i), generatePlaintextDocument(fieldSize, numFields));
}

smallEncrypted = saasShieldSdk.standard().encrypt(smallPlaintext, metadata).get();
mediumEncrypted = saasShieldSdk.standard().encrypt(mediumPlaintext, metadata).get();
largeEncrypted = saasShieldSdk.standard().encrypt(largePlaintext, metadata).get();
extraLargeEncrypted = saasShieldSdk.standard().encrypt(extraLargePlaintext, metadata).get();
}

@TearDown
public void tearDown() {
saasShieldSdk.close();
}

private PlaintextDocument generatePlaintextDocument(int bytesPerField, int numFields) {
PlaintextDocument documentMap = new PlaintextDocument(new HashMap<>());
for (int i = 1; i <= numFields; i++) {
PlaintextBytes byteArray = new PlaintextBytes(new byte[bytesPerField]);
new Random().nextBytes(byteArray.value());
documentMap.value().put(new FieldId("field" + i), byteArray);
}
return documentMap;
}

private void encrypt(PlaintextDocument plaintext) throws Exception {
saasShieldSdk.standard().encrypt(plaintext, metadata).get();
}

private void decrypt(EncryptedDocument document) throws Exception {
saasShieldSdk.standard().decrypt(document, metadata).get();
}

@Benchmark
public void tspEncrypt1B() throws Exception {
encrypt(smallPlaintext);
}

@Benchmark
public void tspEncrypt100B() throws Exception {
encrypt(mediumPlaintext);
}

@Benchmark
public void tspEncrypt10KB() throws Exception {
encrypt(largePlaintext);
}

@Benchmark
public void tspEncrypt1MB() throws Exception {
encrypt(extraLargePlaintext);
}

@Benchmark
public void tspDecrypt1B() throws Exception {
decrypt(smallEncrypted);
}

@Benchmark
public void tspDecrypt100B() throws Exception {
decrypt(mediumEncrypted);
}

@Benchmark
public void tspDecrypt10KB() throws Exception {
decrypt(largeEncrypted);
}

@Benchmark
public void tspDecrypt1MB() throws Exception {
decrypt(extraLargeEncrypted);
}

@Benchmark
public void batchEncrypt10DocsOf100B() throws Exception {
saasShieldSdk.standard().encryptBatch(batchPlaintexts, metadata).get();
}
}
Loading

0 comments on commit beb2fa9

Please sign in to comment.