|
import logging |
|
import time |
|
import random |
|
from os.path import expanduser, abspath |
|
from petrel_client.client_base import ClientBase |
|
from petrel_client.common.io_profile import profile |
|
from petrel_client.common.exception import ObjectNotFoundError |
|
from petrel_client.common.config import Config |
|
from petrel_client.common.log import init_log |
|
from petrel_client.common.io_profile import Profiler |
|
|
|
|
|
LOG = logging.getLogger(__name__) |
|
|
|
|
|
class Client(ClientBase): |
|
def __init__(self, conf_path, name, count_disp): |
|
conf_path = abspath(expanduser(conf_path)) |
|
config = Config(conf_path) |
|
self._default_config = config.default() |
|
|
|
init_log(self._default_config) |
|
LOG.info('init io_profile_test.Client, conf_path %s', conf_path) |
|
|
|
Profiler.set_default_conf(self._default_config) |
|
super(Client, self).__init__(name=name, count_disp=count_disp) |
|
|
|
@profile('get') |
|
def get(self, key): |
|
|
|
def not_found(): |
|
raise ObjectNotFoundError(key) |
|
|
|
def error(): |
|
raise Exception(key) |
|
|
|
def found(): |
|
return 'content' |
|
|
|
action = random.choice([found, not_found, error]) |
|
time.sleep(0.001) |
|
return action() |
|
|
|
@profile('put') |
|
def put(self, key, content): |
|
def normal(): |
|
return len(content) |
|
|
|
def error(): |
|
raise Exception(key) |
|
|
|
action = random.choice([normal, error]) |
|
return action() |
|
|
|
|
|
c = Client(conf_path='~/petreloss.conf', name='cluster1', count_disp=50) |
|
|
|
|
|
for _ in range(100): |
|
try: |
|
c.get('key') |
|
except Exception: |
|
pass |
|
|