| 1 |
# Copyright 2020 - Nokia Software. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
import json |
| 17 |
import math |
| 18 |
import random |
| 19 |
import string |
| 20 |
|
| 21 |
from rally.task import validation |
| 22 |
from rally_openstack import consts |
| 23 |
from rally_openstack import scenario |
| 24 |
from rally_openstack.scenarios.mistral import utils |
| 25 |
|
| 26 |
|
| 27 |
def random_string(length=10): |
| 28 |
"""Generate a random string of given length """ |
| 29 |
|
| 30 |
letters = string.ascii_lowercase |
| 31 |
|
| 32 |
return ''.join(random.choices(letters, k=length)) |
| 33 |
|
| 34 |
|
| 35 |
class MistralExpressionScenario(utils.MistralScenario): |
| 36 |
|
| 37 |
def run(self, tasks_number=50, params_size_mb=5): |
| 38 |
wf_text, wf_name = self.create_wf_string(tasks_number) |
| 39 |
params = self.create_params(params_size_mb) |
| 40 |
|
| 41 |
self._create_workflow(wf_text) |
| 42 |
self._create_execution(wf_name, **params) |
| 43 |
|
| 44 |
def create_params(self, size_mb): |
| 45 |
block_size_mb = 0.2 |
| 46 |
rand_string = random_string(105400) |
| 47 |
number_of_fields = math.floor(size_mb / block_size_mb) |
| 48 |
data_list = '' |
| 49 |
# each one of these blocks is 200kb |
| 50 |
data_template = """ |
| 51 |
"data%s": |
| 52 |
{ |
| 53 |
"dummy": 5470438, |
| 54 |
"data_value": -796997888, |
| 55 |
"sub_data": |
| 56 |
{ |
| 57 |
"meta_data": |
| 58 |
{ |
| 59 |
"value": "%s", |
| 60 |
"text": "dummy text" |
| 61 |
}, |
| 62 |
"field1": "%s", |
| 63 |
"field2": false, |
| 64 |
"field3": |
| 65 |
{ |
| 66 |
"value1": -1081872761.2081857, |
| 67 |
"value2": -1081872761.2081857 |
| 68 |
} |
| 69 |
} |
| 70 |
}, |
| 71 |
""" |
| 72 |
wf_params = """ |
| 73 |
{ |
| 74 |
"field": "some Value", |
| 75 |
"data": |
| 76 |
{ |
| 77 |
{{{__DATA_LIST__}}} |
| 78 |
} |
| 79 |
} |
| 80 |
""" |
| 81 |
for i in range(1, int(number_of_fields + 1)): |
| 82 |
data_list += data_template % (i, rand_string, rand_string) |
| 83 |
|
| 84 |
data_list = data_list[:-2] |
| 85 |
|
| 86 |
wf_params = wf_params.replace('{{{__DATA_LIST__}}}', data_list) |
| 87 |
params = json.loads(wf_params) |
| 88 |
|
| 89 |
return params |
| 90 |
|
| 91 |
def get_query(self): |
| 92 |
raise NotImplementedError |
| 93 |
|
| 94 |
def create_wf_string(self, tasks_number): |
| 95 |
wf_tasks = '' |
| 96 |
wf_name = 'wf_{}'.format(random_string(5)) |
| 97 |
|
| 98 |
query = self.get_query() |
| 99 |
|
| 100 |
wf_text = """ |
| 101 |
version: '2.0' |
| 102 |
{}: |
| 103 |
tasks: |
| 104 |
task0: |
| 105 |
action: std.noop |
| 106 |
{{{__TASK_LIST__}}} |
| 107 |
""" |
| 108 |
task_template = """ |
| 109 |
task{}: |
| 110 |
action: std.noop |
| 111 |
publish: |
| 112 |
output{}: {} |
| 113 |
""" |
| 114 |
|
| 115 |
for i in range(1, tasks_number + 1): |
| 116 |
wf_tasks += task_template.format(i, i, query) |
| 117 |
|
| 118 |
wf_text = wf_text.replace('{{{__TASK_LIST__}}}', wf_tasks) |
| 119 |
|
| 120 |
wf_text = wf_text.format(wf_name) |
| 121 |
|
| 122 |
return wf_text, wf_name |
| 123 |
|
| 124 |
|
| 125 |
@validation.add("required_platform", platform="openstack", users=True) |
| 126 |
@validation.add("required_services", services=[consts.Service.MISTRAL]) |
| 127 |
@scenario.configure(name="MistralExecutions.YaqlExpression", |
| 128 |
platform="openstack") |
| 129 |
class YaqlExpressionScenario(MistralExpressionScenario): |
| 130 |
|
| 131 |
def get_query(self): |
| 132 |
return '<% data %>' |
| 133 |
|
| 134 |
|
| 135 |
@validation.add("required_platform", platform="openstack", users=True) |
| 136 |
@validation.add("required_services", services=[consts.Service.MISTRAL]) |
| 137 |
@scenario.configure(name="MistralExecutions.JinjaExpression", |
| 138 |
platform="openstack") |
| 139 |
class JinjaExpressionScenario(MistralExpressionScenario): |
| 140 |
|
| 141 |
def get_query(self): |
| 142 |
return '{{ data }} ' |