blob: 7313fdd6c61b52c34eca602fe32aa65dde272349 [file] [log] [blame]
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02001import logging
Ievgeniia Zadorozhna97dfde42022-06-17 20:05:09 +03002import pytest
3import random
4import subprocess
5import sys
6import time
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +02007
8import utils
9
10logger = logging.getLogger(__name__)
11
12
13def is_parsable(value, to):
14 """
15 Check if value can be converted into some type
16 :param value: input value that should be converted
17 :param to: type of output value like int, float. It's not a string!
18 :return: bool
19 """
20 try:
21 to(value)
22 except:
23 return False
24 return True
25
26
27@pytest.fixture
28def create_image():
29 image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB", 9000)
30 create_file_cmdline = 'dd if=/dev/zero of=/tmp/image_mk_framework.dd ' \
31 'bs=1M count={} 2>/dev/null' \
32 ''.format(image_size_megabytes)
33 is_cmd_successful = subprocess.call(create_file_cmdline, shell=True) == 0
34 logger.info("Created local image file /tmp/image_mk_framework.dd")
35 yield is_cmd_successful
36
37 # teardown
38 logger.info("Deleting /tmp/image_mk_framework.dd file")
39 subprocess.call('rm -f /tmp/image_mk_framework.dd', shell=True)
40 subprocess.call('rm -f /tmp/image_mk_framework.download', shell=True)
41
42
43def test_speed_glance(create_image, openstack_clients, record_property):
44 """
45 Simplified Performance Tests Download / upload Glance
46 1. Create file with random data (dd)
47 2. Upload data as image to glance.
48 3. Download image.
49 4. Measure download/upload speed and print them into stdout
50 """
51 image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB")
52 if not is_parsable(image_size_megabytes, int):
53 pytest.fail("Can't convert IMAGE_SIZE_MB={} to 'int'".format(
54 image_size_megabytes))
55 image_size_megabytes = int(image_size_megabytes)
56 if not create_image:
57 pytest.skip("Can't create image, maybe there is lack of disk "
58 "space to create file {}MB".
59 format(image_size_megabytes))
60 image_name = "spt-test-image-{}".format(random.randrange(100, 999))
61 try:
62 image = openstack_clients.image.images.create(
63 name=image_name,
Ievgeniia Zadorozhna6ba79092022-12-13 03:06:05 +030064 disk_format='raw',
Ievgeniia Zadorozhna84023022021-12-30 13:00:41 +020065 container_format='bare')
66 logger.info("Created an image {} in Glance.".format(image_name))
67 except BaseException as e:
68 logger.info("Could not create image in Glance. See details: {}"
69 "".format(e))
70 pytest.fail("Can't create image in Glance. Occurred error: {}"
71 "".format(e))
72
73 logger.info("Testing upload file speed...")
74 start_time = time.time()
75 try:
76 openstack_clients.image.images.upload(
77 image.id, image_data=open("/tmp/image_mk_framework.dd", 'rb'))
78 except BaseException as e:
79 pytest.fail("Can't upload image in Glance. "
80 "Occurred error: {}".format(e))
81 end_time = time.time()
82
83 speed_upload = image_size_megabytes / (end_time - start_time)
84
85 logger.info("Testing download file speed...")
86 start_time = time.time()
87 with open("/tmp/image_mk_framework.download", 'wb') as image_file:
88 for item in openstack_clients.image.images.data(image.id):
89 image_file.write(item)
90 end_time = time.time()
91
92 speed_download = image_size_megabytes / (end_time - start_time)
93 logger.info("Deleted image {}.".format(image.id))
94 openstack_clients.image.images.delete(image.id)
95 record_property("Upload", speed_upload)
96 record_property("Download", speed_download)
97
Ievgeniia Zadorozhna97dfde42022-06-17 20:05:09 +030098 sys.stdout.write("\n++++++++++++++++++++++++++++++++++++++++")
99 sys.stdout.write(('\nupload - {} MB/s'.format(speed_upload)))
100 sys.stdout.write(('\ndownload - {} MB/s'.format(speed_download)))
101 sys.stdout.write("\n++++++++++++++++++++++++++++++++++++++++\n")