1
+ import random
2
+
3
+ class Tree :
4
+ def __init__ (self , tree_path_count ):
5
+ self .tree_path_count = tree_path_count
6
+
7
+ def get_random_paths (self , batch_size ):
8
+ if batch_size > self .tree_path_count :
9
+ raise ValueError ("Batch size cannot be greater than the number of paths in the tree" )
10
+ return [random .randint (1 , self .tree_path_count ) for _ in range (batch_size )]
11
+
12
+ def get_unique_buckets_for_random_paths (self , random_paths ):
13
+ unique_buckets = set ()
14
+ for path_id in random_paths :
15
+ leaf_id = self .tree_path_count + path_id - 1
16
+ bucket_id = leaf_id
17
+ while bucket_id > 0 :
18
+ unique_buckets .add (bucket_id )
19
+ bucket_id = bucket_id >> 1
20
+ return unique_buckets
21
+
22
+ EXPERIMETNS = 1000
23
+
24
+ class Simulator :
25
+ def __init__ (self , num_requests , batch_size , tree_path_count ):
26
+ self .num_requests = num_requests
27
+ self .tree = Tree (tree_path_count )
28
+ self .batch_size = batch_size
29
+
30
+ def run_once (self ):
31
+ retrieved_buckets_count = 0
32
+ for i in range (0 , self .num_requests , self .batch_size ):
33
+ random_paths = self .tree .get_random_paths (self .batch_size )
34
+ unique_buckets = self .tree .get_unique_buckets_for_random_paths (random_paths )
35
+ retrieved_buckets_count += len (unique_buckets )
36
+ return retrieved_buckets_count
37
+
38
+ def run (self ):
39
+ total_buckets_count = 0
40
+ for _ in range (EXPERIMETNS ):
41
+ total_buckets_count += self .run_once ()
42
+ print ("Average number of buckets retrieved per request:" , total_buckets_count / (self .num_requests * EXPERIMETNS ))
43
+
44
+
45
+ if __name__ == '__main__' :
46
+ batch_size = 2
47
+ num_requests = 1000
48
+ tree_path_count = 2 ** 20
49
+ sim = Simulator (num_requests , batch_size , tree_path_count )
50
+ sim .run ()
0 commit comments