File size: 1,612 Bytes
2abfccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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