add sampling instead of getting all segments

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2023-06-06 16:08:45 +02:00
parent 15c45be5d0
commit 2a8c7e78f9
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
1 changed files with 18 additions and 2 deletions

View File

@ -1,5 +1,5 @@
import
std/[options, sequtils],
std/[options, sequtils, random],
asynctest,
bearssl/rand,
chronicles,
@ -66,6 +66,21 @@ proc segmentData(s: int, segmentsize: int) : seq[byte] =
result = newSeq[byte](segmentsize)
result[0] = byte(s mod 256)
proc sample(s: Slice[int], len: int): seq[int] =
# random sample without replacement
# TODO: not the best for small len
assert s.a <= s.b
var all = s.b - s.a + 1
var count = len
var generated = newSeq[bool](all) # Initialized to false.
while count != 0:
let n = rand(s)
if not generated[n - s.a]:
generated[n - s.a] = true
result.add n
dec count
when isMainModule:
proc main() {.async.} =
let
@ -112,7 +127,8 @@ when isMainModule:
let startTime = Moment.now()
var futs = newSeq[Future[DiscResult[seq[byte]]]]()
for s in 0 ..< blocksize:
let sample = sample(0 ..< blocksize, samplesize)
for s in sample:
let fut = nodes[n][0].getValue(segmentIDs[s])
futs.add(fut)