2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
13 module IntVal
= struct
16 let description = "Test_IntVal"
19 module Capacity
= struct
23 let expect ~msg
bool =
28 Printexc.(get_callstack
100 |> print_raw_backtrace stderr
);
32 let expect_equals ~name
value expected
=
36 | Some n
-> Printf.sprintf
"(Some '%d')" n
41 "Expected key %s to equal %s, got %s"
47 let test_local_changes
48 (module IntHeap
: SharedMem.Heap
with type value = int and type key
= string)
50 let expect_value ~name expected
=
51 expect_equals ~name
(IntHeap.get name
) expected
;
52 let mem = expected
<> None
in
60 ~msg
:(Printf.sprintf
"Expected key %s to %s a member" name
str)
61 (IntHeap.mem name
= mem)
63 let expect_add key
value =
64 IntHeap.add key
value;
65 expect_value ~name
:key
(Some
value)
67 let expect_remove key
=
68 IntHeap.remove_batch
(IntHeap.KeySet.singleton key
);
69 expect_value ~name
:key None
72 expect_value ~name
:"Filled" (Some
0);
73 expect_value ~name
:"Empty" None
;
75 IntHeap.LocalChanges.push_stack
();
77 expect_value ~name
:"Filled" (Some
0);
78 expect_value ~name
:"Empty" None
;
80 (* For local changes we allow overriding values *)
81 expect_add "Filled" 1;
84 expect_remove "Filled";
85 expect_remove "Empty";
87 expect_add "Filled" 1;
90 IntHeap.LocalChanges.pop_stack
();
91 expect_value ~name
:"Filled" (Some
0);
92 expect_value ~name
:"Empty" None
;
94 IntHeap.LocalChanges.push_stack
();
95 expect_add "Filled" 1;
97 IntHeap.LocalChanges.pop_stack
();
98 expect_value ~name
:"Filled" (Some
0);
99 expect_value ~name
:"Empty" None
;
101 IntHeap.LocalChanges.push_stack
();
102 expect_remove "Filled";
103 expect_remove "Empty";
104 IntHeap.LocalChanges.pop_stack
();
105 expect_value ~name
:"Filled" (Some
0);
106 expect_value ~name
:"Empty" None
;
108 IntHeap.LocalChanges.push_stack
();
109 expect_add "Filled" 0;
110 expect_add "Empty" 2;
111 expect_value ~name
:"Filled" (Some
0);
112 expect_value ~name
:"Empty" (Some
2);
113 IntHeap.LocalChanges.pop_stack
();
114 expect_value ~name
:"Filled" (Some
0);
115 expect_value ~name
:"Empty" None
117 IntHeap.add
"Filled" 0;
120 IntHeap.(remove_batch
@@ KeySet.singleton
"Filled");
121 IntHeap.add
"Empty" 2;
122 IntHeap.LocalChanges.push_stack
();
123 IntHeap.add
"Filled" 0;
124 IntHeap.(remove_batch
@@ KeySet.singleton
"Empty");
126 IntHeap.LocalChanges.pop_stack
()
128 module type HeapWithLocalCache
=
130 SharedMem.HeapWithLocalCache
131 (SharedMem.ImmediateBackend
(SharedMem.NonEvictable
)) (StringKey
)
135 let test_cache_behavior (module IntHeap
: HeapWithLocalCache
) () =
136 let expect_cache_size expected
=
137 let seq_len = Seq.fold_left
(fun x _
-> x
+ 1) 0 in
139 seq_len @@ snd
@@ IntHeap.Cache.get_telemetry_items_and_keys
()
142 ~msg
:(Printf.sprintf
"Expected cache size of %d, got %d" expected
actual)
147 (* Fill the L1 cache. *)
149 IntHeap.add
(Printf.sprintf
"%d" i
) i
;
153 (* Make sure the L1 cache does not grow past capacity - L2 will. *)
154 for i
= 1001 to 2000 do
155 IntHeap.add
(Printf.sprintf
"%d" i
) i
;
156 expect_cache_size (1000 + (i
- 1000))
159 (* Cannot test beyond this point. We don't know how many unique keys are
160 in the combined cache. *)
164 (SharedMem.ImmediateBackend
(SharedMem.NonEvictable
)) (StringKey
)
167 (* We shall not mix compressions, so create 2 separate caches *)
168 module TestWithCacheLz4
=
169 SharedMem.HeapWithLocalCache
170 (SharedMem.ImmediateBackend
(SharedMem.NonEvictable
)) (StringKey
)
173 module TestWithCacheZstd
=
174 SharedMem.HeapWithLocalCache
175 (SharedMem.ImmediateBackend
(SharedMem.NonEvictable
)) (StringKey
)
180 let zstd_compression_with_default_level = 3 in
181 let lz4_compression = 0 in
184 ( "test_local_changes_no_cache",
185 test_local_changes (module TestNoCache
),
187 ( "test_local_changes_with_cache",
188 test_local_changes (module TestWithCacheLz4
),
190 ( "test_local_changes_with_cache_zstd",
191 test_local_changes (module TestWithCacheZstd
),
192 zstd_compression_with_default_level );
193 ( "test_cache_behavior",
194 test_cache_behavior (module TestWithCacheLz4
),
196 ( "test_cache_behavior_zstd",
197 test_cache_behavior (module TestWithCacheZstd
),
198 zstd_compression_with_default_level );
201 let setup_test (name
, test, compression
) =
204 let num_workers = 0 in
209 SharedMem.global_size
= 16;
213 shm_use_sharded_hashtbl
= false;
221 ignore
(handle : SharedMem.handle);
225 List.map
setup_test list
227 let () = Unit_test.run_all
(tests ())