1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd.
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 ##############################################################################
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
16 LOG = logging.getLogger(__name__)
19 class Fio(base.Scenario):
20 """Execute fio benchmark in a host
23 filename - file name for fio workload
26 default: /home/ubuntu/data.raw
27 bs - block size used for the io units
31 iodepth - number of iobuffers to keep in flight
35 rw - type of io pattern [read, write, randwrite, randread, rw, randrw]
39 ramp_time - run time before logging any performance
44 Read link below for more fio args description:
45 http://www.bluestop.org/fio/HOWTO.txt
47 __scenario_type__ = "Fio"
49 TARGET_SCRIPT = "fio_benchmark.bash"
51 def __init__(self, scenario_cfg, context_cfg):
52 self.scenario_cfg = scenario_cfg
53 self.context_cfg = context_cfg
54 self.setup_done = False
58 self.target_script = pkg_resources.resource_filename(
59 "yardstick.benchmark.scenarios.storage",
61 host = self.context_cfg["host"]
62 user = host.get("user", "root")
63 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
64 ip = host.get("ip", None)
65 key_filename = host.get("key_filename", "~/.ssh/id_rsa")
67 LOG.info("user:%s, host:%s", user, ip)
68 self.client = ssh.SSH(user, ip, key_filename=key_filename,
70 self.client.wait(timeout=600)
73 self.client._put_file_shell(self.target_script, '~/fio.sh')
75 self.setup_done = True
77 def run(self, result):
78 """execute the benchmark"""
79 default_args = "-ioengine=libaio -direct=1 -group_reporting " \
80 "-numjobs=1 -time_based --output-format=json"
82 if not self.setup_done:
85 options = self.scenario_cfg["options"]
86 filename = options.get("filename", "/home/ubuntu/data.raw")
87 bs = options.get("bs", "4k")
88 iodepth = options.get("iodepth", "1")
89 rw = options.get("rw", "write")
90 ramp_time = options.get("ramp_time", 20)
91 name = "yardstick-fio"
92 # if run by a duration runner
93 duration_time = self.scenario_cfg["runner"].get("duration", None) \
94 if "runner" in self.scenario_cfg else None
95 # if run by an arithmetic runner
96 arithmetic_time = options.get("duration", None)
98 runtime = duration_time
100 runtime = arithmetic_time
104 cmd_args = "-filename=%s -bs=%s -iodepth=%s -rw=%s -ramp_time=%s " \
105 "-runtime=%s -name=%s %s" \
106 % (filename, bs, iodepth, rw, ramp_time, runtime, name,
108 cmd = "sudo bash fio.sh %s %s" % (filename, cmd_args)
109 LOG.debug("Executing command: %s", cmd)
110 # Set timeout, so that the cmd execution does not exit incorrectly
111 # when the test run time is last long
112 timeout = int(ramp_time) + int(runtime) + 600
113 status, stdout, stderr = self.client.execute(cmd, timeout=timeout)
115 raise RuntimeError(stderr)
117 raw_data = json.loads(stdout)
119 # The bandwidth unit is KB/s, and latency unit is us
120 if rw in ["read", "randread", "rw", "randrw"]:
121 result["read_bw"] = raw_data["jobs"][0]["read"]["bw"]
122 result["read_iops"] = raw_data["jobs"][0]["read"]["iops"]
123 result["read_lat"] = raw_data["jobs"][0]["read"]["lat"]["mean"]
124 if rw in ["write", "randwrite", "rw", "randrw"]:
125 result["write_bw"] = raw_data["jobs"][0]["write"]["bw"]
126 result["write_iops"] = raw_data["jobs"][0]["write"]["iops"]
127 result["write_lat"] = raw_data["jobs"][0]["write"]["lat"]["mean"]
129 if "sla" in self.scenario_cfg:
131 for k, v in result.items():
132 if k not in self.scenario_cfg['sla']:
136 # For lattency small value is better
137 max_v = float(self.scenario_cfg['sla'][k])
139 sla_error += "%s %f > sla:%s(%f); " % (k, v, k, max_v)
141 # For bandwidth and iops big value is better
142 min_v = int(self.scenario_cfg['sla'][k])
144 sla_error += "%s %d < " \
145 "sla:%s(%d); " % (k, v, k, min_v)
147 assert sla_error == "", sla_error
151 '''internal test function'''
152 key_filename = pkg_resources.resource_filename("yardstick.resources",
153 "files/yardstick_key")
156 "ip": "10.229.47.137",
158 "key_filename": key_filename
162 logger = logging.getLogger("yardstick")
163 logger.setLevel(logging.DEBUG)
166 "filename": "/home/ubuntu/data.raw",
174 args = {"options": options}
180 if __name__ == '__main__':