Skip to content
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

test: improve benchmarks #65

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ethers": "^6.8.0",
"fhevm": "0.6.0-0",
"fhevm-core-contracts": "0.1.0-2",
"fhevmjs": "^0.6.0-16",
"fhevmjs": "^0.6.0-18",
"fs-extra": "^10.1.0",
"globals": "^15.9.0",
"hardhat": "^2.22.8",
Expand Down
36 changes: 36 additions & 0 deletions e2e/test/benchmarks/createInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from "chai";

import { createInstance } from "../instance";
import { getSigners, initSigners } from "../signers";
import { Timing, displayTimings } from "./utils";

describe("Benchmark input creation", function () {
before(async function () {
await initSigners();
this.signers = await getSigners();
this.fhevm = await createInstance();
});

it("should create 10 inputs and send them in parrallel", async function () {
const input = this.fhevm.createEncryptedInput(
"0x1337AA343Db8D44238Fe40486aDeECdf354e1f60",
this.signers.alice.address,
);
input.add4(9n);
input.add128(13n);
const ciphertext = await input._prove();
const timings: Timing[] = await Promise.all(
new Array(10).fill(null).map(async (_, i) => {
let timing: Timing = {
description: `Create input ${i}`,
time: 0,
};
let start = Date.now();
await input._verify(ciphertext);
timing.time = Date.now() - start;
return timing;
}),
);
displayTimings(timings);
});
});
78 changes: 6 additions & 72 deletions e2e/test/benchmarks/benchmarks.ts → e2e/test/benchmarks/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import { expect } from "chai";

import { deployEncryptedERC20Fixture } from "../encryptedERC20/EncryptedERC20.fixture";
import { deployReencryptFixture } from "../gateway/Reencrypt.fixture";
import { Decrypt, createDecrypt, createInstance } from "../instance";
import { getSigners, initSigners } from "../signers";
import { Timing, displayTimings } from "./utils";

type Timing = {
description: string;
time: number;
};

