Merge "Add Http Request Scenario"

This commit is contained in:
Jenkins 2015-04-29 14:36:51 +00:00 committed by Gerrit Code Review
commit 1daf204d31
11 changed files with 204 additions and 47 deletions

View File

@ -468,15 +468,16 @@
failure_rate:
max: 0
Requests.check_response:
HttpRequests.check_request:
-
args:
url: "http://git.openstack.org/cgit"
response: 200
url: "http://www.example.com"
method: "GET"
status_code: 200
runner:
type: "constant"
times: 1
concurrency: 1
times: 2
concurrency: 2
sla:
failure_rate:
max: 0

View File

@ -1406,15 +1406,38 @@
failure_rate:
max: 0
Requests.check_response:
HttpRequests.check_request:
-
args:
url: "http://www.google.com"
response: 200
url: "http://www.example.com"
method: "GET"
status_code: 200
runner:
type: "constant"
times: 5
concurrency: 5
times: 2
concurrency: 2
sla:
failure_rate:
max: 0
HttpRequests.check_random_request:
-
args:
requests:
-
url: "http://www.example.com"
method: "GET"
-
url: "http://www.openstack.org"
method: "GET"
status_code: 200
runner:
type: "constant"
times: 2
concurrency: 2
sla:
failure_rate:
max: 0
NovaSecGroup.create_and_delete_secgroups:
-

View File

@ -10,32 +10,42 @@
# License for the specific language governing permissions and limitations
# under the License.
import requests
import random
from rally.benchmark.scenarios import base
from rally.common.i18n import _
from rally import exceptions
from rally.benchmark.scenarios.requests import utils
class WrongStatusException(exceptions.RallyException):
msg_fmt = _("Requests scenario exception: '%(message)s'")
class Requests(base.Scenario):
class HttpRequests(utils.RequestScenario):
"""Benchmark scenarios for HTTP requests."""
@base.scenario()
def check_response(self, url, response=None):
def check_request(self, url, method, status_code, **kwargs):
"""Standard way to benchmark web services.
This benchmark is used to GET a URL and check it with expected
This benchmark is used to make request and check it with expected
Response.
:param url: URL to be fetched
:param response: expected response code
:param url: url for the Request object
:param method: method for the Request object
:param status_code: expected response code
:param kwargs: optional additional request parameters
"""
resp = requests.head(url)
if response and response != resp.status_code:
error = "Expected HTTP request code is `%s` actual `%s`" % (
response, resp.status_code)
raise WrongStatusException(error)
self._check_request(url, method, status_code, **kwargs)
@base.scenario()
def check_random_request(self, requests, status_code):
"""Benchmark the list of requests
This scenario takes random url from list of requests, and raises
exception if the response is not the expected response.
:param requests: List of request dicts
:param status_code: Expected Response Code it will
be used only if we doesn't specified it in request proper
"""
request = random.choice(requests)
request.setdefault("status_code", status_code)
self._check_request(**request)

View File

@ -0,0 +1,38 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import requests
from rally.benchmark.scenarios import base
from rally.common.i18n import _
class RequestScenario(base.Scenario):
"""Base class for Request scenarios with basic atomic actions."""
@base.atomic_action_timer("requests.check_request")
def _check_request(self, url, method, status_code, **kwargs):
"""Compare request status code with specified code
:param status_code: Expected status code of request
:param url: Uniform resource locator
:param method: Type of request method (GET | POST ..)
:param kwargs: Optional additional request parameters
:raises: ValueError if return http status code
not equal to expected status code
"""
resp = requests.request(method, url, **kwargs)
if status_code != resp.status_code:
error_msg = _("Expected HTTP request code is `%s` actual `%s`")
raise ValueError(
error_msg % (status_code, resp.status_code))

View File

@ -0,0 +1,17 @@
{
"HttpRequests.check_random_request": [
{
"args": {
"requests": [{"url": "http://www.example.com", "method": "GET",
"status_code": 200},
{"url": "http://www.openstack.org", "method": "GET"}],
"status_code": 200
},
"runner": {
"type": "constant",
"times": 20,
"concurrency": 5
}
}
]
}

View File

@ -0,0 +1,17 @@
---
HttpRequests.check_random_request:
-
args:
requests:
-
url: "http://www.example.com"
method: "GET"
status_code: 200
-
url: "http://www.openstack.org"
method: "GET"
status_code: 200
runner:
type: "constant"
times: 20
concurrency: 5

View File

@ -1,9 +1,11 @@
{
"Requests.check_response": [
"HttpRequests.check_request": [
{
"args": {
"url": "http://www.google.com",
"response": 302
"url": "http://www.example.com",
"method": "GET",
"status_code": 200,
"allow_redirects": false
},
"runner": {
"type": "constant",

View File

@ -0,0 +1,12 @@
---
HttpRequests.check_request:
-
args:
url: "http://www.example.com"
method: "GET"
status_code: 200
allow_redirects: False
runner:
type: "constant"
times: 20
concurrency: 5

View File

@ -1,10 +0,0 @@
---
Requests.check_response:
-
args:
url: "http://www.google.com"
response: 302
runner:
type: "constant"
times: 20
concurrency: 5

View File

@ -21,11 +21,18 @@ SCN = "rally.benchmark.scenarios"
class RequestsTestCase(test.TestCase):
@mock.patch("%s.requests.http_requests.requests" % SCN)
def test_check_response(self, mock_requests):
mock_requests.head(mock.MagicMock()).status_code = 200
Requests = http_requests.Requests()
@mock.patch("%s.requests.utils.RequestScenario._check_request" % SCN)
def test_check_request(self, mock_check):
Requests = http_requests.HttpRequests()
Requests.check_request("sample_url", "GET", 200)
mock_check.assert_called_once_with("sample_url", "GET", 200)
self.assertRaises(http_requests.WrongStatusException,
Requests.check_response, url="sample_url",
response=302)
@mock.patch("%s.requests.utils.RequestScenario._check_request" % SCN)
@mock.patch("%s.requests.http_requests.random.choice" % SCN)
def test_check_random_request(self, mock_random_choice, mock_check):
mock_random_choice.return_value = {"url": "sample_url"}
Requests = http_requests.HttpRequests()
Requests.check_random_request(status_code=200,
requests=[{"url": "sample_url"}])
mock_random_choice.assert_called_once_with([{"url": "sample_url"}])
mock_check.assert_called_once_with(status_code=200, url="sample_url")

View File

@ -0,0 +1,40 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from rally.benchmark.scenarios.requests import utils
from tests.unit import test
SCN = "rally.benchmark.scenarios"
class RequestsTestCase(test.TestCase):
@mock.patch("requests.request")
def test__check_request(self, mock_requests):
mock_requests.return_value = mock.MagicMock(status_code=200)
scenario = utils.RequestScenario()
scenario._check_request(status_code=200, url="sample", method="GET")
self._test_atomic_action_timer(scenario.atomic_actions(),
"requests.check_request")
mock_requests.assert_called_once_with("GET", "sample")
@mock.patch("requests.request")
def test_check_wrong_request(self, mock_requests):
mock_requests.return_value = mock.MagicMock(status_code=200)
scenario = utils.RequestScenario()
self.assertRaises(ValueError, scenario._check_request,
status_code=201, url="sample", method="GET")