Skip to content

Commit

Permalink
Merge pull request #11 from tanwanirahul/concurrency_seq_duration_test
Browse files Browse the repository at this point in the history
Concurrency seq duration test
  • Loading branch information
tanwanirahul committed Feb 28, 2016
2 parents 802981b + cef8a54 commit f7e07d4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
20 changes: 20 additions & 0 deletions tests/test_concurrency_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ def compare_seq_and_concurrent_req(self):

for idx, seq_resp in enumerate(seq_responses):
self.assertDictEqual(seq_resp, conc_responses[idx], "Sequential and concurrent response not same!")

def compare_seq_concurrent_duration(self):
'''
Makes the batch requests run sequentially and in parallel and asserts
parallelism to reduce the total duration time.
'''
# Make a batch call for GET, POST and PUT request.
sleep_2_seconds = ("get", "/sleep/?seconds=1", '', {})
sleep_1_second = ("get", "/sleep/?seconds=1", '', {})

# Get the response for a batch request.
batch_requests = self.make_multiple_batch_request([sleep_2_seconds, sleep_1_second, sleep_2_seconds])
seq_duration = int(batch_requests._headers.get(br_settings.DURATION_HEADER_NAME)[1])

# Update the executor settings.
br_settings.executor = self.get_executor()
concurrent_batch_requests = self.make_multiple_batch_request([sleep_2_seconds, sleep_1_second, sleep_2_seconds])
concurrency_duration = int(concurrent_batch_requests._headers.get(br_settings.DURATION_HEADER_NAME)[1])

self.assertLess(concurrency_duration, seq_duration, "Concurrent requests are slower than running them in sequence.")
9 changes: 8 additions & 1 deletion tests/test_process_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from batch_requests.concurrent.executor import ProcessBasedExecutor


class TestThreadConcurrency(TestBaseConcurrency):
class TestProcessConcurrency(TestBaseConcurrency):
'''
Tests sequential and concurrent process based execution.
'''
Expand All @@ -24,3 +24,10 @@ def test_thread_concurrency_response(self):
the response.
'''
self.compare_seq_and_concurrent_req()

def test_duration(self):
'''
Compare that running tests with ProcessBasedConcurreny return faster than running
them sequentially.
'''
self.compare_seq_concurrent_duration()
7 changes: 7 additions & 0 deletions tests/test_thread_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ def test_thread_concurrency_response(self):
the response.
'''
self.compare_seq_and_concurrent_req()

def test_duration(self):
'''
Compare that running tests with ThreadBasedConcurreny return faster than running
them sequentially.
'''
self.compare_seq_concurrent_duration()
21 changes: 21 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from time import sleep


class SimpleView(View):
Expand Down Expand Up @@ -106,3 +107,23 @@ def get(self, request, *args, **kwargs):
Handles the get request.
'''
raise Exception("exception")


class SleepingView(View):

'''
Make the current thread sleep for the number of seconds passed.
This is to mimic the long running services.
'''

def get(self, request, *args, **kwargs):
'''
Handles the get request.
'''
# Lookup for the duration to sleep.
seconds = int(request.GET.get("seconds", "5"))

# Make the current thread sleep for the specified duration.
sleep(seconds)
# Make the current thread sleep.
return HttpResponse("Success!")
4 changes: 3 additions & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from django.conf.urls import patterns, url

from batch_requests.views import handle_batch_requests
from tests.test_views import SimpleView, EchoHeaderView, ExceptionView
from tests.test_views import SimpleView, EchoHeaderView, ExceptionView,\
SleepingView


urlpatterns = patterns('',
url(r'^views/', SimpleView.as_view()),
url(r'^echo/', EchoHeaderView.as_view()),
url(r'^exception/', ExceptionView.as_view()),
url(r'^sleep/', SleepingView.as_view()),
url(r'^api/v1/batch/', handle_batch_requests),
)

0 comments on commit f7e07d4

Please sign in to comment.