From f7d2add7e002b380abdbdb991135b224914d26e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Wed, 17 Jan 2024 10:56:09 +0100 Subject: [PATCH] fix() rename benchmarks to gas estimation --- Makefile | 6 +-- fhevm/benchmarks_test.go | 73 ------------------------------------- fhevm/gasestimation_test.go | 73 +++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 76 deletions(-) delete mode 100644 fhevm/benchmarks_test.go create mode 100644 fhevm/gasestimation_test.go diff --git a/Makefile b/Makefile index 556b0c8..d26b514 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,9 @@ 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: gasEstimation +gasEstimation: build-tfhe-rs-capi + cd fhevm && go test -count=1 -v . -run GasEstimation .PHONY: build-tfhe-rs-capi build-tfhe-rs-capi: diff --git a/fhevm/benchmarks_test.go b/fhevm/benchmarks_test.go deleted file mode 100644 index ef57018..0000000 --- a/fhevm/benchmarks_test.go +++ /dev/null @@ -1,73 +0,0 @@ -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) { FheShr(t, fheUintType, false) }) - benchTests(t, "ScalarShr", func(fheUintType FheUintType) { FheShr(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) }) -} diff --git a/fhevm/gasestimation_test.go b/fhevm/gasestimation_test.go new file mode 100644 index 0000000..d747627 --- /dev/null +++ b/fhevm/gasestimation_test.go @@ -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 estimateTests(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 TestGasEstimation(t *testing.T) { + estimateTests(t, "not", func(fheUintType FheUintType) { FheNot(t, fheUintType, false) }) + + estimateTests(t, "and", func(fheUintType FheUintType) { FheBitAnd(t, fheUintType, false) }) + + estimateTests(t, "eq", func(fheUintType FheUintType) { FheEq(t, fheUintType, false) }) + estimateTests(t, "ScalarEq", func(fheUintType FheUintType) { FheEq(t, fheUintType, true) }) + + estimateTests(t, "shr", func(fheUintType FheUintType) { FheShr(t, fheUintType, false) }) + estimateTests(t, "ScalarShr", func(fheUintType FheUintType) { FheShr(t, fheUintType, true) }) + + estimateTests(t, "min", func(fheUintType FheUintType) { FheMin(t, fheUintType, false) }) + estimateTests(t, "ScalarMin", func(fheUintType FheUintType) { FheMin(t, fheUintType, true) }) + + estimateTests(t, "add", func(fheUintType FheUintType) { FheAdd(t, fheUintType, false) }) + estimateTests(t, "ScalarAdd", func(fheUintType FheUintType) { FheAdd(t, fheUintType, true) }) + + estimateTests(t, "sub", func(fheUintType FheUintType) { FheSub(t, fheUintType, false) }) + estimateTests(t, "ScalarSub", func(fheUintType FheUintType) { FheSub(t, fheUintType, true) }) + + estimateTests(t, "mul", func(fheUintType FheUintType) { FheMul(t, fheUintType, false) }) + estimateTests(t, "ScalarMul", func(fheUintType FheUintType) { FheMul(t, fheUintType, true) }) + + estimateTests(t, "ScalarDiv", func(fheUintType FheUintType) { FheDiv(t, fheUintType, true) }) + + estimateTests(t, "IfThenElse", func(fheUintType FheUintType) { FheIfThenElse(t, fheUintType, 1) }) +}