forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_statistics.go
73 lines (61 loc) · 2.05 KB
/
example_statistics.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
package main
import (
"context"
"fmt"
"io"
"github.com/sirupsen/logrus"
"github.com/stellar/go/ingest"
backends "github.com/stellar/go/ingest/ledgerbackend"
"github.com/stellar/go/support/log"
)
func statistics() {
ctx := context.Background()
// Only log errors from the backend to keep output cleaner.
lg := log.New()
lg.SetLevel(logrus.ErrorLevel)
config.Log = lg
backend, err := backends.NewCaptive(config)
panicIf(err)
defer backend.Close()
// Prepare a range to be ingested:
var startingSeq uint32 = 2 // can't start with genesis ledger
var ledgersToRead uint32 = 10000
fmt.Printf("Preparing range (%d ledgers)...\n", ledgersToRead)
ledgerRange := backends.BoundedRange(startingSeq, startingSeq+ledgersToRead)
err = backend.PrepareRange(ctx, ledgerRange)
panicIf(err)
// These are the statistics that we're tracking.
var successfulTransactions, failedTransactions int
var operationsInSuccessful, operationsInFailed int
for seq := startingSeq; seq <= startingSeq+ledgersToRead; seq++ {
fmt.Printf("Processed ledger %d...\r", seq)
txReader, err := ingest.NewLedgerTransactionReader(
ctx, backend, config.NetworkPassphrase, seq,
)
panicIf(err)
defer txReader.Close()
// Read each transaction within the ledger, extract its operations, and
// accumulate the statistics we're interested in.
for {
tx, err := txReader.Read()
if err == io.EOF {
break
}
panicIf(err)
envelope := tx.Envelope
operationCount := len(envelope.Operations())
if tx.Result.Successful() {
successfulTransactions++
operationsInSuccessful += operationCount
} else {
failedTransactions++
operationsInFailed += operationCount
}
}
}
fmt.Println("\nDone. Results:")
fmt.Printf(" - total transactions: %d\n", successfulTransactions+failedTransactions)
fmt.Printf(" - succeeded / failed: %d / %d\n", successfulTransactions, failedTransactions)
fmt.Printf(" - total operations: %d\n", operationsInSuccessful+operationsInFailed)
fmt.Printf(" - succeeded / failed: %d / %d\n", operationsInSuccessful, operationsInFailed)
}