Search code examples
locust

How to package locust file with pyinstaller


In virtual environment I installed locust. I want to create distributable package so any user can run load tests. I used pyinstaller. The exe file is created but when I run it nothing happens.

from locust import HttpUser, TaskSet, task
import orjson as json
 
 
def create_post(self):
    paramsDict = {}
    paramsDict = json.loads('{ ...myjson.. }')
    params = json.dumps(paramsDict) #for orjson
    headers = {'content-type': 'application/json','Accept-Encoding':'gzip'}
    
    response = self.client.post("/",data= params, headers=headers, name = "Post query")
    print(f'response -> {response}')
 
 
class WebsiteUser(HttpUser):
    tasks = [create_post]

I used locust as library as Axel pointed. I build with pyinstaller and tests runs just web page is not displayed

I build with pyinstaller but web did not appear `response -> <Response [200]>
Traceback (most recent call last):
  File "gevent\pywsgi.py", line 999, in handle_one_response
  File "gevent\pywsgi.py", line 945, in run_application
  File "flask\app.py", line 2464, in __call__
  File "flask\app.py", line 2450, in wsgi_app
  File "flask\app.py", line 1867, in handle_exception
  File "flask\_compat.py", line 39, in reraise
  File "flask\app.py", line 2447, in wsgi_app
  File "flask\app.py", line 1952, in full_dispatch_request
  File "flask\app.py", line 1821, in handle_user_exception
  File "flask\_compat.py", line 39, in reraise
  File "flask\app.py", line 1950, in full_dispatch_request
  File "flask\app.py", line 1936, in dispatch_request
  File "locust\web.py", line 419, in wrapper
  File "locust\web.py", line 132, in index
  File "flask\templating.py", line 138, in render_template
  File "jinja2\environment.py", line 930, in get_or_select_template
  File "jinja2\environment.py", line 883, in get_template
  File "jinja2\environment.py", line 857, in _load_template
  File "jinja2\loaders.py", line 115, in load
  File "flask\templating.py", line 60, in get_source
  File "flask\templating.py", line 89, in _get_source_fast
jinja2.exceptions.TemplateNotFound: index.html
2021-01-11T11:29:52Z {'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '61083', 'HTTP_HOST': 'localhost:8089', (hidden keys: 31)} failed with TemplateNot

The template needs to be included in build. Into spec file I added into data section as well as static with java scripts and css

datas=[('<path>\\Lib\\site-packages\\locust\\templates', 'locust\\templates'),('<path>\\Lib\\site-packages\\locust\\static', 'locust\\static')],

With Inno Setup I added also those to folders to be in same folder as executable. And web page is displayed.


Solution

  • All depend on how are you expecting to get your test executed and what are your really packing. Locust have different ways of use. Assuming that you wanna pack your test file as an executable file you may need to include in your file a main entrypoint that use locust as a library

    If you are packing the locust itself and the virtualenv including your test file you may need to specify how to run locust as a command and pass your test file as a command line argument