diff --git a/.gitignore b/.gitignore index fd1de91..857bc3f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,6 @@ __pycache__/ .coverage.* TODO -IDE and Tooling files -.idea/* \ No newline at end of file +# IDE and Tooling files +.idea/* +*~ diff --git a/testproject/Dockerfile.rabbitmq b/testproject/Dockerfile.rabbitmq new file mode 100644 index 0000000..5e5cfb3 --- /dev/null +++ b/testproject/Dockerfile.rabbitmq @@ -0,0 +1,27 @@ +FROM ubuntu:16.04 + +MAINTAINER Artem Malyshev + +# python-dev \ +RUN apt-get update && \ + apt-get install -y \ + git \ + python-setuptools \ + python-pip && \ + pip install -U pip + +# Install asgi_rabbitmq driver and most recent Daphne +RUN pip install \ + git+https://github.com/proofit404/asgi_rabbitmq.git#egg=asgi_rabbitmq \ + git+https://github.com/django/daphne.git@#egg=daphne + +# Clone Channels and install it +RUN git clone https://github.com/django/channels.git /srv/channels/ && \ + cd /srv/channels && \ + git reset --hard origin/master && \ + python setup.py install + +WORKDIR /srv/channels/testproject/ +ENV RABBITMQ_URL=amqp://guest:guest@rabbitmq:5672/%2F + +EXPOSE 80 diff --git a/testproject/Dockerfile b/testproject/Dockerfile.redis similarity index 100% rename from testproject/Dockerfile rename to testproject/Dockerfile.redis diff --git a/testproject/README.rst b/testproject/README.rst index c754700..3ccb737 100644 --- a/testproject/README.rst +++ b/testproject/README.rst @@ -18,13 +18,13 @@ e.g. to create it right in the test directory (assuming python 2 is your system' How to use with Docker: ~~~~~~~~~~~~~~~~~~~~~~~ -Build the docker image from Dockerfile, tag it `channels-test`:: +Build the docker image from Dockerfile, tag it `channels-redis-test`:: - docker build -t channels-test . + docker build -t channels-redis-test -f Dockerfile.redis . Run the server:: - docker-compose up -d + docker-compose -f docker-compose.redis.yml up The benchmark project will now be running on: http:{your-docker-ip}:80 @@ -40,6 +40,19 @@ Let's just try a quick test with the default values from the parameter list:: python benchmark.py ws://localhost:80 +How to use with Docker and RabbitMQ: +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Build the docker image from Dockerfile, tag it `channels-rabbitmq-test`:: + + docker build -t channels-rabbitmq-test -f Dockerfile.rabbitmq . + +Run the server:: + + docker-compose -f docker-compose.rabbitmq.yml up + +The rest is the same. + How to use with runserver: ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -62,14 +75,14 @@ In another terminal window, run the benchmark with:: Additional load testing options: -~~~~~~~~~~~~~~~~~~~~~~~~~~ - +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + If you wish to setup a separate machine to loadtest your environment, you can do the following steps. Install fabric on your machine. This is highly dependent on what your environment looks like, but the recommend option is to:: pip install fabric - + (Hint: if you're on Windows 10, just use the Linux subsystem and use ``apt-get install fabric``. It'll save you a lot of trouble.) Git clone this project down to your machine:: @@ -81,7 +94,7 @@ Relative to where you cloned the directory, move up a couple levels:: cd channels/testproject/ Spin up a server on your favorite cloud host (AWS, Linode, Digital Ocean, etc.) and get its host and credentials. Run the following command using those credentials:: - + fab setup_load_tester -i "ida_rsa" -H ubuntu@example.com That machine will provision itself. It may (depending on your vendor) prompt you a few times for a ``Y/n`` question. This is just asking you about increasing stroage space. diff --git a/testproject/chtest/consumers.py b/testproject/chtest/consumers.py index f447e45..85db43f 100644 --- a/testproject/chtest/consumers.py +++ b/testproject/chtest/consumers.py @@ -1,14 +1,6 @@ from channels.sessions import enforce_ordering -#@enforce_ordering(slight=True) -def ws_connect(message): - pass - - -#@enforce_ordering(slight=True) def ws_message(message): "Echoes messages back to the client" - message.reply_channel.send({ - "text": message['text'], - }) + message.reply_channel.send({'text': message['text']}) diff --git a/testproject/docker-compose.rabbitmq.yml b/testproject/docker-compose.rabbitmq.yml new file mode 100644 index 0000000..1cc885d --- /dev/null +++ b/testproject/docker-compose.rabbitmq.yml @@ -0,0 +1,28 @@ +version: '2' +services: + rabbitmq: + image: rabbitmq:management + ports: + - "15672:15672" + rabbitmq_daphne: + image: channels-rabbitmq-test + build: + context: . + dockerfile: Dockerfile.rabbitmq + command: daphne -b 0.0.0.0 -p 80 testproject.asgi.rabbitmq:channel_layer + volumes: + - .:/srv/channels/testproject/ + ports: + - "80:80" + depends_on: + - rabbitmq + rabbitmq_worker: + image: channels-rabbitmq-test + build: + context: . + dockerfile: Dockerfile.rabbitmq + command: python manage.py runworker --settings=testproject.settings.channels_rabbitmq + volumes: + - .:/srv/channels/testproject/ + depends_on: + - rabbitmq diff --git a/testproject/docker-compose.redis.yml b/testproject/docker-compose.redis.yml new file mode 100644 index 0000000..8cb3d18 --- /dev/null +++ b/testproject/docker-compose.redis.yml @@ -0,0 +1,26 @@ +version: '2' +services: + redis: + image: redis:alpine + redis_daphne: + image: channels-redis-test + build: + context: . + dockerfile: Dockerfile.redis + command: daphne -b 0.0.0.0 -p 80 testproject.asgi.redis:channel_layer + volumes: + - .:/srv/channels/testproject/ + ports: + - "80:80" + depends_on: + - redis + redis_worker: + image: channels-redis-test + build: + context: . + dockerfile: Dockerfile.redis + command: python manage.py runworker --settings=testproject.settings.channels_redis + volumes: + - .:/srv/channels/testproject/ + depends_on: + - redis diff --git a/testproject/docker-compose.yml b/testproject/docker-compose.yml deleted file mode 100644 index 1753b71..0000000 --- a/testproject/docker-compose.yml +++ /dev/null @@ -1,18 +0,0 @@ -version: '2' -services: - redis: - image: redis:alpine - daphne: - image: channels-test - build: . - command: daphne -b 0.0.0.0 -p 80 testproject.asgi:channel_layer - ports: - - "80:80" - depends_on: - - redis - worker: - image: channels-test - build: . - command: python manage.py runworker --settings=testproject.settings.channels_redis - depends_on: - - redis diff --git a/testproject/fabfile.py b/testproject/fabfile.py index 6c884dc..772ff08 100644 --- a/testproject/fabfile.py +++ b/testproject/fabfile.py @@ -23,7 +23,7 @@ def setup_channels(): @task def run_daphne(redis_ip): with cd("/srv/channels/testproject/"): - sudo("REDIS_URL=redis://%s:6379 daphne -b 0.0.0.0 -p 80 testproject.asgi:channel_layer" % redis_ip) + sudo("REDIS_URL=redis://%s:6379 daphne -b 0.0.0.0 -p 80 testproject.asgi.redis:channel_layer" % redis_ip) @task diff --git a/testproject/manage.py b/testproject/manage.py index 97ed576..9a0be8b 100644 --- a/testproject/manage.py +++ b/testproject/manage.py @@ -3,7 +3,10 @@ import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings") + os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", + "testproject.settings.channels_redis", + ) from django.core.management import execute_from_command_line diff --git a/testproject/requirements.benchmark.txt b/testproject/requirements.benchmark.txt new file mode 100644 index 0000000..f04a638 --- /dev/null +++ b/testproject/requirements.benchmark.txt @@ -0,0 +1,7 @@ +autobahn==0.17.1 +constantly==15.1.0 +incremental==16.10.1 +six==1.10.0 +Twisted==16.6.0 +txaio==2.6.0 +zope.interface==4.3.3 diff --git a/testproject/testproject/asgi/__init__.py b/testproject/testproject/asgi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testproject/testproject/asgi_for_ipc.py b/testproject/testproject/asgi/ipc.py similarity index 100% rename from testproject/testproject/asgi_for_ipc.py rename to testproject/testproject/asgi/ipc.py diff --git a/testproject/testproject/asgi/rabbitmq.py b/testproject/testproject/asgi/rabbitmq.py new file mode 100644 index 0000000..b132a73 --- /dev/null +++ b/testproject/testproject/asgi/rabbitmq.py @@ -0,0 +1,8 @@ +import os +from channels.asgi import get_channel_layer + +os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", + "testproject.settings.channels_rabbitmq", +) +channel_layer = get_channel_layer() diff --git a/testproject/testproject/asgi.py b/testproject/testproject/asgi/redis.py similarity index 100% rename from testproject/testproject/asgi.py rename to testproject/testproject/asgi/redis.py diff --git a/testproject/testproject/settings/channels_rabbitmq.py b/testproject/testproject/settings/channels_rabbitmq.py new file mode 100644 index 0000000..4754d09 --- /dev/null +++ b/testproject/testproject/settings/channels_rabbitmq.py @@ -0,0 +1,14 @@ +# Settings for channels specifically +from testproject.settings.base import * + +INSTALLED_APPS += ('channels',) + +CHANNEL_LAYERS = { + 'default': { + 'BACKEND': 'asgi_rabbitmq.RabbitmqChannelLayer', + 'ROUTING': 'testproject.urls.channel_routing', + 'CONFIG': { + 'url': os.environ['RABBITMQ_URL'], + }, + }, +} diff --git a/testproject/testproject/urls.py b/testproject/testproject/urls.py index 89bb74d..7341fa0 100644 --- a/testproject/testproject/urls.py +++ b/testproject/testproject/urls.py @@ -1,17 +1,11 @@ -from django.conf.urls import url +from django.conf.urls import url from chtest import views -urlpatterns = [ - url(r'^$', views.index), -] - +urlpatterns = [url(r'^$', views.index)] try: from chtest import consumers - - channel_routing = { - "websocket.receive": consumers.ws_message, - "websocket.connect": consumers.ws_connect, -} + + channel_routing = {"websocket.receive": consumers.ws_message} except: pass