Mercurial > games > semicongine
comparison tests/test_storage.nim @ 989:3e0116b5d2ed
did: undo complicated background storage API, sync is good enough for now
author | sam <sam@basx.dev> |
---|---|
date | Sun, 07 Apr 2024 21:56:43 +0700 |
parents | 46778940c1d7 |
children | 1705e005cdee |
comparison
equal
deleted
inserted
replaced
988:46778940c1d7 | 989:3e0116b5d2ed |
---|---|
1 import std/os | 1 import std/os |
2 import std/strformat | 2 import std/strformat |
3 | 3 |
4 import semicongine | 4 import semicongine |
5 | 5 |
6 #[ | |
7 proc testSimple(storage: StorageType) = | 6 proc testSimple(storage: StorageType) = |
8 const TEST_VALUE = 42 | 7 const TEST_VALUE = 42 |
9 const KEY = "test" | 8 const KEY = "test" |
10 | 9 |
11 # get default | 10 # get default |
12 var future1 = load[int](storage, KEY) | 11 assert load[int](storage, KEY) == default(type(TEST_VALUE)) |
13 assert future1.awaitResult() == default(type(TEST_VALUE)) | |
14 | 12 |
15 # save and load custom | 13 # save and load custom |
16 var future2 = store(storage, KEY, TEST_VALUE) | 14 store(storage, KEY, TEST_VALUE) |
17 future2.awaitStored() | 15 assert load[int](storage, KEY) == TEST_VALUE |
18 future1 = load[int](storage, KEY) | |
19 assert future1.awaitResult() == TEST_VALUE | |
20 | |
21 proc testBusyWait(storage: StorageType) = | |
22 const TEST_VALUE = "43" | |
23 const KEY = "test2" | |
24 | |
25 # get default | |
26 var future1 = load[string](storage, KEY) | |
27 while not future1.hasResult(): | |
28 sleep(1) | |
29 assert future1.getResult() == default(type(TEST_VALUE)) | |
30 | |
31 # save and load custom | |
32 var future2 = store(storage, KEY, TEST_VALUE) | |
33 while not future2.isStored(): | |
34 sleep(1) | |
35 future1 = load[string](storage, KEY) | |
36 while not future1.hasResult(): | |
37 sleep(1) | |
38 assert future1.awaitResult() == TEST_VALUE | |
39 | 16 |
40 proc stressTest(storage: StorageType) = | 17 proc stressTest(storage: StorageType) = |
41 for i in 1 .. 10000: | 18 for i in 1 .. 10000: |
42 let key = &"key-{i}" | 19 let key = &"key-{i}" |
43 var p = store(storage, key, i) | 20 store(storage, key, i) |
44 p.awaitStored() | 21 assert load[int](storage, key) == i |
45 var p1 = load[int](storage, key) | |
46 assert p1.awaitResult() == i | |
47 ]# | |
48 | 22 |
49 proc concurrentStressTest(storage: StorageType) = | |
50 var storeFutures: seq[StoreFuture[int]] | |
51 | |
52 for i in 1 .. 10000: | |
53 let key = &"key-{i}" | |
54 echo key | |
55 store() | |
56 # storeFutures.add store(storage, key, i) | |
57 | |
58 for i in 1 .. 10000: | |
59 echo i | |
60 let key = &"key-{i}" | |
61 storeFutures[i - 1].awaitStored() | |
62 var p1 = load[int](storage, key) | |
63 assert p1.awaitResult() == i | |
64 | |
65 #[ | |
66 proc main() = | 23 proc main() = |
67 SystemStorage.purge() | 24 SystemStorage.purge() |
68 echo "SystemStorage: Testing simple store/load" | 25 echo "SystemStorage: Testing simple store/load" |
69 SystemStorage.testSimple() | 26 SystemStorage.testSimple() |
70 echo "SystemStorage: Testing store/load with busy wait" | |
71 SystemStorage.testBusyWait() | |
72 | 27 |
73 UserStorage.purge() | 28 UserStorage.purge() |
74 echo "UserStorage: Testing simple store/load" | 29 echo "UserStorage: Testing simple store/load" |
75 UserStorage.testSimple() | 30 UserStorage.testSimple() |
76 echo "UserStorage: Testing store/load with busy wait" | |
77 UserStorage.testBusyWait() | |
78 | 31 |
79 echo "Stress test with 10'000 saves/loads" | 32 echo "Stress test with 10'000 saves/loads" |
80 SystemStorage.stressTest() | 33 SystemStorage.stressTest() |
81 | 34 |
82 SystemStorage.purge() | 35 SystemStorage.purge() |
83 UserStorage.purge() | 36 UserStorage.purge() |
84 | 37 |
85 # TODO: fails currently, but is likely not too important | |
86 # echo "Stress test with 10'000 saves/loads and a little concurrency" | |
87 # SystemStorage.concurrentStressTest() | |
88 ]# | |
89 | 38 |
90 when isMainModule: | 39 when isMainModule: |
91 echo "Stress test with 10'000 saves/loads and a little concurrency" | 40 main() |
92 SystemStorage.concurrentStressTest() | |
93 # main() |