From cb84da67edabf092cbcda4102b39bcdc88d71a90 Mon Sep 17 00:00:00 2001 From: chenhq Date: Mon, 20 Nov 2023 16:04:58 +0800 Subject: [PATCH] [Feat] execute [python -m webmeter jmeter_server=on] to start jemter-server file --- setup.py | 2 +- webmeter/core/engine.py | 26 +++++++++++++------ .../apache-jmeter-5.6.2/bin/jmeter.properties | 2 +- webmeter/core/plan.py | 2 +- webmeter/core/sqlhandle/crud.py | 4 +-- webmeter/core/sqlhandle/database.py | 2 +- webmeter/core/sqlhandle/models.py | 2 +- webmeter/core/task.py | 4 +-- webmeter/debug.py | 17 +++++++++--- webmeter/view/api.py | 12 ++++----- webmeter/view/page.py | 2 +- webmeter/web.py | 21 ++++++++++----- 12 files changed, 62 insertions(+), 34 deletions(-) diff --git a/setup.py b/setup.py index 226b739..0421f3a 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setuptools.setup( install_requires=['fastapi','uvicorn', 'requests', 'loguru', 'fire','pyfiglet','psutil', 'pyyaml','python-multipart','sqlalchemy'], - version='1.0.14', + version='1.0.15', python_requires='>=3.10', long_description=long_description, long_description_content_type="text/markdown", diff --git a/webmeter/core/engine.py b/webmeter/core/engine.py index 7050594..c32077d 100644 --- a/webmeter/core/engine.py +++ b/webmeter/core/engine.py @@ -2,9 +2,9 @@ import datetime from loguru import logger from typing import Optional -from core.utils import Common, Platform -from core.sqlhandle import crud -from core.task import TaskBase +from webmeter.core.utils import Common, Platform +from webmeter.core.sqlhandle import crud +from webmeter.core.task import TaskBase class EngineServie(TaskBase): @@ -16,12 +16,25 @@ class EngineServie(TaskBase): 'linux': os.path.join(JMETER_DIR, 'bin', 'jmeter.sh') } + JMETER_SERVER_PATH = { + 'windows': os.path.join(JMETER_DIR, 'bin', 'jmeter-server.bat'), + 'macos': os.path.join(JMETER_DIR, 'bin', 'jmeter-server'), + 'linux': os.path.join(JMETER_DIR, 'bin', 'jmeter-server') + } + @classmethod def check_JavaEnvironment(cls): result = Common.exec_cmd('java -version') if result != 0: logger.error('Please download java (https://www.java.com/)') - raise Exception('No java version found') + raise Exception('No JAVA_HOME found') + return result + + @classmethod + def check_JmeterEnvironment(cls): + result = Common.exec_cmd('jmeter -v') + if result != 0: + raise Exception('No JMETER_HOME found') return result @classmethod @@ -127,10 +140,7 @@ def run(cls, content: dict, model='local') -> int: 'status': 'Done' }) else: - crud.update_task(tasks={ - 'task': task_format, - 'status': 'Error' - }) + crud.update_task(tasks={'task': task_format,'status': 'Error'}) logger.error('remote_host connect failed') else: logger.error('task is failed') diff --git a/webmeter/core/jmeter/apache-jmeter-5.6.2/bin/jmeter.properties b/webmeter/core/jmeter/apache-jmeter-5.6.2/bin/jmeter.properties index 3e17bcc..84f5a89 100644 --- a/webmeter/core/jmeter/apache-jmeter-5.6.2/bin/jmeter.properties +++ b/webmeter/core/jmeter/apache-jmeter-5.6.2/bin/jmeter.properties @@ -265,7 +265,7 @@ gui.quick_9=ViewResultsFullVisualizer #--------------------------------------------------------------------------- # Remote Hosts - comma delimited -# remote_hosts={host}:1099,{host}:1099 +#remote_hosts={host1}:1099,{host2}:1099 # RMI port to be used by the server (must start rmiregistry with same port) #server_port=1099 diff --git a/webmeter/core/plan.py b/webmeter/core/plan.py index 6b87fc9..f3b749b 100644 --- a/webmeter/core/plan.py +++ b/webmeter/core/plan.py @@ -2,7 +2,7 @@ import shutil from loguru import logger from fastapi import UploadFile -from core.utils import Common, JMX +from webmeter.core.utils import Common, JMX class Base(object): diff --git a/webmeter/core/sqlhandle/crud.py b/webmeter/core/sqlhandle/crud.py index 1f5d5bd..802469e 100644 --- a/webmeter/core/sqlhandle/crud.py +++ b/webmeter/core/sqlhandle/crud.py @@ -1,8 +1,8 @@ from typing import Optional from loguru import logger import os, shutil, datetime -from core.sqlhandle import models, schemas, database -from core.plan import TestPlan +from webmeter.core.sqlhandle import models, schemas, database +from webmeter.core.plan import TestPlan def create_task(tasks: dict): with database.dbConnect() as session: diff --git a/webmeter/core/sqlhandle/database.py b/webmeter/core/sqlhandle/database.py index bd0c790..f4ad2d0 100644 --- a/webmeter/core/sqlhandle/database.py +++ b/webmeter/core/sqlhandle/database.py @@ -4,7 +4,7 @@ from contextlib import contextmanager from loguru import logger import os -from core.utils import Common +from webmeter.core.utils import Common SQL_DIR = Common.make_dir(os.path.join(os.getcwd(), 'webmeter')) SQLALCHEMY_DATABASE_URL = "sqlite:///{}/app.db".format(SQL_DIR) diff --git a/webmeter/core/sqlhandle/models.py b/webmeter/core/sqlhandle/models.py index 3e363de..ac99600 100644 --- a/webmeter/core/sqlhandle/models.py +++ b/webmeter/core/sqlhandle/models.py @@ -1,5 +1,5 @@ from sqlalchemy import Boolean, Column, Integer, String, DateTime, Float -from core.sqlhandle.database import Base +from webmeter.core.sqlhandle.database import Base class Task(Base): diff --git a/webmeter/core/task.py b/webmeter/core/task.py index 5f6dfeb..dd47629 100644 --- a/webmeter/core/task.py +++ b/webmeter/core/task.py @@ -1,8 +1,8 @@ from loguru import logger import json, os from typing import Optional -from core.utils import Common -from core.sqlhandle import crud +from webmeter.core.utils import Common +from webmeter.core.sqlhandle import crud class TaskBase(object): diff --git a/webmeter/debug.py b/webmeter/debug.py index 5774da2..f11bf86 100644 --- a/webmeter/debug.py +++ b/webmeter/debug.py @@ -1,10 +1,12 @@ import uvicorn from fastapi import FastAPI -from view import page,api -from core.utils import Common from fastapi.staticfiles import StaticFiles import requests import webbrowser +from loguru import logger +from view import page,api +from core.utils import Common +from core.engine import EngineServie app = FastAPI(debug=True) app.include_router(page.router) @@ -25,8 +27,15 @@ def open_url(host: str, port: int) -> None: flag = status(host, port) webbrowser.open('http://{}:{}/plan'.format(host, port), new=2) -def main(host=Common.ip(), port=6006) -> None: - start(host, port) +def main(host=Common.ip(), port=6006, jmeter_server='off') -> None: + if jmeter_server == 'off': + start(host, port) + elif jmeter_server == 'on': + EngineServie.check_JavaEnvironment() + EngineServie.check_JmeterEnvironment() + Common.exec_cmd(f'{EngineServie.JMETER_SERVER_PATH.get(Common.pc_platform())} -Djava.rmi.server.hostname={host}') + else: + logger.error('The value of jmeter_server is invalid.') if __name__ == "__main__": main() diff --git a/webmeter/view/api.py b/webmeter/view/api.py index f6c420c..f73b58a 100644 --- a/webmeter/view/api.py +++ b/webmeter/view/api.py @@ -1,12 +1,12 @@ from loguru import logger from typing import Union from fastapi import APIRouter, UploadFile, File, Form -from core.plan import TestPlan -from core.engine import EngineServie -from core.sqlhandle import crud, models, schemas -from core.sqlhandle.database import engine -from core.task import TaskDetail -from core.utils import Performance +from webmeter.core.plan import TestPlan +from webmeter.core.engine import EngineServie +from webmeter.core.sqlhandle import crud, models, schemas +from webmeter.core.sqlhandle.database import engine +from webmeter.core.task import TaskDetail +from webmeter.core.utils import Performance router = APIRouter() test_plan = TestPlan() models.Base.metadata.create_all(bind=engine) diff --git a/webmeter/view/page.py b/webmeter/view/page.py index 9541db0..5b95d40 100644 --- a/webmeter/view/page.py +++ b/webmeter/view/page.py @@ -4,7 +4,7 @@ from fastapi.responses import RedirectResponse from fastapi import APIRouter import os -from core.utils import Common +from webmeter.core.utils import Common router = APIRouter() diff --git a/webmeter/web.py b/webmeter/web.py index cac4dc0..c86f836 100644 --- a/webmeter/web.py +++ b/webmeter/web.py @@ -5,8 +5,10 @@ import webbrowser import multiprocessing from fastapi.staticfiles import StaticFiles +from loguru import logger from webmeter.view import page,api from webmeter.core.utils import Common +from webmeter.core.engine import EngineServie app = FastAPI(debug=False) app.include_router(page.router) @@ -29,9 +31,16 @@ def open_url(host: str, port: int) -> None: flag = status(host, port) webbrowser.open('http://{}:{}/plan'.format(host, port), new=2) -def main(host=Common.ip(), port=6006) -> None: - pool = multiprocessing.Pool(processes=2) - pool.apply_async(start, (host, port)) - pool.apply_async(open_url, (host, port)) - pool.close() - pool.join() \ No newline at end of file +def main(host=Common.ip(), port=6006, jmeter_server='off') -> None: + if jmeter_server == 'off': + pool = multiprocessing.Pool(processes=2) + pool.apply_async(start, (host, port)) + pool.apply_async(open_url, (host, port)) + pool.close() + pool.join() + elif jmeter_server == 'on': + EngineServie.check_JavaEnvironment() + EngineServie.check_JmeterEnvironment() + Common.exec_cmd(f'{EngineServie.JMETER_SERVER_PATH.get(Common.pc_platform())} -Djava.rmi.server.hostname={host}') + else: + logger.error('The value of jmeter_server is invalid.') \ No newline at end of file