mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-12 02:13:08 +00:00
add store benchmark logic
This commit is contained in:
parent
02833b81e5
commit
7b16d7d15d
105
benchmarks/blockstore/bench_store.nim
Normal file
105
benchmarks/blockstore/bench_store.nim
Normal 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()
|
||||
@ -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(",")
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user