summaryrefslogtreecommitdiff
path: root/ledger/perf_test.go
blob: c1e520fef7150608f218edd1783dae831da4d3bd (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (C) 2019-2021 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 ledger

import (
	"context"
	"fmt"
	"testing"

	"github.com/algorand/go-deadlock"
	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/agreement"
	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/bookkeeping"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/util/execpool"
)

func genesis(naccts int) (InitState, []basics.Address, []*crypto.SignatureSecrets) {
	return genesisWithProto(naccts, protocol.ConsensusCurrentVersion)
}
func genesisWithProto(naccts int, proto protocol.ConsensusVersion) (InitState, []basics.Address, []*crypto.SignatureSecrets) {
	blk := bookkeeping.Block{}
	blk.CurrentProtocol = proto
	blk.BlockHeader.GenesisID = "test"
	blk.FeeSink = testSinkAddr
	blk.RewardsPool = testPoolAddr
	crypto.RandBytes(blk.BlockHeader.GenesisHash[:])

	addrs := []basics.Address{}
	keys := []*crypto.SignatureSecrets{}
	accts := make(map[basics.Address]basics.AccountData)

	// 10 billion microalgos, across N accounts and pool and sink
	amount := 10 * 1000000000 * 1000000 / uint64(naccts+2)

	for i := 0; i < naccts; i++ {
		var seed crypto.Seed
		crypto.RandBytes(seed[:])
		key := crypto.GenerateSignatureSecrets(seed)
		addr := basics.Address(key.SignatureVerifier)

		keys = append(keys, key)
		addrs = append(addrs, addr)

		adata := basics.AccountData{}
		adata.MicroAlgos.Raw = amount //1000 * 1000 * 1000 * 1000 / uint64(naccts)
		accts[addr] = adata
	}

	pooldata := basics.AccountData{}
	pooldata.MicroAlgos.Raw = amount //1000 * 1000 * 1000 * 1000
	pooldata.Status = basics.NotParticipating
	accts[testPoolAddr] = pooldata

	sinkdata := basics.AccountData{}
	sinkdata.MicroAlgos.Raw = amount //1000 * 1000 * 1000 * 1000
	sinkdata.Status = basics.NotParticipating
	accts[testSinkAddr] = sinkdata

	genesisHash := blk.BlockHeader.GenesisHash

	return InitState{blk, accts, genesisHash}, addrs, keys
}

func BenchmarkManyAccounts(b *testing.B) {
	deadlock.Opts.Disable = true

	b.StopTimer()

	genesisInitState, addrs, _ := genesis(1)
	addr := addrs[0]

	dbName := fmt.Sprintf("%s.%d", b.Name(), crypto.RandUint64())
	const inMem = true
	cfg := config.GetDefaultLocal()
	cfg.Archival = true
	l, err := OpenLedger(logging.Base(), dbName, inMem, genesisInitState, cfg)
	require.NoError(b, err)
	defer l.Close()

	blk := genesisInitState.Block
	for i := 0; i < b.N; i++ {
		blk = bookkeeping.MakeBlock(blk.BlockHeader)

		proto, ok := config.Consensus[blk.CurrentProtocol]
		require.True(b, ok)

		var txbytes int
		for {
			var st transactions.SignedTxn
			crypto.RandBytes(st.Sig[:])
			st.Txn.Type = protocol.PaymentTx
			st.Txn.Sender = addr
			st.Txn.Fee = basics.MicroAlgos{Raw: 1}
			st.Txn.Amount = basics.MicroAlgos{Raw: 1}
			crypto.RandBytes(st.Txn.Receiver[:])

			txib, err := blk.EncodeSignedTxn(st, transactions.ApplyData{})
			require.NoError(b, err)

			txlen := len(protocol.Encode(&txib))
			if txbytes+txlen > proto.MaxTxnBytesPerBlock {
				break
			}

			txbytes += txlen
			blk.Payset = append(blk.Payset, txib)
		}

		var c agreement.Certificate
		b.StartTimer()
		err := l.AddBlock(blk, c)
		b.StopTimer()
		require.NoError(b, err)
	}
}

func BenchmarkValidate(b *testing.B) {
	b.StopTimer()

	genesisInitState, addrs, keys := genesis(10000)

	backlogPool := execpool.MakeBacklog(nil, 0, execpool.LowPriority, nil)
	defer backlogPool.Shutdown()

	dbName := fmt.Sprintf("%s.%d", b.Name(), crypto.RandUint64())
	const inMem = true
	cfg := config.GetDefaultLocal()
	cfg.Archival = true
	l, err := OpenLedger(logging.Base(), dbName, inMem, genesisInitState, cfg)
	require.NoError(b, err)
	defer l.Close()

	blk := genesisInitState.Block
	for i := 0; i < b.N; i++ {
		newblk := bookkeeping.MakeBlock(blk.BlockHeader)

		proto, ok := config.Consensus[newblk.CurrentProtocol]
		require.True(b, ok)

		var txbytes int
		for i := 0; i < 10000; i++ {
			t := transactions.Transaction{
				Type: protocol.PaymentTx,
				Header: transactions.Header{
					Sender:     addrs[i],
					Fee:        basics.MicroAlgos{Raw: 1},
					FirstValid: newblk.Round(),
					LastValid:  newblk.Round(),
				},
				PaymentTxnFields: transactions.PaymentTxnFields{
					Amount: basics.MicroAlgos{Raw: 1},
				},
			}
			crypto.RandBytes(t.Receiver[:])
			st := t.Sign(keys[i])

			txib, err := newblk.EncodeSignedTxn(st, transactions.ApplyData{})
			require.NoError(b, err)

			txlen := len(protocol.Encode(&txib))
			if txbytes+txlen > proto.MaxTxnBytesPerBlock {
				break
			}

			txbytes += txlen
			newblk.Payset = append(newblk.Payset, txib)
		}

		newblk.BlockHeader.TxnRoot, err = newblk.PaysetCommit()
		require.NoError(b, err)

		b.StartTimer()
		_, err = l.Validate(context.Background(), newblk, backlogPool)
		b.StopTimer()
		require.NoError(b, err)
	}
}