Merge "Cleanup CGNAPT unit tests"
[yardstick.git] / yardstick / benchmark / scenarios / lib / add_memory_load.py
1 # ############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # ############################################################################
9
10 from __future__ import print_function
11 from __future__ import absolute_import
12
13 import logging
14
15 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
17
18 LOG = logging.getLogger(__name__)
19
20
21 class AddMemoryLoad(base.Scenario):
22     """Add memory load in server
23     """
24
25     __scenario_type__ = "AddMemoryLoad"
26
27     def __init__(self, scenario_cfg, context_cfg):
28         self.scenario_cfg = scenario_cfg
29         self.context_cfg = context_cfg
30
31         self.options = scenario_cfg.get('options', {})
32
33         self.client = ssh.SSH.from_node(self.context_cfg['host'])
34         self.client.wait(timeout=600)
35
36     def run(self, result):
37         self._add_load()
38
39     def _add_load(self):
40         try:
41             memory_load = self.options['memory_load']
42         except KeyError:
43             LOG.error('memory_load parameter must be provided')
44         else:
45             if float(memory_load) == 0:
46                 return
47             cmd = 'free | awk "/Mem/ {print $2}"'
48             code, stdout, stderr = self.client.execute(cmd)
49             total = int(stdout.split()[1])
50             used = int(stdout.split()[2])
51             remain_memory = total * float(memory_load) - used
52             if remain_memory > 0:
53                 count = remain_memory / 1024 / 128
54                 LOG.info('Add %s vm load', count)
55                 if count != 0:
56                     cmd = 'stress -t 10 -m {} --vm-keep'.format(count)
57                     self.client.execute(cmd)