Merge "UDP relay"
[yardstick.git] / yardstick / benchmark / scenarios / storage / bonnie.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
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 from __future__ import absolute_import
10 from __future__ import print_function
11
12 import logging
13 import subprocess
14
15 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
17
18 LOG = logging.getLogger(__name__)
19
20
21 class Bonnie(base.Scenario):
22     """Execute bonnie benchmark in a host
23
24     Parameters
25     file_size - size fo the test file in MB. File size should be double RAM for good results.
26         type:    int
27         unit:    MB
28         default: 2048
29     ram_size - specify RAM size in MB to use, this is used to reduce testing time.
30         type:    int
31         unit:    MB
32         default: na
33     test_dir - this directory is where bonnie++ will create the benchmark operations.
34         type:    string
35         unit:    na
36         default: "/tmp"
37     test_user - the user who should perform the test. This is not required if you are not running
38                 as root.
39         type:    string
40         unit:    na
41         default: na
42     concurrency - number of thread to perform test
43         type:    int
44         unit:    na
45         default: 1
46     """
47     __scenario_type__ = "Bonnie++"
48
49     def __init__(self, scenario_cfg, context_cfg):
50         self.scenario_cfg = scenario_cfg
51         self.context_cfg = context_cfg
52         self.setup_done = False
53
54     def setup(self):
55         """scenario setup"""
56         host = self.context_cfg["host"]
57
58         self.client = ssh.SSH.from_node(host, defaults={"user": "root"})
59         self.client.wait(timeout=600)
60
61         self.setup_done = True
62
63     def run(self, result):    # pragma: no cover
64         """execute the benchmark"""
65         if not self.setup_done:
66             self.setup()
67
68         cmd_args = ""
69
70         options = self.scenario_cfg["options"]
71         file_size = options.get("file_size", 2048)
72         test_dir = options.get("test_dir", "/tmp")
73
74         if "ram_size" in options:
75             cmd_args += " -r %s" % options["ram_size"]
76
77         if "test_user" in options:
78             cmd_args += " -u %s" % options["test_user"]
79
80         if "concurrency" in options:
81             cmd_args += " -c %s" % options["concurrency"]
82
83         cmd = "bonnie++ -d %s -s %s %s" % (test_dir, file_size, cmd_args)
84
85         LOG.debug("Executing command: %s", cmd)
86         status, stdout, stderr = self.client.execute(cmd)
87         if status:
88             raise RuntimeError(stderr)
89
90         raw_data = stdout.split('\n')[-2]
91         result.update({"raw_data": raw_data})
92
93         LOG.debug("Generating Bonnie++ HTML report...")
94         with open("/tmp/bonnie.html", "w") as bon_file:
95             p = subprocess.Popen(["bon_csv2html"], stdout=bon_file, stdin=subprocess.PIPE)
96             p.communicate(raw_data)
97         LOG.info('Bonnie++ benchmark completed, please find benchmark report at /tmp/bonnie.html')