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