ncli_db: database tool

includes a benchmark tool for now
This commit is contained in:
Jacek Sieka 2020-05-28 16:19:25 +02:00 committed by tersec
parent 0493737d7a
commit 061be037d1
4 changed files with 71 additions and 7 deletions

View File

@ -24,6 +24,7 @@ TOOLS := \
ncli_pretty \
ncli_query \
ncli_transition \
ncli_db \
process_dashboard \
stack_sizes \
state_sim \

View File

@ -14,7 +14,7 @@ import
import
block_pools/[block_pools_types, clearance, candidate_chains, quarantine]
export results
export results, block_pools_types
# Block_Pools
# --------------------------------------------

58
ncli/ncli_db.nim Normal file
View File

@ -0,0 +1,58 @@
import
confutils, stats, chronicles, strformat, tables,
../beacon_chain/block_pool,
../beacon_chain/spec/[crypto, datatypes],
../beacon_chain/[beacon_chain_db, extras, state_transition, ssz],
../research/simutils,
eth/db/[kvstore, kvstore_sqlite3]
type Timers = enum
tInit = "Initialize DB"
tLoadBlock = "Load block from database"
tLoadState = "Load state from database"
tApplyBlock = "Apply block"
cli do(databaseDir: string, cmd: string):
var timers: array[Timers, RunningStat]
echo "Opening database..."
let
db = BeaconChainDB.init(kvStore SqStoreRef.init(databaseDir, "nbc").tryGet())
if not BlockPool.isInitialized(db):
echo "Database not initialized"
quit 1
echo "Initializing block pool..."
let pool = withTimerRet(timers[tInit]):
CandidateChains.init(db, {})
echo &"Loaded {pool.blocks.len} blocks, head slot {pool.head.blck.slot}"
case cmd
of "bench":
var
blockRefs: seq[BlockRef]
blocks: seq[SignedBeaconBlock]
cur = pool.head.blck
while cur != nil:
blockRefs.add cur
cur = cur.parent
for b in 1..<blockRefs.len: # Skip genesis block
withTimer(timers[tLoadBlock]):
blocks.add db.getBlock(blockRefs[blockRefs.len - b - 1].root).get()
let state = (ref HashedBeaconState)(
root: db.getBlock(blockRefs[^1].root).get().message.state_root
)
withTimer(timers[tLoadState]):
discard db.getState(state[].root, state[].data, noRollback)
for b in blocks:
withTimer(timers[tApplyBlock]):
discard state_transition(state[], b, {}, noRollback)
printTimers(true, timers)

View File

@ -1,5 +1,5 @@
import
stats, os, strformat,
stats, os, strformat, times,
../tests/[testblockutil],
../beacon_chain/[extras, ssz],
../beacon_chain/spec/[beaconstate, datatypes, digest, helpers]
@ -74,11 +74,9 @@ proc loadGenesis*(validators: int, validate: bool): ref HashedBeaconState =
res
proc printTimers*[Timers: enum](
state: BeaconState, attesters: RunningStat, validate: bool,
timers: array[Timers, RunningStat]) =
echo "Validators: ", state.validators.len, ", epoch length: ", SLOTS_PER_EPOCH
echo "Validators per attestation (mean): ", attesters.mean
validate: bool,
timers: array[Timers, RunningStat]
) =
proc fmtTime(t: float): string = &"{t * 1000 :>12.3f}, "
echo "All time are ms"
@ -92,3 +90,10 @@ proc printTimers*[Timers: enum](
echo fmtTime(timers[t].mean), fmtTime(timers[t].standardDeviationS),
fmtTime(timers[t].min), fmtTime(timers[t].max), &"{timers[t].n :>12}, ",
$t
proc printTimers*[Timers: enum](
state: BeaconState, attesters: RunningStat, validate: bool,
timers: array[Timers, RunningStat]) =
echo "Validators: ", state.validators.len, ", epoch length: ", SLOTS_PER_EPOCH
echo "Validators per attestation (mean): ", attesters.mean
printTimers(validate, timers)