From 1a2d36ce4c0ee22e0b2650e2aec11b5efa856e04 Mon Sep 17 00:00:00 2001
From: xiaoxialulu <546464268@qq.com>
Date: Fri, 24 May 2024 14:16:06 +0800
Subject: [PATCH] "Add performance testing function and update atomic.py"
---
backend/.idea/workspace.xml | 49 +++++++-------
backend/api/tests/atomic.py | 103 ++++++++++++++++++------------
backend/unitrunner/engine/bomb.py | 42 ++++++++++++
3 files changed, 126 insertions(+), 68 deletions(-)
diff --git a/backend/.idea/workspace.xml b/backend/.idea/workspace.xml
index c3c2f1f..b03aa52 100644
--- a/backend/.idea/workspace.xml
+++ b/backend/.idea/workspace.xml
@@ -4,13 +4,10 @@
-
-
+
-
-
-
+
@@ -489,15 +486,7 @@
-
-
-
-
- 1713768814349
-
-
-
- 1713768814349
+
@@ -883,7 +872,15 @@
1716282123949
-
+
+
+ 1716530743094
+
+
+
+ 1716530743094
+
+
@@ -913,15 +910,6 @@
-
-
-
-
-
-
-
-
-
@@ -938,7 +926,16 @@
-
+
+
+
+
+
+
+
+
+
+
@@ -1000,6 +997,6 @@
-
+
\ No newline at end of file
diff --git a/backend/api/tests/atomic.py b/backend/api/tests/atomic.py
index b70e0ba..c7f1eed 100644
--- a/backend/api/tests/atomic.py
+++ b/backend/api/tests/atomic.py
@@ -2,12 +2,68 @@
import time
import atomic_bomb_engine
from atomic_bomb_engine import server
-from unitrunner.engine.bomb import AtomicBombEngine
+from unitrunner.engine.bomb import AtomicBombEngine, performance
-@server.ui(port=8880)
-async def main():
- engine = AtomicBombEngine(
+# @server.ui(port=8880)
+# async def main():
+# engine = AtomicBombEngine(
+# test_duration_secs=10,
+# concurrent_requests=1,
+# timeout_secs=15,
+# cookie_store_enable=True,
+# verbose=False,
+# increase_step=1,
+# increase_interval=3
+# )
+# validators = [{
+# "jsonpath": "$.msg",
+# "reference_object": "操作成功"
+# }]
+# runner = engine.run(data={
+# "name": "test-1",
+# "url": "https://www.baidu.com/",
+# "method": "POST",
+# "form_data": {"name": "{{api-test-msg-1}}", "number": "{{api-test-num}}"},
+# "validators": validators,
+# })
+# # runner = atomic_bomb_engine.BatchRunner()
+# # step = getattr(atomic_bomb_engine, 'endpoint')
+# # runner.run(
+# # test_duration_secs=10,
+# # concurrent_requests=1,
+# # step_option=atomic_bomb_engine.step_option(increase_step=1, increase_interval=3),
+# # timeout_secs=15,
+# # cookie_store_enable=True,
+# # verbose=False,
+# # api_endpoints=[
+# # step(
+# # name="test-1",
+# # url="https://www.baidu.com/",
+# # method="POST",
+# # form_data={"name": "{{api-test-msg-1}}", "number": "{{api-test-num}}"},
+# # weight=100,
+# # assert_options=[
+# # atomic_bomb_engine.assert_option(jsonpath="$.msg", reference_object="操作成功"),
+# # ],
+# # ),
+# # ])
+# return runner
+
+
+if __name__ == '__main__':
+ data = {
+ "name": "test-1",
+ "url": "https://www.baidu.com/",
+ "method": "POST",
+ "form_data": {"name": "{{api-test-msg-1}}", "number": "{{api-test-num}}"},
+ "validators": [{
+ "jsonpath": "$.msg",
+ "reference_object": "操作成功"
+ }],
+ }
+ results = asyncio.run(performance(
+ test_data=data,
test_duration_secs=10,
concurrent_requests=1,
timeout_secs=15,
@@ -15,44 +71,7 @@ async def main():
verbose=False,
increase_step=1,
increase_interval=3
- )
- validators = [{
- "jsonpath": "$.msg",
- "reference_object": "操作成功"
- }]
- runner = engine.run(data={
- "name": "test-1",
- "url": "https://www.baidu.com/",
- "method": "POST",
- "form_data": {"name": "{{api-test-msg-1}}", "number": "{{api-test-num}}"},
- "validators": validators,
- })
- # runner = atomic_bomb_engine.BatchRunner()
- # step = getattr(atomic_bomb_engine, 'endpoint')
- # runner.run(
- # test_duration_secs=10,
- # concurrent_requests=1,
- # step_option=atomic_bomb_engine.step_option(increase_step=1, increase_interval=3),
- # timeout_secs=15,
- # cookie_store_enable=True,
- # verbose=False,
- # api_endpoints=[
- # step(
- # name="test-1",
- # url="https://www.baidu.com/",
- # method="POST",
- # form_data={"name": "{{api-test-msg-1}}", "number": "{{api-test-num}}"},
- # weight=100,
- # assert_options=[
- # atomic_bomb_engine.assert_option(jsonpath="$.msg", reference_object="操作成功"),
- # ],
- # ),
- # ])
- return runner
-
-
-if __name__ == '__main__':
- results = asyncio.run(main())
+ ))
a = []
for res in results:
if res.get("should_wait"):
diff --git a/backend/unitrunner/engine/bomb.py b/backend/unitrunner/engine/bomb.py
index 24b718d..41e9900 100644
--- a/backend/unitrunner/engine/bomb.py
+++ b/backend/unitrunner/engine/bomb.py
@@ -167,3 +167,45 @@ def assert_option(validate_check: list) -> List[Any]:
assert_options.append(atomic_bomb_engine.assert_option(jsonpath, reference_object))
return assert_options
+
+
+async def performance(
+ test_data,
+ test_duration_secs=None,
+ concurrent_requests=None,
+ timeout_secs=0,
+ cookie_store_enable=True,
+ verbose=False,
+ increase_step=None,
+ increase_interval=None
+):
+ """
+ Runs a performance test with the provided configuration.
+
+ Args:
+ test_data: Data to be used for the test.
+ test_duration_secs: Optional duration of the test in seconds.
+ concurrent_requests: Optional number of concurrent requests.
+ timeout_secs: Optional timeout in seconds for individual requests.
+ cookie_store_enable: Optional flag to enable cookie storage.
+ verbose: Optional flag to enable verbose logging.
+ increase_step: Optional step size for increasing load (experimental).
+ increase_interval: Optional interval for load increase (experimental).
+
+ Returns:
+ An object representing the test runner (implementation-specific).
+ """
+ try:
+ engine = AtomicBombEngine(
+ test_duration_secs=test_duration_secs,
+ concurrent_requests=concurrent_requests,
+ timeout_secs=timeout_secs,
+ cookie_store_enable=cookie_store_enable,
+ verbose=verbose,
+ increase_step=increase_step,
+ increase_interval=increase_interval,
+ )
+ runner = engine.run(test_data)
+ return runner
+ except (Exception, KeyboardInterrupt):
+ raise Exception("Test runner encountered an exception.")