diff --git a/README.md b/README.md
index d8f5089..76438ef 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,15 @@
## 部署
> 由于对`vercel`不熟悉, vercel的配置文件搞不明白所以暂时没有vercel的配置文件, 我也试了很多方法去部署但是要么是500要么是400
+> 在部署之前你需要先安装`python3.9.x`以上的版本
+### 部署到本地服务器
+ ```shell
+ $ git clone https://github.com/MarkusJoe/RequestCounter.git
+ $ cd RequestCounter
+ $ pip3 install -r requirements.txt
+ $ python3 app.py
+ ```
+
## 调用须知
- API支持`GET` 和 `POST` 方法请求
- 最大可以计数`10`位数, 超过则重置
diff --git a/bin/tests/stress_test.py b/bin/tests/stress_test.py
index a0574a7..a54d5d6 100644
--- a/bin/tests/stress_test.py
+++ b/bin/tests/stress_test.py
@@ -7,7 +7,12 @@
import requests
+import threading
-for i in range(100):
- print(requests.get('http://127.0.0.1:5000/API?name=89999&length=10&theme=t2').elapsed.microseconds)
\ No newline at end of file
+def thread_():
+ print(requests.get('http://127.0.0.1:5000/get?name=89999&length=10&theme=t2').elapsed.microseconds)
+
+
+for i in range(1000000):
+ threading.Thread(target=thread_).start()
diff --git a/db/data.db b/db/data.db
index 3bd6aaa..44cab3c 100644
Binary files a/db/data.db and b/db/data.db differ
diff --git a/db/db.py b/db/db.py
index 3f0c5bd..4ca1154 100644
--- a/db/db.py
+++ b/db/db.py
@@ -18,8 +18,14 @@ def insert_data(name: str) -> None:
:param name:
:return:
"""
- cursor.execute('insert into ReqCount values(?, ?)', (name, 1))
- conn.commit()
+ conn = sqlite3.connect('./db/data.db', check_same_thread=False)
+ cursor = conn.cursor()
+ try:
+ cursor.execute('insert into ReqCount values(?, ?)', (name, 1))
+ conn.commit()
+ finally:
+ cursor.close()
+ conn.close()
def fetch_data(name: str) -> int:
@@ -28,26 +34,30 @@ def fetch_data(name: str) -> int:
:param name:
:return:
"""
- cursor.execute('select * from ReqCount')
- conn.commit()
- data = cursor.fetchall()
+ conn = sqlite3.connect('./db/data.db', check_same_thread=False)
+ cursor = conn.cursor()
+ try:
+ cursor.execute('select * from ReqCount')
+ conn.commit()
+ data = cursor.fetchall()
- # 将返回的元组转换为字典
- temp_dict = {}
- for k, v in data:
- temp_dict.setdefault(k, []).append(v)
- # 上方代码将元组转换为字典后字典的值会变成只有一个值的列表, 用下面两行代码去除
- for i, c in zip(temp_dict.keys(), temp_dict.values()):
- temp_dict[i] = c[0]
+ temp_dict = {}
+ for k, v in data:
+ temp_dict.setdefault(k, []).append(v)
+ for i, c in zip(temp_dict.keys(), temp_dict.values()):
+ temp_dict[i] = c[0]
- if name in temp_dict.keys():
- count = temp_dict[name] # 获取原数字 为 整型
- update_data(name, count)
- return count
- else:
- # 新建用户数据
- insert_data(name)
- return 0
+ if name in temp_dict.keys():
+ count = temp_dict[name] # 获取原数字 为 整型
+ update_data(name, count)
+ return count
+ else:
+ # 新建用户数据
+ insert_data(name)
+ return 0
+ finally:
+ cursor.close()
+ conn.close()
def update_data(name: str, times: int) -> None:
@@ -57,9 +67,15 @@ def update_data(name: str, times: int) -> None:
:param times:
:return:
"""
- times += 1
- cursor.execute('update ReqCount set times=? where name=?', (times, name))
- conn.commit()
+ conn = sqlite3.connect('./db/data.db', check_same_thread=False)
+ cursor = conn.cursor()
+ try:
+ times += 1
+ cursor.execute('update ReqCount set times=? where name=?', (times, name))
+ conn.commit()
+ finally:
+ cursor.close()
+ conn.close()
def fetch_table() -> list:
@@ -67,15 +83,15 @@ def fetch_table() -> list:
返回已有主题列表
:return:
"""
- lst = []
conn_temp = sqlite3.connect('./bin/assets/theme.db', check_same_thread=False)
cursor_temp = conn_temp.cursor()
- cursor_temp.execute("select * from sqlite_master where type='table'")
- for i in cursor_temp.fetchall():
- lst.append(i[1])
- return lst
+ try:
+ lst = []
+ cursor_temp.execute("select * from sqlite_master where type='table'")
+ for i in cursor_temp.fetchall():
+ lst.append(i[1])
+ return lst
+ finally:
+ cursor_temp.close()
+ conn_temp.close()
-
-if __name__ != '__main__':
- conn = sqlite3.connect('./db/data.db', check_same_thread=False)
- cursor = conn.cursor()
diff --git a/docs/README.md b/docs/README.md
index 9ee833f..f47b4fb 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -37,119 +37,20 @@
测试用代码如下
```python
import requests
+import threading
-for i in range(100):
- res = requests.get('http://127.0.0.1/API?name=2&length=10&theme=t2').elapsed.microseconds
- print(res) # 返回响应速度 单位: 微秒
+
+def thread_():
+ print(requests.get('http://127.0.0.1:5000/get?name=89999&length=10&theme=t2').elapsed.microseconds)
+
+
+for i in range(1000000):
+ threading.Thread(target=thread_).start()
```
->在`AMD Athlon(tm) X4 830 Quad Core Processor 2.99 GHz` `4G` 内存的电脑上运行项目
-> 在本机上使用`Requests`库`GET`方法请求`100`次 (名称为`2`长度为`10`主题为`t2`)
-> 使用`elapsed.microseconds`属性 获取响应速度返回的值如下
-
-单位: 微秒
-参考: 1秒(second)(s) = 1000毫秒(millisecond)(ms) = 1000000微秒(microseconds)(us)
-61764
-22125
-25059
-16524
-13396
-12787
-20579
-18987
-25500
-14910
-11303
-11787
-16949
-17094
-15264
-10934
-10526
-11943
-16967
-17205
-12887
-11386
-11547
-17743
-18625
-17208
-14763
-12858
-12708
-18598
-16879
-16216
-14655
-12089
-11588
-17287
-20115
-14403
-12857
-11596
-15006
-18246
-18013
-13623
-10673
-12064
-17838
-16651
-16483
-13522
-13231
-15624
-11043
-10765
-16317
-17200
-13250
-11230
-11533
-16727
-16781
-16253
-14658
-13762
-13194
-17700
-17933
-15686
-12051
-10904
-17163
-16591
-16464
-15271
-12405
-17173
-26737
-15964
-12363
-12653
-12893
-17524
-20084
-14165
-12037
-13427
-17245
-18652
-18438
-14811
-14233
-14555
-17998
-16949
-16445
-12120
-11695
-14257
-17557
-16587
-
-
+>在`AMD Athlon(tm) X4 830 Quad Core Processor 2.99 GHz` `4G` 内存的电脑上运行项目
+> 在本机上使用`Requests`库`GET`方法请求`1000000`次 (名称为`2`长度为`10`主题为`t2`)
+> 使用`elapsed.microseconds`属性 获取响应速度在 `0.03` ~ `0.8` 秒左右
+
# 关于
## 开源