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