@@ -2,12 +2,14 @@ package sqlite
22
33import (
44 "testing"
5+ "time"
56
67 "go.sia.tech/core/consensus"
78 proto4 "go.sia.tech/core/rhp/v4"
89 "go.sia.tech/core/types"
910 "go.sia.tech/explored/explorer"
1011 "go.sia.tech/explored/internal/testutil"
12+ "lukechampine.com/frand"
1113)
1214
1315func TestMetrics (t * testing.T ) {
@@ -436,3 +438,52 @@ func TestV2Metrics(t *testing.T) {
436438
437439 assertMetrics (metricsGenesis )
438440}
441+
442+ func BenchmarkBlockTimeMetrics (b * testing.B ) {
443+ n := newTestChain (b , false , nil )
444+
445+ const month = 30 * 24 * time .Hour
446+ const blockTime = 10 * time .Minute
447+
448+ now := time .Now ().Add (- month )
449+ err := n .db .transaction (func (tx * txn ) error {
450+ blockStmt , err := tx .Prepare (`INSERT INTO blocks(id, height, parent_id, nonce, timestamp, leaf_index) VALUES (?, ?, ?, ?, ?, ?)` )
451+ if err != nil {
452+ return err
453+ }
454+ defer blockStmt .Close ()
455+
456+ var parentID types.BlockID
457+ nonce , leafIndex := encode (uint64 (0 )), encode (uint64 (0 ))
458+ for i := range 500000 {
459+ if i % 10000 == 0 {
460+ b .Log ("Adding block:" , i )
461+ }
462+ id := types .BlockID (frand .Entropy256 ())
463+ if _ , err := blockStmt .Exec (encode (id ), i , encode (parentID ), nonce , encode (now ), leafIndex ); err != nil {
464+ b .Fatal (err )
465+ }
466+
467+ parentID = id
468+ now = now .Add (blockTime )
469+ }
470+ return nil
471+ })
472+ if err != nil {
473+ b .Fatal (err )
474+ }
475+
476+ for b .Loop () {
477+ blockTimes , err := n .db .BlockTimeMetrics (blockTime )
478+ if err != nil {
479+ b .Fatal (err )
480+ }
481+ if blockTimes .Day != blockTime {
482+ b .Fatalf ("expected %v average block time for past day, got %v" , blockTime , blockTimes .Day )
483+ } else if blockTimes .Week != blockTime {
484+ b .Fatalf ("expected %v average block time for past week, got %v" , blockTime , blockTimes .Week )
485+ } else if blockTimes .Month != blockTime {
486+ b .Fatalf ("expected %v average block time for past month, got %v" , blockTime , blockTimes .Month )
487+ }
488+ }
489+ }
0 commit comments