forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaches.fs
More file actions
263 lines (216 loc) · 9.44 KB
/
Copy pathCaches.fs
File metadata and controls
263 lines (216 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
module internal CompilerService.Caches
open FSharp.Compiler.Caches
open Xunit
open FSharp.Test.Assert
open System.Threading.Tasks
open Microsoft.FSharp.Collections
open System
#if DEBUG
let shouldNeverTimeout = 15_000
#else
// accomodate unpredictable CI thread scheduling
let shouldNeverTimeout = 200_000
#endif
let defaultStructural() = CacheOptions.getDefault HashIdentity.Structural
// Metrics assertions below read absolute per-name totals via CacheMetrics.getTotalsByName. Those totals
// are aggregated process-globally while a CacheMetrics.ListenToAll() listener is running. This works
// because each test uses a unique cache name and this module is the only ListenToAll caller in the
// assembly, so nothing else increments those names. A second concurrently-active listener would
// double-count every measurement, so keep it that way.
[<Fact>]
let ``Create and dispose many`` () =
let caches =
[ for _ in 1 .. 100 do
new Cache<string, int>(defaultStructural(), name = "Create and dispose many") :> IDisposable ]
for c in caches do
c.Dispose()
[<Fact>]
let ``Basic add and retrieve`` () =
let name = "Basic_add_and_retrieve"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<string, int>(defaultStructural(), name = name)
cache.TryAdd("key1", 1) |> shouldBeTrue
cache.TryAdd("key2", 2) |> shouldBeTrue
let mutable value = 0
cache.TryGetValue("key1", &value) |> shouldBeTrue
value |> shouldEqual 1
cache.TryGetValue("key2", &value) |> shouldBeTrue
value |> shouldEqual 2
cache.TryGetValue("key3", &value) |> shouldBeFalse
// Metrics assertions
let totals = CacheMetrics.getTotalsByName name
totals.["adds"] |> shouldEqual 2L
[<Fact>]
let ``Eviction of least recently used`` () =
let name = "Eviction_of_least_recently_used"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<string, int>({ defaultStructural() with TotalCapacity = 2; HeadroomPercentage = 0 }, name = name)
cache.TryAdd("key1", 1) |> shouldBeTrue
cache.TryAdd("key2", 2) |> shouldBeTrue
let mutable value = 0
cache.TryGetValue("key1", &value) |> shouldBeTrue
let evictionResult = TaskCompletionSource()
cache.Evicted.Add evictionResult.SetResult
cache.EvictionFailed.Add (fun _ -> evictionResult.SetException(Xunit.Sdk.FailException.ForFailure "eviction failed"))
cache.TryAdd("key3", 3) |> shouldBeTrue
evictionResult.Task.Wait shouldNeverTimeout |> shouldBeTrue
cache.TryGetValue("key2", &value) |> shouldBeFalse
cache.TryGetValue("key1", &value) |> shouldBeTrue
value |> shouldEqual 1
cache.TryGetValue("key3", &value) |> shouldBeTrue
value |> shouldEqual 3
// Metrics assertions
let totals = CacheMetrics.getTotalsByName name
totals.["adds"] |> shouldEqual 3L
[<Fact>]
let ``Stress test evictions`` () =
let cacheSize = 100
let iterations = 10_000
let name = "Stress test evictions"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<string, int>({ defaultStructural() with TotalCapacity = cacheSize; HeadroomPercentage = 0 }, name = name)
let evictionsCompleted = new TaskCompletionSource<unit>()
let expectedEvictions = iterations - cacheSize
cache.Evicted.Add <| fun () ->
if (CacheMetrics.getTotalsByName name).["evictions"] = expectedEvictions then
evictionsCompleted.SetResult()
cache.EvictionFailed.Add <| fun _ ->
evictionsCompleted.SetException(Xunit.Sdk.FailException.ForFailure "eviction failed")
for i in 1 .. iterations do
cache.TryAdd($"key{i}", i) |> shouldBeTrue
evictionsCompleted.Task.Wait shouldNeverTimeout |> shouldBeTrue
let mutable value = 0
cache.TryGetValue($"key{iterations - cacheSize}", &value) |> shouldBeFalse
cache.TryGetValue($"key{iterations - cacheSize + 1}", &value) |> shouldBeTrue
value |> shouldEqual (iterations - cacheSize + 1)
cache.TryGetValue($"key{iterations}", &value) |> shouldBeTrue
value |> shouldEqual iterations
// Metrics assertions
let totals = CacheMetrics.getTotalsByName name
totals.["adds"] |> shouldEqual (int64 iterations)
[<Fact>]
let ``Metrics can be retrieved`` () =
let name = "test_metrics"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<string, int>({ defaultStructural() with TotalCapacity = 2; HeadroomPercentage = 0 }, name = name)
cache.TryAdd("key1", 1) |> shouldBeTrue
cache.TryAdd("key2", 2) |> shouldBeTrue
let mutable value = 0
cache.TryGetValue("key1", &value) |> shouldBeTrue
let evictionCompleted = TaskCompletionSource()
cache.Evicted.Add(fun _ -> evictionCompleted.SetResult())
cache.EvictionFailed.Add(fun _ -> evictionCompleted.SetException(Xunit.Sdk.FailException.ForFailure "eviction failed"))
cache.TryAdd("key3", 3) |> shouldBeTrue
evictionCompleted.Task.Wait shouldNeverTimeout |> shouldBeTrue
let totals = CacheMetrics.getTotalsByName name
CacheMetrics.getRatioByName name |> shouldEqual 1.0
totals.["evictions"] |> shouldEqual 1L
totals.["adds"] |> shouldEqual 3L
[<Fact>]
let ``GetOrAdd basic usage`` () =
let cacheName = "GetOrAdd_basic_usage"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<string, int>(defaultStructural(), name = cacheName)
let mutable factoryCalls = 0
let factory k = factoryCalls <- factoryCalls + 1; String.length k
let v1 = cache.GetOrAdd("abc", factory)
v1 |> shouldEqual 3
let v2 = cache.GetOrAdd("abc", factory)
v2 |> shouldEqual 3
factoryCalls |> shouldEqual 1
let v3 = cache.GetOrAdd("defg", factory)
v3 |> shouldEqual 4
factoryCalls |> shouldEqual 2
// Metrics assertions
let totals = CacheMetrics.getTotalsByName cacheName
totals.["hits"] |> shouldEqual 1L
totals.["misses"] |> shouldEqual 2L
CacheMetrics.getRatioByName cacheName |> shouldEqual (1.0/3.0)
totals.["adds"] |> shouldEqual 2L
[<Fact>]
let ``AddOrUpdate basic usage`` () =
let cacheName = "AddOrUpdate_basic_usage"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<string, int>(defaultStructural(), name = cacheName)
cache.AddOrUpdate("x", 1)
let mutable value = 0
cache.TryGetValue("x", &value) |> shouldBeTrue
value |> shouldEqual 1
cache.AddOrUpdate("x", 42)
cache.TryGetValue("x", &value) |> shouldBeTrue
value |> shouldEqual 42
cache.AddOrUpdate("y", 99)
cache.TryGetValue("y", &value) |> shouldBeTrue
value |> shouldEqual 99
// Metrics assertions
let totals = CacheMetrics.getTotalsByName cacheName
totals.["hits"] |> shouldEqual 3L // 3 cache hits
totals.["misses"] |> shouldEqual 0L // 0 cache misses
CacheMetrics.getRatioByName cacheName |> shouldEqual 1.0
totals.["adds"] |> shouldEqual 2L // "x" and "y" added
totals.["updates"] |> shouldEqual 1L // "x" updated
type BoxedKey = BoxedKey of int * int
[<Fact>]
let ``GetOrAdd with reference identity`` () =
let cacheName = "GetOrAdd_with_Reference"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<BoxedKey, int>(CacheOptions.getReferenceIdentity(), cacheName)
let t1 = BoxedKey (1, 2)
let t2 = BoxedKey (1, 2)
let t3 = BoxedKey (1, 2)
let mutable createdCOunter = 0
let factory _ =
createdCOunter <- createdCOunter + 1
createdCOunter
let v1 = cache.GetOrAdd(t1, factory) // miss
let v2 = cache.GetOrAdd(t2, factory) // miss (different reference)
v1 |> shouldEqual 1
v2 |> shouldEqual 2
// Reference comparer: t1 and t2 are different keys
t1 = t2 |> shouldBeTrue // value equality
obj.ReferenceEquals(t1, t2) |> shouldBeFalse // reference inequality
let mutable v1' = 0
let mutable v2' = 0
let mutable v3' = 0
cache.TryGetValue(t1, &v1') |> shouldBeTrue // hit
cache.TryGetValue(t2, &v2') |> shouldBeTrue // hit
cache.TryGetValue(t3, &v3') |> shouldBeFalse // miss
let v1'' = cache.GetOrAdd(t1, factory) // hit
let v2'' = cache.GetOrAdd(t2, factory) // hit
v1'' |> shouldEqual v1'
v2'' |> shouldEqual v2'
// Metrics assertions
let totals = CacheMetrics.getTotalsByName cacheName
totals.["hits"] |> shouldEqual 4L
totals.["misses"] |> shouldEqual 3L
CacheMetrics.getRatioByName cacheName |> shouldEqual (4.0 / 7.0)
totals.["adds"] |> shouldEqual 2L
[<Fact>]
let ``AddOrUpdate with reference identity`` () =
let cacheName = "AddOrUpdate_with_Reference"
use _ = CacheMetrics.ListenToAll()
use cache = new Cache<obj, int>(CacheOptions.getReferenceIdentity(), name = cacheName)
let t1 = box (3, 4)
let t2 = box (3, 4)
cache.AddOrUpdate(t1, 7)
let mutable value1 = 0
cache.TryGetValue(t1, &value1) |> shouldBeTrue
value1 |> shouldEqual 7
cache.AddOrUpdate(t2, 8)
let mutable value2 = 0
cache.TryGetValue(t2, &value2) |> shouldBeTrue
value2 |> shouldEqual 8
// t1 and t2 are different keys under reference equality
obj.ReferenceEquals(t1, t2) |> shouldBeFalse
// Now update t1 and check value and metrics
cache.AddOrUpdate(t1, 9)
let mutable value1Updated = 0
cache.TryGetValue(t1, &value1Updated) |> shouldBeTrue
value1Updated |> shouldEqual 9
// Metrics assertions
let totals = CacheMetrics.getTotalsByName cacheName
totals.["hits"] |> shouldEqual 3L // 3 cache hits
totals.["misses"] |> shouldEqual 0L // 0 cache misses
CacheMetrics.getRatioByName cacheName |> shouldEqual 1.0
totals.["adds"] |> shouldEqual 2L // t1 and t2 added
totals.["updates"] |> shouldEqual 1L // t1 updated once