forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catchupaccessor_test.go
133 lines (121 loc) · 4.65 KB
/
catchupaccessor_test.go
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
// 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"
"encoding/binary"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"
)
func benchmarkRestoringFromCatchpointFileHelper(b *testing.B) {
genesisInitState, _ := testGenerateInitState(b, protocol.ConsensusCurrentVersion, 100)
const inMem = false
log := logging.TestingLog(b)
cfg := config.GetDefaultLocal()
cfg.Archival = false
log.SetLevel(logging.Warn)
dbBaseFileName := strings.Replace(b.Name(), "/", "_", -1)
// delete database files, in case they were left there by previous iterations of this test.
os.Remove(dbBaseFileName + ".block.sqlite")
os.Remove(dbBaseFileName + ".tracker.sqlite")
l, err := OpenLedger(log, dbBaseFileName, inMem, genesisInitState, cfg)
require.NoError(b, err, "could not open ledger")
defer func() {
l.Close()
os.Remove(dbBaseFileName + ".block.sqlite")
os.Remove(dbBaseFileName + ".tracker.sqlite")
}()
catchpointAccessor := MakeCatchpointCatchupAccessor(l, log)
catchpointAccessor.ResetStagingBalances(context.Background(), true)
accountsCount := uint64(b.N)
fileHeader := CatchpointFileHeader{
Version: catchpointFileVersion,
BalancesRound: basics.Round(0),
BlocksRound: basics.Round(0),
Totals: ledgercore.AccountTotals{},
TotalAccounts: accountsCount,
TotalChunks: (accountsCount + BalancesPerCatchpointFileChunk - 1) / BalancesPerCatchpointFileChunk,
Catchpoint: "",
BlockHeaderDigest: crypto.Digest{},
}
encodedFileHeader := protocol.Encode(&fileHeader)
var progress CatchpointCatchupAccessorProgress
err = catchpointAccessor.ProgressStagingBalances(context.Background(), "content.msgpack", encodedFileHeader, &progress)
require.NoError(b, err)
// pre-create all encoded chunks.
accounts := uint64(0)
encodedAccountChunks := make([][]byte, 0, accountsCount/BalancesPerCatchpointFileChunk+1)
last64KIndex := -1
for accounts < accountsCount {
// generate a chunk;
chunkSize := accountsCount - accounts
if chunkSize > BalancesPerCatchpointFileChunk {
chunkSize = BalancesPerCatchpointFileChunk
}
if accounts >= accountsCount-64*1024 && last64KIndex == -1 {
last64KIndex = len(encodedAccountChunks)
}
var balances catchpointFileBalancesChunk
balances.Balances = make([]encodedBalanceRecord, chunkSize)
for i := uint64(0); i < chunkSize; i++ {
var randomAccount encodedBalanceRecord
accountData := basics.AccountData{}
accountData.MicroAlgos.Raw = crypto.RandUint63()
randomAccount.AccountData = protocol.Encode(&accountData)
crypto.RandBytes(randomAccount.Address[:])
binary.LittleEndian.PutUint64(randomAccount.Address[:], accounts+i)
balances.Balances[i] = randomAccount
}
encodedAccountChunks = append(encodedAccountChunks, protocol.Encode(&balances))
accounts += chunkSize
}
b.ResetTimer()
accounts = uint64(0)
var last64KStart time.Time
for len(encodedAccountChunks) > 0 {
encodedAccounts := encodedAccountChunks[0]
encodedAccountChunks = encodedAccountChunks[1:]
if last64KIndex == 0 {
last64KStart = time.Now()
}
err = catchpointAccessor.ProgressStagingBalances(context.Background(), "balances.XX.msgpack", encodedAccounts, &progress)
require.NoError(b, err)
last64KIndex--
}
if !last64KStart.IsZero() {
last64KDuration := time.Now().Sub(last64KStart)
b.ReportMetric(float64(last64KDuration.Nanoseconds())/float64(64*1024), "ns/last_64k_account")
}
}
func BenchmarkRestoringFromCatchpointFile(b *testing.B) {
benchSizes := []int{1024 * 100, 1024 * 200, 1024 * 400, 1024 * 800}
for _, size := range benchSizes {
b.Run(fmt.Sprintf("Restore-%d", size), func(b *testing.B) {
b.N = size
benchmarkRestoringFromCatchpointFileHelper(b)
})
}
}