Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 1 | import logging |
Ievgeniia Zadorozhna | 97dfde4 | 2022-06-17 20:05:09 +0300 | [diff] [blame] | 2 | import pytest |
| 3 | import random |
| 4 | import subprocess |
| 5 | import sys |
| 6 | import time |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 7 | |
| 8 | import utils |
Ievgeniia Zadorozhna | 9664b42 | 2023-03-28 21:09:46 +0300 | [diff] [blame] | 9 | from utils import helpers |
| 10 | |
| 11 | from texttable import Texttable |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 12 | |
| 13 | logger = logging.getLogger(__name__) |
| 14 | |
| 15 | |
| 16 | def is_parsable(value, to): |
| 17 | """ |
| 18 | Check if value can be converted into some type |
| 19 | :param value: input value that should be converted |
| 20 | :param to: type of output value like int, float. It's not a string! |
| 21 | :return: bool |
| 22 | """ |
| 23 | try: |
| 24 | to(value) |
Ievgeniia Zadorozhna | 670e7ff | 2023-03-29 00:22:22 +0300 | [diff] [blame] | 25 | except BaseException: |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 26 | return False |
| 27 | return True |
| 28 | |
| 29 | |
| 30 | @pytest.fixture |
| 31 | def create_image(): |
| 32 | image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB", 9000) |
| 33 | create_file_cmdline = 'dd if=/dev/zero of=/tmp/image_mk_framework.dd ' \ |
| 34 | 'bs=1M count={} 2>/dev/null' \ |
| 35 | ''.format(image_size_megabytes) |
| 36 | is_cmd_successful = subprocess.call(create_file_cmdline, shell=True) == 0 |
| 37 | logger.info("Created local image file /tmp/image_mk_framework.dd") |
| 38 | yield is_cmd_successful |
| 39 | |
| 40 | # teardown |
| 41 | logger.info("Deleting /tmp/image_mk_framework.dd file") |
| 42 | subprocess.call('rm -f /tmp/image_mk_framework.dd', shell=True) |
| 43 | subprocess.call('rm -f /tmp/image_mk_framework.download', shell=True) |
| 44 | |
| 45 | |
Ievgeniia Zadorozhna | 9664b42 | 2023-03-28 21:09:46 +0300 | [diff] [blame] | 46 | def test_speed_glance(create_image, openstack_clients, |
| 47 | request, html_report): |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 48 | """ |
| 49 | Simplified Performance Tests Download / upload Glance |
| 50 | 1. Create file with random data (dd) |
| 51 | 2. Upload data as image to glance. |
| 52 | 3. Download image. |
| 53 | 4. Measure download/upload speed and print them into stdout |
| 54 | """ |
Ievgeniia Zadorozhna | 9664b42 | 2023-03-28 21:09:46 +0300 | [diff] [blame] | 55 | result_table = Texttable(max_width=120) |
| 56 | table_rows = [["Test Speed Glance", "Image Size", "Time Consumed", |
| 57 | "Result"]] |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 58 | image_size_megabytes = utils.get_configuration().get("IMAGE_SIZE_MB") |
| 59 | if not is_parsable(image_size_megabytes, int): |
| 60 | pytest.fail("Can't convert IMAGE_SIZE_MB={} to 'int'".format( |
| 61 | image_size_megabytes)) |
| 62 | image_size_megabytes = int(image_size_megabytes) |
| 63 | if not create_image: |
| 64 | pytest.skip("Can't create image, maybe there is lack of disk " |
| 65 | "space to create file {}MB". |
| 66 | format(image_size_megabytes)) |
| 67 | image_name = "spt-test-image-{}".format(random.randrange(100, 999)) |
| 68 | try: |
| 69 | image = openstack_clients.image.images.create( |
| 70 | name=image_name, |
Ievgeniia Zadorozhna | 6ba7909 | 2022-12-13 03:06:05 +0300 | [diff] [blame] | 71 | disk_format='raw', |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 72 | container_format='bare') |
| 73 | logger.info("Created an image {} in Glance.".format(image_name)) |
| 74 | except BaseException as e: |
| 75 | logger.info("Could not create image in Glance. See details: {}" |
| 76 | "".format(e)) |
| 77 | pytest.fail("Can't create image in Glance. Occurred error: {}" |
| 78 | "".format(e)) |
| 79 | |
| 80 | logger.info("Testing upload file speed...") |
| 81 | start_time = time.time() |
| 82 | try: |
| 83 | openstack_clients.image.images.upload( |
| 84 | image.id, image_data=open("/tmp/image_mk_framework.dd", 'rb')) |
| 85 | except BaseException as e: |
| 86 | pytest.fail("Can't upload image in Glance. " |
| 87 | "Occurred error: {}".format(e)) |
| 88 | end_time = time.time() |
| 89 | |
Ievgeniia Zadorozhna | 9664b42 | 2023-03-28 21:09:46 +0300 | [diff] [blame] | 90 | time_diff = end_time - start_time |
| 91 | speed_upload = image_size_megabytes / time_diff |
| 92 | table_rows.append(["Upload", |
| 93 | "{} MB".format(image_size_megabytes), |
| 94 | "{} s".format(round(time_diff, 3)), |
| 95 | "{} MB/s".format(round(speed_upload, 2))]) |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 96 | |
| 97 | logger.info("Testing download file speed...") |
| 98 | start_time = time.time() |
| 99 | with open("/tmp/image_mk_framework.download", 'wb') as image_file: |
| 100 | for item in openstack_clients.image.images.data(image.id): |
| 101 | image_file.write(item) |
| 102 | end_time = time.time() |
| 103 | |
Ievgeniia Zadorozhna | 9664b42 | 2023-03-28 21:09:46 +0300 | [diff] [blame] | 104 | time_diff = end_time - start_time |
| 105 | speed_download = image_size_megabytes / time_diff |
| 106 | table_rows.append(["Download", |
| 107 | "{} MB".format(image_size_megabytes), |
| 108 | "{} s".format(round(time_diff, 3)), |
| 109 | "{} MB/s".format(round(speed_download, 2))]) |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 110 | logger.info("Deleted image {}.".format(image.id)) |
| 111 | openstack_clients.image.images.delete(image.id) |
Ievgeniia Zadorozhna | 8402302 | 2021-12-30 13:00:41 +0200 | [diff] [blame] | 112 | |
Ievgeniia Zadorozhna | 9664b42 | 2023-03-28 21:09:46 +0300 | [diff] [blame] | 113 | result_table.add_rows(table_rows) |
| 114 | sys.stdout.write('\n{}\n'.format(result_table.draw())) |
| 115 | |
| 116 | # Send the results to CSV file at reports/ directory |
| 117 | helpers.create_test_result_table_csv_file( |
| 118 | table_rows, request.node.name) |