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

Add benchmarks test #56

Merged
merged 10 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ build: build-tfhe-rs-capi
test: build-tfhe-rs-capi
cd fhevm && go test -v .

.PHONY: benchmarks
benchmarks: build-tfhe-rs-capi
cd fhevm && go test -count=1 -v . -run Benchmarks

.PHONY: build-tfhe-rs-capi
build-tfhe-rs-capi:
cd tfhe-rs && RUSTFLAGS="" make build_c_api_experimental_deterministic_fft \
Expand Down
73 changes: 73 additions & 0 deletions fhevm/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package fhevm

import (
"testing"
"time"
)

type operation func(FheUintType)

const numBenchmarkRuns = 5

func convertInGas(t *testing.T, name string, elapsed [numBenchmarkRuns]time.Duration) {
lowest := elapsed[0]

// Find the lowest duration in the array
for i := 1; i < numBenchmarkRuns; i++ {
if elapsed[i] < lowest {
lowest = elapsed[i]
}
}

gasUsed := int64(lowest) / 1000 // 1s = 1,000,000 gas
gasUsed = gasUsed / 7 * 10 // 0.7s = 1,000,000 gas
gasUsed = gasUsed / 1000 // Divide to round it

t.Logf("%s in %s => %d", name, lowest, gasUsed*1000)
}

func runTest(t *testing.T, name string, fn operation, bits string, fheUintType FheUintType) {
var elapsed [numBenchmarkRuns]time.Duration
n := 0
for n < numBenchmarkRuns {
start := time.Now()
fn(fheUintType)
elapsed[n] = time.Since(start)
n += 1
}
convertInGas(t, name+bits, elapsed)
}

func benchTests(t *testing.T, name string, fn operation) {
runTest(t, name, fn, "8", FheUint8)
runTest(t, name, fn, "16", FheUint16)
runTest(t, name, fn, "32", FheUint32)
}

func TestBenchmarks(t *testing.T) {
benchTests(t, "not", func(fheUintType FheUintType) { FheNot(t, fheUintType, false) })

benchTests(t, "and", func(fheUintType FheUintType) { FheBitAnd(t, fheUintType, false) })

benchTests(t, "eq", func(fheUintType FheUintType) { FheEq(t, fheUintType, false) })
benchTests(t, "ScalarEq", func(fheUintType FheUintType) { FheEq(t, fheUintType, true) })

benchTests(t, "shr", func(fheUintType FheUintType) { FheMin(t, fheUintType, false) })
benchTests(t, "ScalarShr", func(fheUintType FheUintType) { FheMin(t, fheUintType, true) })

benchTests(t, "min", func(fheUintType FheUintType) { FheMin(t, fheUintType, false) })
benchTests(t, "ScalarMin", func(fheUintType FheUintType) { FheMin(t, fheUintType, true) })

benchTests(t, "add", func(fheUintType FheUintType) { FheAdd(t, fheUintType, false) })
benchTests(t, "ScalarAdd", func(fheUintType FheUintType) { FheAdd(t, fheUintType, true) })

benchTests(t, "sub", func(fheUintType FheUintType) { FheSub(t, fheUintType, false) })
benchTests(t, "ScalarSub", func(fheUintType FheUintType) { FheSub(t, fheUintType, true) })

benchTests(t, "mul", func(fheUintType FheUintType) { FheMul(t, fheUintType, false) })
benchTests(t, "ScalarMul", func(fheUintType FheUintType) { FheMul(t, fheUintType, true) })

benchTests(t, "ScalarDiv", func(fheUintType FheUintType) { FheDiv(t, fheUintType, true) })

benchTests(t, "IfThenElse", func(fheUintType FheUintType) { FheIfThenElse(t, fheUintType, 1) })
}
10 changes: 5 additions & 5 deletions fhevm/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ func FheMul(t *testing.T, fheUintType FheUintType, scalar bool) {
switch fheUintType {
case FheUint8:
lhs = 2
rhs = 1
rhs = 3
case FheUint16:
lhs = 169
rhs = 5
Expand Down Expand Up @@ -1637,14 +1637,14 @@ func FheDiv(t *testing.T, fheUintType FheUintType, scalar bool) {
var lhs, rhs uint64
switch fheUintType {
case FheUint8:
lhs = 4
rhs = 2
lhs = 6
rhs = 7
case FheUint16:
lhs = 721
rhs = 1000
rhs = 251
case FheUint32:
lhs = 137
rhs = 17
rhs = 65521
}
expected := lhs / rhs
depth := 1
Expand Down