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 5 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
89 changes: 89 additions & 0 deletions fhevm/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
immortal-tofu marked this conversation as resolved.
Show resolved Hide resolved

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) })
}
6 changes: 3 additions & 3 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,8 +1637,8 @@ func FheDiv(t *testing.T, fheUintType FheUintType, scalar bool) {
var lhs, rhs uint64
switch fheUintType {
case FheUint8:
lhs = 4
rhs = 2
lhs = 6
rhs = 3
case FheUint16:
lhs = 721
rhs = 1000
Expand Down