describe("Benchmarks", function () {
describe("Benchmark ERC20", function () {
const timings: Timing[] = [];
let decrypt: Decrypt;
before(async function () {
await initSigners();
this.signers = await getSigners();
this.fhevm = await createInstance();
});

it("benchmarks erc20", async function () {
const erc20 = await deployEncryptedERC20Fixture();
this.erc20Address = await erc20.getAddress();
this.erc20 = erc20;
});

it("benchmarks erc20", async function () {
decrypt = createDecrypt(this.fhevm, this.signers.alice, this.erc20Address);
// Minting the contract
let mintTiming: Timing = {
Expand Down Expand Up @@ -80,67 +75,6 @@ describe("Benchmarks", function () {
reencryptTiming.time = Date.now() - start;
timings.push(reencryptTiming);

console.log(timings);
});

it("benchmarks reencrypt sequential", async function () {
const reencrypt = await deployReencryptFixture();
this.reencryptAddress = await reencrypt.getAddress();
this.reencrypt = reencrypt;
await new Promise((resolve) => setTimeout(resolve, 10000));
decrypt = createDecrypt(this.fhevm, this.signers.alice, this.reencryptAddress);
let reencryptTimings: Timing[] = [];

const types = [1, 4, 8, 16, 32, 64, 128, 256];
const reencryptPromise = types.reduce(async (p, type) => {
await p;
let timing: Timing = {
description: `Reencrypt ${type}bits`,
time: 0,
};
let start = Date.now();
let handle: bigint;
if (type == 1) {
handle = await this.reencrypt.resultBool();
} else {
handle = await this.reencrypt[`result${type}`]();
}
const result = await decrypt(handle);
timing.time = Date.now() - start;
reencryptTimings.push(timing);
}, Promise.resolve());

await reencryptPromise;
console.log(reencryptTimings);
});

it("benchmarks reencrypt parrallel", async function () {
const reencrypt = await deployReencryptFixture();
this.reencryptAddress = await reencrypt.getAddress();
this.reencrypt = reencrypt;
await new Promise((resolve) => setTimeout(resolve, 10000));
decrypt = createDecrypt(this.fhevm, this.signers.alice, this.reencryptAddress);
let reencryptTimings: Timing[] = [];

const types = [1, 4, 8, 16, 32, 64, 128, 256];
const reencryptPromises = types.map(async (type) => {
let timing: Timing = {
description: `Reencrypt ${type}bits`,
time: 0,
};
let start = Date.now();
let handle: bigint;
if (type == 1) {
handle = await this.reencrypt.resultBool();
} else {
handle = await this.reencrypt[`result${type}`]();
}
const result = await decrypt(handle);
timing.time = Date.now() - start;
reencryptTimings.push(timing);
});

await Promise.all(reencryptPromises);
console.log(reencryptTimings);
console.log(displayTimings(timings));
});
});
79 changes: 79 additions & 0 deletions e2e/test/benchmarks/reencrypt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { expect } from "chai";

import { deployReencryptFixture } from "../gateway/Reencrypt.fixture";
import { Decrypt, createDecrypt, createInstance } from "../instance";
import { getSigners, initSigners } from "../signers";
import { Timing, displayTimings } from "./utils";

describe("Benchmark reencrypt", function () {
const timings: Timing[] = [];
let decrypt: Decrypt;
before(async function () {
await initSigners();
this.signers = await getSigners();
this.fhevm = await createInstance();
const reencrypt = await deployReencryptFixture();
this.reencryptAddress = await reencrypt.getAddress();
this.reencrypt = reencrypt;
});

it("benchmarks reencrypt sequential", async function () {
await new Promise((resolve) => setTimeout(resolve, 10000));
decrypt = createDecrypt(this.fhevm, this.signers.alice, this.reencryptAddress);
let reencryptTimings: Timing[] = [];

const types = [1, 4, 8, 16, 32, 64, 128, 256];
const reencryptPromise = types.reduce(async (p, type) => {
const label = type === 1 ? "a boolean" : `a ${type}bits value`;
await p;
let timing: Timing = {
description: `Reencrypt (sequential) ${label}`,
time: 0,
};
let start = Date.now();
let handle: bigint;
if (type == 1) {
handle = await this.reencrypt.resultBool();
} else {
handle = await this.reencrypt[`result${type}`]();
}
const result = await decrypt(handle);
timing.time = Date.now() - start;
reencryptTimings.push(timing);
}, Promise.resolve());

await reencryptPromise;
displayTimings(reencryptTimings);
});

it("benchmarks reencrypt parrallel", async function () {
const reencrypt = await deployReencryptFixture();
this.reencryptAddress = await reencrypt.getAddress();
this.reencrypt = reencrypt;
await new Promise((resolve) => setTimeout(resolve, 10000));
decrypt = createDecrypt(this.fhevm, this.signers.alice, this.reencryptAddress);
let reencryptTimings: Timing[] = [];

const types = [1, 4, 8, 16, 32, 64, 128, 256];
const reencryptPromises = types.map(async (type) => {
const label = type === 1 ? "a boolean" : `a ${type}bits value`;
let timing: Timing = {
description: `Reencrypt (parrallel) ${label}`,
time: 0,
};
let start = Date.now();
let handle: bigint;
if (type == 1) {
handle = await this.reencrypt.resultBool();
} else {
handle = await this.reencrypt[`result${type}`]();
}
const result = await decrypt(handle);
timing.time = Date.now() - start;
reencryptTimings.push(timing);
});

await Promise.all(reencryptPromises);
displayTimings(reencryptTimings);
});
});
14 changes: 14 additions & 0 deletions e2e/test/benchmarks/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Timing = {
description: string;
time: number;
};

export const displayTimings = (timings: Timing[]) => {
timings.forEach((t: Timing) => {
let time = `${t.time}ms`;
if (t.time >= 100) {
time = `${Math.round(t.time / 100) / 10}s`;
}
console.log(`${t.description} in ${time}`);
});
};