Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import: add csv debug option #2301

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nimbus/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ type
defaultValue: 8192
name: "chunk-size" .}: uint64

csvStats* {.
hidden
desc: "Save performance statistics to CSV"
name: "debug-csv-stats".}: Option[string]

func parseCmdArg(T: type NetworkId, p: string): T
{.gcsafe, raises: [ValueError].} =
parseInt(p).T
Expand Down
54 changes: 45 additions & 9 deletions nimbus/nimbus_import.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import
chronicles,
chronos/timer,
std/strformat,
std/[strformat, strutils],
stew/io2,
./config,
./common/common,
Expand All @@ -35,7 +35,8 @@ func shortLog(a: timer.Duration, parts = int.high): string {.inline.} =
res.add(n)
v = v mod T.nanoseconds()
dec parts
if v == 0 or parts <= 0: return res
if v == 0 or parts <= 0:
return res

f("w", Week)
f("d", Day)
Expand All @@ -58,11 +59,12 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
setControlCHook(controlCHandler)

let
start = try:
com.db.getSavedStateBlockNumber().truncate(uint64) + 1
except RlpError as exc:
error "Could not read block number", err = exc.msg
quit(QuitFailure)
start =
try:
com.db.getSavedStateBlockNumber().truncate(uint64) + 1
except RlpError as exc:
error "Could not read block number", err = exc.msg
quit(QuitFailure)

chain = com.newChain()

Expand All @@ -71,6 +73,23 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
gas = 0.u256
txs = 0
time0 = Moment.now()
csv =
if conf.csvStats.isSome:
try:
let f = open(conf.csvStats.get(), fmAppend)
if f.getFileSize() == 0:
f.writeLine("block_number,blocks,txs,gas,time")
f
except IOError as exc:
error "Could not open statistics output file",
file = conf.csvStats, err = exc.msg
quit(QuitFailure)
else:
File(nil)
defer:
if csv != nil:
close(csv)

template blockNumber(): uint64 =
start + imported

Expand Down Expand Up @@ -110,7 +129,6 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
diff1 = (time2 - time1).nanoseconds().float / 1000000000
diff0 = (time2 - time0).nanoseconds().float / 1000000000

# TODO generate csv with import statistics
info "Imported blocks",
blockNumber,
blocks = imported,
Expand All @@ -122,7 +140,25 @@ proc importBlocks*(conf: NimbusConf, com: CommonRef) =
avgBps = f(imported.float / diff0),
avgTps = f(txs.float / diff0),
avgGps = f(gas.truncate(uint64).float / diff0), # TODO fix truncate
elapsed = shortLog(time2-time0, 3)
elapsed = shortLog(time2 - time0, 3)

if csv != nil:
# In the CSV, we store a line for every chunk of blocks processed so
# that the file can meaningfully be appended to when restarting the
# process - this way, each sample is independent
try:
csv.writeLine(
[
$blockNumber,
$headers.len,
$statsRes[].txs,
$statsRes[].gas,
$(time2 - time1).nanoseconds(),
].join(",")
)
csv.flushFile()
except IOError as exc:
warn "Could not write csv", err = exc.msg
headers.setLen(0)
bodies.setLen(0)

Expand Down
Loading