add store benchmark logic

This commit is contained in:
munna0908 2025-03-06 18:46:15 +05:30
parent 02833b81e5
commit 7b16d7d15d
No known key found for this signature in database
GPG Key ID: 2FFCD637E937D3E6
2 changed files with 109 additions and 4 deletions

View File

@ -0,0 +1,105 @@
import std/[sequtils, strformat, os, options]
import std/[times, strutils, terminal]
import pkg/questionable
import std/random
import pkg/questionable/results
import pkg/datastore
import pkg/codex/blocktype as bt
import pkg/libp2p/[cid, multicodec]
import pkg/codex/merkletree/codex
import pkg/codex/stores/repostore/[store, types, operations]
import pkg/codex/utils
import ../utils
import ../../tests/codex/helpers
let DataDir = "/Users/rahul/Work/repos/dataDir"
var repoDs = Datastore(
FSDatastore.new(DataDir, depth = 5).expect("Should create repo file data store!")
)
var metaDs = Datastore(
LevelDbDatastore.new(DataDir).expect("Should create repo LevelDB data store!")
)
proc generateRandomBytes(size: int): seq[byte] =
randomize()
result = newSeq[byte](size)
for i in 0 ..< size:
result[i] = byte(rand(0 .. 255))
proc createTestBlock(size: int): bt.Block =
bt.Block.new(generateRandomBytes(size)).tryGet()
proc benchmarkRepoStore() =
let store = RepoStore.new(repoDs, metaDs, quotaMaxBytes = 100000000000'nb)
waitFor store.start()
echo "Initializing RepoStore benchmarks..."
# Setup test data
let
testDataLen = 4.MiBs
testBlk = createTestBlock(testDataLen.int)
benchmarkLoops = 800
var
blcks = newSeq[Block]()
proofs = newSeq[CodexProof]()
for i in 0 ..< benchmarkLoops:
var blk = createTestBlock(testDataLen.int)
blcks.add(blk)
let (manifest, tree) = makeManifestAndTree(blcks).tryGet()
let treeCid = tree.rootCid.tryGet()
echo "Manifest blocks", manifest.blocksCount
for i in 0 ..< benchmarkLoops:
let proof = tree.getProof(i).tryGet()
proofs.add(proof)
var i = 0
# Benchmark putBlock
benchmark fmt"put_block_{testDataLen}", benchmarkLoops:
(waitFor store.putBlock(blcks[i])).tryGet()
i += 1
i = 0
benchmark fmt"put_cid_and_proof", benchmarkLoops:
(waitFor store.putCidAndProof(treeCid, i, blcks[i].cid, proofs[i])).tryGet()
i += 1
i = 0
benchmark fmt"get_cid_and_proof", benchmarkLoops:
discard (waitFor store.getCidAndProof(treeCid, i)).tryGet()
i += 1
i = 0
benchmark fmt"has_block_{testDataLen}", benchmarkLoops:
discard (waitFor store.hasBlock(blcks[i].cid)).tryGet()
i += 1
i = 0
benchmark "get_block", benchmarkLoops:
discard (waitFor store.getBlock(blcks[i].cid)).tryGet()
i += 1
i = 0
benchmark "del_block_with_index", benchmarkLoops:
(waitFor store.delBlock(treeCid, i.Natural)).tryGet()
i += 1
for i in 0 ..< benchmarkLoops:
discard waitFor store.putBlock(blcks[i])
i = 0
benchmark "delete_block", benchmarkLoops:
discard waitFor store.delBlock(blcks[i].cid)
i += 1
printBenchMarkSummaries()
when isMainModule:
benchmarkRepoStore()

View File

@ -36,7 +36,7 @@ template benchmark*(name: untyped, count: int, blk: untyped) =
var elapsedStr = ""
for v in runs:
elapsedStr &= ", " & v.formatFloat(format = ffDecimal, precision = 3)
elapsedStr &= ", " & v.formatFloat(format = ffDecimal, precision = 5)
stdout.styledWriteLine(
fgGreen, "CPU Time [", benchmarkName, "] ", "avg(", $count, "): ", elapsedStr, " s"
)
@ -61,9 +61,9 @@ template printBenchMarkSummaries*(
if exportExcel:
let timestamp = now().format("yyyy-MM-dd HH:mm:ss")
var f: File
var isNewFile = not fileExists("repo-benchmars.csv")
var isNewFile = not fileExists(BenchmarkFile)
if f.open("repo-benchmars.csv", fmAppend):
if f.open(BenchmarkFile, fmAppend):
try:
# Write header if new file
if isNewFile:
@ -75,7 +75,7 @@ template printBenchMarkSummaries*(
[
timestamp,
name,
data.avgTimeSec.formatFloat(format = ffDecimal, precision = 3),
data.avgTimeSec.formatFloat(format = ffDecimal, precision = 5),
$data.count,
].join(",")
)