-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
128 lines (100 loc) · 3.11 KB
/
utils.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import logging
import time
import math
import statistics
import yaml
from faker import Faker
import randomio
from os_benchmark import errors
from os_benchmark.drivers import utils as driver_utils
logger = logging.getLogger('osb.utils')
faker = Faker()
def get_config_file(config_file=None):
"""
Get full configuration
"""
if 'OSB_CONFIG_FILE' in os.environ:
files = [os.environ['OSB_CONFIG_FILE']]
elif config_file is not None:
files = [config_file]
else:
files = ['~/.osb.yml', '/etc/osb.yml']
configs = None
for filename in files:
filename = os.path.expanduser(filename)
try:
with open(filename) as fd:
configs = yaml.full_load(fd)
break
except FileNotFoundError:
continue
logger.info("Use config file '%s'", filename)
if not configs:
msg = "No configuration file found."
raise errors.ConfigurationError(msg)
return configs
def get_driver_config(config_name=None, config_file=None):
"""
Get driver configuration as dict
"""
configs = get_config_file(config_file=config_file)
if len(configs) == 1 and config_name is None:
config_name = list(configs.keys())[0]
logger.debug("Use the single driver config '%s'", config_name)
if config_name and config_name not in configs:
msg = "'%s' config found." % config_name
raise errors.ConfigurationError(msg)
if config_name is None:
msg = 'Unknown configuration, please specify one in %s' % (
', '.join([c for c in configs])
)
raise errors.ConfigurationError(msg)
return configs[config_name]
def get_driver(config):
"""Get configured driver"""
key = config.pop('driver')
driver_class = driver_utils.get_driver_class(key)
logger.debug("Driver configured with '%s'", config)
driver = driver_class(**config)
return driver
def get_random_name(size=30, prefix=None, suffix=None):
"""
Creates a random name with static prefix or suffix.
"""
name = faker.user_name()
while len(name) < size:
name += faker.user_name()
if prefix:
name = prefix + name[len(prefix):size]
if suffix:
name = name[:size-len(suffix)] + suffix
return name[:size]
def get_random_content(size):
"""Creates a random fileobj"""
return randomio.FileGenerator(size)
def timeit(func, *args, **kwargs):
"""Time a function"""
start = time.time()
output = func(*args, **kwargs)
end = time.time()
elapsed = end - start
return elapsed, output
async def async_timeit(func, *args, **kwargs):
"""Time a function"""
start = time.time()
output = await func(*args, **kwargs)
end = time.time()
elapsed = end - start
return elapsed, output
def percentile(values, percent):
if not values:
return
values = sorted(values)
return values[int(math.ceil((len(values) * percent) / 100)) - 1]
def percentile95(values):
return percentile(values, 95)
def unescape(string):
if string.startswith('\\'):
return string[1:]
return string