annotate tests/test_storage.nim @ 1097:bc3efccc2bf4

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