~ubuntuone-support/+junk/syncdaemon-perftest

« back to all changes in this revision

Viewing changes to u1perftest/__init__.py

  • Committer: Roman Yepishev
  • Date: 2012-04-02 10:28:38 UTC
  • Revision ID: roman.yepishev@canonical.com-20120402102838-cftyw8c8754a1rrj
Latest version of the scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Common u1perftest routines"""
2
2
 
 
3
import os
 
4
 
3
5
from txstatsd.metrics.metrics import Metrics
4
6
from txstatsd.client import UdpStatsDClient
 
7
from u1perftest.util import setup_logging
 
8
 
 
9
from random import randint
5
10
 
6
11
class PerfTest(object):
7
12
    def __init__(self):
8
13
        self.timeout = None
9
14
        self.file_count = None
10
15
        self.file_size = None
 
16
        self.file_size_randomize = False
 
17
        self.file_size_decreasing = True
11
18
        self.logger = None
12
19
        self.track_speed = False
13
20
        self.track_queues = False
14
21
        self.root = None
 
22
        self._file_created_cb = lambda path, info: None
 
23
 
 
24
    def _parse_common_options(self, options):
 
25
        self.logger = setup_logging(options.log)
 
26
        self.file_count = int(options.file_count)
 
27
 
 
28
        if options.file_size.startswith('~'):
 
29
            self.file_size = int(options.file_size[1:])
 
30
            self.file_size_randomize = True
 
31
        elif options.file_size.startswith('..'):
 
32
            self.file_size = int(options.file_size[2:])
 
33
            self.file_size_decreasing = True
 
34
        else:
 
35
            self.file_size = int(options.file_size)
 
36
 
 
37
        self.logger.info("size: %d, randomize: %s, decreasing: %s",
 
38
            self.file_size, self.file_size_randomize, self.file_size_decreasing)
 
39
 
 
40
        self.track_speed = options.track_speed
 
41
        self.track_queues = options.track_queues
 
42
 
 
43
        statsd_info = options.statsd
 
44
        self._setup_metrics(statsd_info, options.graph_prefix)
 
45
 
 
46
        if options.timeout:
 
47
            self.timeout = int(options.timeout)
15
48
 
16
49
    def _setup_metrics(self, info, prefix):
17
50
 
27
60
        connection.connect()
28
61
        self.metrics = Metrics(connection, prefix)
29
62
 
 
63
    def _generate_files(self, testdir):
 
64
        
 
65
        current_path = self.root
 
66
 
 
67
        for node in testdir.split('/'):
 
68
            current_path = os.path.join(current_path, node)
 
69
            try:
 
70
                os.mkdir(current_path)
 
71
            except:
 
72
                pass
 
73
 
 
74
        rnd = open("/dev/urandom", "r")
 
75
 
 
76
        for file_id in range(self.file_count):
 
77
            path = os.path.join(current_path, u"file%d.bin" % (file_id,))
 
78
            if self.file_size_randomize:
 
79
                size = randint(0, self.file_size)
 
80
            elif self.file_size_decreasing:
 
81
                # ..200000 - 0,
 
82
                # to ensure we have various sizes
 
83
                size = self.file_size/self.file_count * (file_id + 1)
 
84
            else:
 
85
                size = self.file_size
 
86
 
 
87
            self.logger.debug("Creating %s (%d bytes)", path, size)
 
88
 
 
89
            self._file_created_cb(path, {
 
90
                'uploaded': False,
 
91
                'size': size,
 
92
                'upload_started': None
 
93
            })
 
94
 
 
95
            fh = open(path, "w")
 
96
            result = rnd.read(size)
 
97
            fh.write(result)
 
98
            fh.close()
 
99
 
 
100
        rnd.close()
 
101