1 个不稳定版本
0.1.0 | 2023年2月13日 |
---|
#181 在 数据库实现
330KB
7.5K SLoC
alex-db-server
提供 REST API 通信的数据库服务器应用程序。
开发模式运行
cd alex-db-server/
cp .env .env.local
# Edit the '.env.local' file now using your preferred editor.
set -a
source .env.local
set +a
RUST_LOG=alex-db-server=trace,info,debug,tokio=trace,runtime=trace cargo run
Finished dev [unoptimized + debuginfo] target(s) in 1.30s
Running `/home/michal/projects/alex-db/target/debug/alex-db-server`
2023-02-09T14:21:02.746448Z INFO alex_db_server::config: data_dir = Some("/home/michal/data/")
2023-02-09T14:21:02.746504Z INFO alex_db_server::config: enable_security_api_keys = true
2023-02-09T14:21:02.746530Z INFO alex_db_server::config: port = 10240
2023-02-09T14:21:02.746555Z INFO alex_db_server::config: save_triggered_after_ms = 27000
2023-02-09T14:21:02.746575Z INFO alex_db_server::config: save_triggered_by_threshold = 4
2023-02-09T14:21:02.746599Z INFO alex_db_server::config: sleep_time_between_gc_ms = 900
2023-02-09T14:21:02.746622Z INFO alex_db_server::config: sleep_time_between_saves_ms = 9000
2023-02-09T14:21:02.749852Z INFO alex_db_server::app: initial api key created: Some(63545360-301e-482f-93fc-84e6d11d8aee)
2023-02-09T14:21:02.760592Z INFO alex_db_server: listening on 0.0.0.0:10240
示例请求
通过在您的网络浏览器中导航到 https://127.0.0.1:10240/swagger-ui/ 来访问 API 文档。
在此示例中,请将 '63545360-301e-482f-93fc-84e6d11d8aee' 替换为您的 '初始 API 密钥'。
数据库统计信息
执行以下命令
curl --location --request GET 'https://127.0.0.1:10240/stats' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
并将收到结果
{"reads":0,"requests":0,"saved_at":"2023-02-09T14:26:00.865051741Z","saved_writes":0,"writes":0}
创建
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test1-key",
"value": "test1-value"
}'
并将收到结果
{"key":"test1-key","value":"test1-value"}
列出
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test2-key",
"value": true
}'
curl --location --request GET 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
并将收到结果
[{"key":"test1-key","value":"test1-value"},{"key":"test2-key","value":true}]
您可以使用额外的参数进行排序和分页。
- 方向
- 升序
- 降序
- 排序
- created_at
- delete_at
- key
- updated_at
- page - 页码
- limit - 每页限制
执行以下命令
curl --location --request GET 'https://127.0.0.1:10240/values?sort=created_at&direction=asc&page=1&limit=1' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
并将收到结果
[{"key":"test1-key","value":"test1-value"}]
读取
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test3-key",
"value": 10
}'
curl --location --request GET 'https://127.0.0.1:10240/values/test3-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
并将收到结果
{"key":"test3-key","value":10}
更新
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test4-key",
"value": ["test4-value"],
"ttl": 120
}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test4-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"value": "test4-value-updated",
"ttl": 200
}'
并将收到结果
{"key":"test4-key","value":"test4-value-updated"}
删除
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test5-key",
"value": ["test5-value", true, 10]
}'
curl --location --request DELETE 'https://127.0.0.1:10240/values/test5-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
追加
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test6-key",
"value": ["test6-value"]
}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test6-key/append' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"append": ["test6-value-appended"]
}'
并将收到结果
{"key":"test6-key","value":["test6-value","test6-value-appended"]}
预追加
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test7-key",
"value": ["test7-value"]
}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test7-key/prepend' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"prepend": ["test7-value-prepended"]
}'
并将收到结果
{"key":"test7-key","value":["test7-value-prepended","test7-value"]}
递增
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test8-key",
"value": 1000
}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test8-key/increment' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test8-key/increment' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"increment": 10
}'
并将收到结果
{"key":"test8-key","value":1011}
递减
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test9-key",
"value": 5000
}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test9-key/decrement' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test9-key/decrement' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"decrement": 10
}'
并将收到结果
{"key":"test9-key","value":4989}
弹出前
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test10-key",
"value": ["test10-value1", "test10-value2", "test10-value3", true, false, true, 10, 11, 12]
}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test10-key/pop-front' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test10-key/pop-front' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"pop_front": 3
}'
并将收到结果
["test10-value2","test10-value3",true]
弹出后
执行以下命令
curl --location --request POST 'https://127.0.0.1:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"key": "test11-key",
"value": ["test11-value1", "test11-value2", "test11-value3", true, false, true, 10, 11, 12, ["test11-a-value1", "test11-a-value2", "test11-a-value3"], ["test11-b-value1", "test11-b-value2", "test11-b-value3"], ["test11-c-value1", "test11-c-value2", "test11-c-value3"]]
}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test11-key/pop-back' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'
curl --location --request PUT 'https://127.0.0.1:10240/values/test11-key/pop-back' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
"pop_back": 3
}'
并将收到结果
[["test11-b-value1","test11-b-value2","test11-b-value3"],["test11-a-value1","test11-a-value2","test11-a-value3"],12]
性能
目前,服务器在其 API 端点上的性能令人满意。
执行以下命令
curl --location --request GET 'https://127.0.0.1:10240/values/test3-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
-w ' Total: %{time_total}s\n'
并将收到结果
{"key":"test3-key","value":10} Total: 0.001278s
并且
{"key":"test3-key","value":10} Total: 0.000617s
当您以发布模式运行项目时
cargo run --release
根据我们在 Ubuntu 22.04 系统上的评估,API 端点的响应时间在开发模式下略高于 1ms,在生产模式下低于 1ms。
执行以下命令
ab -c 16 -n 100000 -H "X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee" https://127.0.0.1:10240/values/test3-key
并将收到结果
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: localhost
Server Port: 10240
Document Path: /values/test3-key
Document Length: 30 bytes
Concurrency Level: 16
Time taken for tests: 12.560 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 13800000 bytes
HTML transferred: 3000000 bytes
Requests per second: 7961.69 [#/sec] (mean)
Time per request: 2.010 [ms] (mean)
Time per request: 0.126 [ms] (mean, across all concurrent requests)
Transfer rate: 1072.96 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 14
Processing: 0 2 0.6 2 17
Waiting: 0 2 0.6 2 17
Total: 0 2 0.6 2 17
Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 2
95% 2
98% 2
99% 3
100% 17 (longest request)
这表明服务器在测试机器上能够每秒处理超过 7,900 个请求。
尚未测量内部数据库的性能。上述数字反映了通过 HTTP 协议访问数据库时的性能。
请注意,与 HTTP 服务器相关联存在开销。服务器必须处理 HTTP 请求,查询数据库,序列化数据,并发送响应。
依赖关系
~18–31MB
~455K SLoC