summaryrefslogtreecommitdiff
path: root/data/account/participationRegistryBench_test.go
blob: 70790ae748d5ca177dc28f749c48402fa0506028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package account

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/util/db"
)

func benchmarkKeyRegistration(numKeys int, b *testing.B) {
	// setup
	rootDB, err := db.OpenPair(b.Name(), true)
	if err != nil {
		b.Fail()
	}
	registry, err := makeParticipationRegistry(rootDB, logging.TestingLog(b))
	if err != nil {
		b.Fail()
	}

	// Insert records so that we can t
	b.Run(fmt.Sprintf("KeyInsert_%d", numKeys), func(b *testing.B) {
		a := require.New(b)
		for n := 0; n < b.N; n++ {
			for key := 0; key < numKeys; key++ {
				p := makeTestParticipation(a, key, basics.Round(0), basics.Round(1000000), 3)
				registry.Insert(p)
			}
		}
	})

	// The first call to Register updates the DB.
	b.Run(fmt.Sprintf("KeyRegistered_%d", numKeys), func(b *testing.B) {
		a := require.New(b)
		for n := 0; n < b.N; n++ {
			for key := 0; key < numKeys; key++ {
				p := makeTestParticipation(a, key, basics.Round(0), basics.Round(1000000), 3)

				// Unfortunately we need to repeatedly clear out the registration fields to ensure the
				// db update runs each time this is called.
				record := registry.cache[p.ID()]
				record.EffectiveFirst = 0
				record.EffectiveLast = 0
				registry.cache[p.ID()] = record
				registry.Register(p.ID(), 50)
			}
		}
	})

	// The keys should now be updated, so Register is a no-op.
	b.Run(fmt.Sprintf("NoOp_%d", numKeys), func(b *testing.B) {
		a := require.New(b)
		for n := 0; n < b.N; n++ {
			for key := 0; key < numKeys; key++ {
				p := makeTestParticipation(a, key, basics.Round(0), basics.Round(1000000), 3)
				registry.Register(p.ID(), 50)
			}
		}
	})
}

func BenchmarkKeyRegistration1(b *testing.B)  { benchmarkKeyRegistration(1, b) }
func BenchmarkKeyRegistration5(b *testing.B)  { benchmarkKeyRegistration(5, b) }
func BenchmarkKeyRegistration10(b *testing.B) { benchmarkKeyRegistration(10, b) }
func BenchmarkKeyRegistration50(b *testing.B) { benchmarkKeyRegistration(50, b) }