folly: fix github ci tests on macos arm
[hiphop-php.git] / hphp / hack / test / unit / heap / test_sharedmem.ml
blob15daa37fc3a8221681b2943af634a188eb0436a4
1 (*
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
9 *)
11 type key = Digest.t
13 module IntVal = struct
14 type t = int
16 let description = "Test_IntVal"
17 end
19 module Capacity = struct
20 let capacity = 1000
21 end
23 let expect ~msg bool =
24 if bool then
26 else (
27 print_endline msg;
28 Printexc.(get_callstack 100 |> print_raw_backtrace stderr);
29 exit 1
32 let expect_equals ~name value expected =
33 let str x =
34 match x with
35 | None -> "None"
36 | Some n -> Printf.sprintf "(Some '%d')" n
38 expect
39 ~msg:
40 (Printf.sprintf
41 "Expected key %s to equal %s, got %s"
42 name
43 (str expected)
44 (str value))
45 (value = expected)
47 let test_local_changes
48 (module IntHeap : SharedMem.Heap with type value = int and type key = string)
49 () =
50 let expect_value ~name expected =
51 expect_equals ~name (IntHeap.get name) expected;
52 let mem = expected <> None in
53 let str =
54 if mem then
55 "be"
56 else
57 "not be"
59 expect
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
71 let test () =
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;
82 expect_add "Empty" 2;
84 expect_remove "Filled";
85 expect_remove "Empty";
87 expect_add "Filled" 1;
88 expect_add "Empty" 2;
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;
96 expect_add "Empty" 2;
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;
118 test ();
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");
125 test ();
126 IntHeap.LocalChanges.pop_stack ()
128 module type HeapWithLocalCache =
129 module type of
130 SharedMem.HeapWithLocalCache
131 (SharedMem.ImmediateBackend (SharedMem.NonEvictable)) (StringKey)
132 (IntVal)
133 (Capacity)
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
138 let actual =
139 seq_len @@ snd @@ IntHeap.Cache.get_telemetry_items_and_keys ()
141 expect
142 ~msg:(Printf.sprintf "Expected cache size of %d, got %d" expected actual)
143 (expected = actual)
145 expect_cache_size 0;
147 (* Fill the L1 cache. *)
148 for i = 1 to 1000 do
149 IntHeap.add (Printf.sprintf "%d" i) i;
150 expect_cache_size i
151 done;
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))
157 done
159 (* Cannot test beyond this point. We don't know how many unique keys are
160 in the combined cache. *)
162 module TestNoCache =
163 SharedMem.Heap
164 (SharedMem.ImmediateBackend (SharedMem.NonEvictable)) (StringKey)
165 (IntVal)
167 (* We shall not mix compressions, so create 2 separate caches *)
168 module TestWithCacheLz4 =
169 SharedMem.HeapWithLocalCache
170 (SharedMem.ImmediateBackend (SharedMem.NonEvictable)) (StringKey)
171 (IntVal)
172 (Capacity)
173 module TestWithCacheZstd =
174 SharedMem.HeapWithLocalCache
175 (SharedMem.ImmediateBackend (SharedMem.NonEvictable)) (StringKey)
176 (IntVal)
177 (Capacity)
179 let tests () =
180 let zstd_compression_with_default_level = 3 in
181 let lz4_compression = 0 in
182 let list =
184 ( "test_local_changes_no_cache",
185 test_local_changes (module TestNoCache),
186 lz4_compression );
187 ( "test_local_changes_with_cache",
188 test_local_changes (module TestWithCacheLz4),
189 lz4_compression );
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),
195 lz4_compression );
196 ( "test_cache_behavior_zstd",
197 test_cache_behavior (module TestWithCacheZstd),
198 zstd_compression_with_default_level );
201 let setup_test (name, test, compression) =
202 ( name,
203 fun () ->
204 let num_workers = 0 in
205 let handle =
206 SharedMem.init
207 ~num_workers
209 SharedMem.global_size = 16;
210 heap_size = 409600;
211 hash_table_pow = 12;
212 shm_dirs = [];
213 shm_use_sharded_hashtbl = false;
214 shm_cache_size = -1;
215 shm_min_avail = 0;
216 log_level = 0;
217 sample_rate = 0.0;
218 compression;
221 ignore (handle : SharedMem.handle);
222 test ();
223 true )
225 List.map setup_test list
227 let () = Unit_test.run_all (tests ())