aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/lib/add_memory_load.py
blob: 26cf140d1efb0f61fbd286c6e57c08b01bc2f804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# ############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
# ############################################################################

from __future__ import print_function
from __future__ import absolute_import

import logging

import yardstick.ssh as ssh
from yardstick.benchmark.scenarios import base

LOG = logging.getLogger(__name__)


class AddMemoryLoad(base.Scenario):
    """Add memory load in server
    """

    __scenario_type__ = "AddMemoryLoad"

    def __init__(self, scenario_cfg, context_cfg):
        self.scenario_cfg = scenario_cfg
        self.context_cfg = context_cfg

        self.options = scenario_cfg.get('options', {})

        self.client = ssh.SSH.from_node(self.context_cfg['host'])
        self.client.wait(timeout=600)

    def run(self, result):
        self._add_load()

    def _add_load(self):
        try:
            memory_load = self.options['memory_load']
        except KeyError:
            LOG.error('memory_load parameter must be provided')
        else:
            if float(memory_load) == 0:
                return
            cmd = 'free | awk "/Mem/ {print $2}"'
            code, stdout, stderr = self.client.execute(cmd)
            total = int(stdout.split()[1])
            used = int(stdout.split()[2])
            remain_memory = total * float(memory_load) - used
            if remain_memory > 0:
                count = remain_memory / 1024 / 128
                LOG.info('Add %s vm load', count)
                if count != 0:
                    cmd = 'stress -t 10 -m {} --vm-keep'.format(count)
                    self.client.execute(cmd)