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

2-/4 No compression bench #1846

Closed
wants to merge 5 commits into from
Closed
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
16 changes: 8 additions & 8 deletions clients/feeder/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/starknet"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -92,9 +92,9 @@ func NopBackoff(d time.Duration) time.Duration {
}

// NewTestClient returns a client and a function to close a test server.
func NewTestClient(t *testing.T, network *utils.Network) *Client {
srv := newTestServer(t, network)
t.Cleanup(srv.Close)
func NewTestClient(tb pebble.TestBench, network *utils.Network) *Client {
srv := newTestServer(tb, network)
tb.Cleanup(srv.Close)
ua := "Juno/v0.0.1-test Starknet Implementation"
apiKey := "API_KEY"

Expand All @@ -117,19 +117,19 @@ func NewTestClient(t *testing.T, network *utils.Network) *Client {
return c
}

func newTestServer(t *testing.T, network *utils.Network) *httptest.Server {
func newTestServer(tb pebble.TestBench, network *utils.Network) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
queryMap, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

assert.Equal(t, []string{"API_KEY"}, r.Header["X-Throttling-Bypass"])
assert.Equal(t, []string{"Juno/v0.0.1-test Starknet Implementation"}, r.Header["User-Agent"])
assert.Equal(tb, []string{"API_KEY"}, r.Header["X-Throttling-Bypass"])
assert.Equal(tb, []string{"Juno/v0.0.1-test Starknet Implementation"}, r.Header["User-Agent"])

wd, err := os.Getwd()
require.NoError(t, err)
require.NoError(tb, err)

base := wd[:strings.LastIndex(wd, "juno")+4]
queryArg := ""
Expand Down
20 changes: 14 additions & 6 deletions db/pebble/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"sync"
"testing"

"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/utils"
Expand Down Expand Up @@ -31,6 +30,7 @@
Logger: logger,
Cache: pebble.NewCache(int64(cache * utils.Megabyte)),
MaxOpenFiles: maxOpenFiles,
Levels: []pebble.LevelOptions{{Compression: pebble.NoCompression}},
})
if err != nil {
return nil, err
Expand All @@ -41,19 +41,27 @@
// NewMem opens a new in-memory database
func NewMem() (db.DB, error) {
return newPebble("", &pebble.Options{
FS: vfs.NewMem(),
FS: vfs.NewMem(),
Levels: []pebble.LevelOptions{{Compression: pebble.NoCompression}},
})
}

type TestBench interface {
Fatalf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Cleanup(func())
FailNow()
}

// NewMemTest opens a new in-memory database, panics on error
func NewMemTest(t *testing.T) db.DB {
func NewMemTest(tb TestBench) db.DB {
memDB, err := NewMem()
if err != nil {
t.Fatalf("create in-memory db: %v", err)
tb.Fatalf("create in-memory db: %v", err)

Check warning on line 60 in db/pebble/db.go

View check run for this annotation

Codecov / codecov/patch

db/pebble/db.go#L60

Added line #L60 was not covered by tests
}
t.Cleanup(func() {
tb.Cleanup(func() {
if err := memDB.Close(); err != nil {
t.Errorf("close in-memory db: %v", err)
tb.Errorf("close in-memory db: %v", err)

Check warning on line 64 in db/pebble/db.go

View check run for this annotation

Codecov / codecov/patch

db/pebble/db.go#L64

Added line #L64 was not covered by tests
}
})
return memDB
Expand Down
66 changes: 66 additions & 0 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/mocks"
"github.com/NethermindEth/juno/rpc"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/sync"
"github.com/NethermindEth/juno/utils"
Expand Down Expand Up @@ -225,3 +226,68 @@ func TestSubscribeNewHeads(t *testing.T) {
require.Equal(t, want.Header, got)
sub.Unsubscribe()
}

func BenchmarkSync(b *testing.B) {
mockCtrl := gomock.NewController(b)
defer mockCtrl.Finish()

client := feeder.NewTestClient(b, &utils.Mainnet)
gw := adaptfeeder.New(client)
log := utils.NewNopZapLogger()

testDB := pebble.NewMemTest(b)
bc := blockchain.New(testDB, &utils.Sepolia)
b0, err := gw.BlockByNumber(context.Background(), 0)
require.NoError(b, err)
s0, err := gw.StateUpdate(context.Background(), 0)
require.NoError(b, err)
require.NoError(b, bc.Store(b0, &core.BlockCommitments{}, s0, nil))

synchronizer := sync.New(bc, gw, log, time.Duration(0), false)

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
err := synchronizer.Run(ctx)
if err != nil {
b.Error("failed to synchronise: ", err)
}
cancel()
}
}

func BenchmarkRPC(b *testing.B) {
mockCtrl := gomock.NewController(b)
defer mockCtrl.Finish()

client := feeder.NewTestClient(b, &utils.Mainnet)
gw := adaptfeeder.New(client)
log := utils.NewNopZapLogger()

testDB := pebble.NewMemTest(b)
bc := blockchain.New(testDB, &utils.Sepolia)
b0, err := gw.BlockByNumber(context.Background(), 0)
require.NoError(b, err)
s0, err := gw.StateUpdate(context.Background(), 0)
require.NoError(b, err)
require.NoError(b, bc.Store(b0, &core.BlockCommitments{}, s0, nil))

synchronizer := sync.New(bc, gw, log, time.Duration(0), false)
handler := rpc.New(bc, synchronizer, nil, "", &utils.Mainnet, nil)

ctxSync, cancelSync := context.WithTimeout(context.Background(), timeout)
require.NoError(b, synchronizer.Run(ctxSync))
cancelSync()

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
_, err := handler.BlockTransactionCount(rpc.BlockID{Latest: true})
if err != nil {
b.Error("RPC call failed: ", err)
}
}
}
Loading