~hadware/magicicada-server/trusty-support

« back to all changes in this revision

Viewing changes to src/backends/db/scripts/dev/dal-benchmark.py

  • Committer: Facundo Batista
  • Date: 2015-08-05 13:10:02 UTC
  • Revision ID: facundo@taniquetil.com.ar-20150805131002-he7b7k704d8o7js6
First released version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008-2015 Canonical
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as
 
5
# published by the Free Software Foundation, either version 3 of the
 
6
# License, or (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
15
#
 
16
# For further info, check  http://launchpad.net/filesync-server
 
17
 
 
18
"""Benchmark script for the DAL."""
 
19
 
 
20
import uuid
 
21
from datetime import datetime
 
22
 
 
23
from ubuntuone.storageprotocol import content_hash
 
24
from optparse import OptionParser
 
25
 
 
26
from backends.filesync.notifier.notifier import register_notifier_for_bus
 
27
from backends.filesync.notifier.testing.testcase import AccumulatingNotifyBus
 
28
from backends.filesync.data.services import make_storage_user
 
29
 
 
30
MIME_TYPES = [
 
31
    u'audio/mp3',
 
32
    u'audio/mpg',
 
33
    u'audio/mpeg',
 
34
    u'audio/mpeg3',
 
35
    u'audio/x-mp3',
 
36
    u'audio/x-mpeg',
 
37
    u'application/mp3',
 
38
    # OGG
 
39
    u'audio/ogg',
 
40
    u'audio/x-vorbis+ogg',
 
41
    u'application/ogg',
 
42
    # itunes
 
43
    u'audio/mp4a-latm',
 
44
    u'audio/mp4',
 
45
    u'audio/x-m4a',
 
46
]
 
47
 
 
48
 
 
49
def create_user(shard_id):
 
50
    """Create a new random user."""
 
51
    id = datetime.utcnow().toordinal()
 
52
    username = unicode(uuid.uuid4())
 
53
    return make_storage_user(id, username, u"", 2 * (2 ** 30),
 
54
                             shard_id=shard_id)
 
55
 
 
56
 
 
57
def do_fileops(bench, base, sufix):
 
58
    """Do a bunch of file operations for a directory."""
 
59
    with bench("makefile_" + sufix):
 
60
        file = base.make_file(unicode(uuid.uuid4()))
 
61
    with bench("makedir_" + sufix):
 
62
        dir = base.make_subdirectory(unicode(uuid.uuid4()))
 
63
 
 
64
    with bench("delfile_" + sufix):
 
65
        file.delete()
 
66
 
 
67
    with bench("deldir_" + sufix):
 
68
        dir.delete()
 
69
 
 
70
    with bench("5050_flat_" + sufix):
 
71
        for i in range(50):
 
72
            base.make_file(unicode(uuid.uuid4()))
 
73
            base.make_subdirectory(unicode(uuid.uuid4()))
 
74
 
 
75
    with bench("getchildren_" + sufix):
 
76
        base.get_children()
 
77
 
 
78
    first = dir = base.make_subdirectory(unicode(uuid.uuid4()))
 
79
    with bench("5050_nested_" + sufix):
 
80
        for i in range(50):
 
81
            dir.make_file(unicode(uuid.uuid4()))
 
82
            dir = dir.make_subdirectory(unicode(uuid.uuid4()))
 
83
 
 
84
    first.make_file(unicode(uuid.uuid4()))
 
85
    with bench("getchildren_sparse_" + sufix):
 
86
        first.get_children()
 
87
 
 
88
    hash = content_hash.content_hash_factory()
 
89
    hash.update(str(uuid.uuid4()))
 
90
    hashstr = hash.content_hash()
 
91
    storage_key = uuid.uuid4()
 
92
 
 
93
    with bench("make_with_content_" + sufix):
 
94
        base.make_file_with_content(unicode(uuid.uuid4()), hashstr, 100, 100,
 
95
                                    12, storage_key)
 
96
    with bench("make_with_content_repeated_" + sufix):
 
97
        base.make_file_with_content(unicode(uuid.uuid4()), hashstr, 100, 100,
 
98
                                    12, storage_key)
 
99
 
 
100
 
 
101
def benchmark_one(bench, shard_id=None):
 
102
    """Run a benchmark on a bunch of things."""
 
103
    user = create_user(shard_id)
 
104
    with bench("getroot"):
 
105
        user.root._load()
 
106
 
 
107
    volume = user.volume()
 
108
 
 
109
    with bench("make_random_dir"):
 
110
        root_base = user.root.make_subdirectory(unicode(uuid.uuid4()))
 
111
 
 
112
    udf = user.make_udf("~/" + unicode(uuid.uuid4()))
 
113
    udf_dir = user.volume(udf.id).get_root().make_subdirectory(
 
114
        unicode(uuid.uuid4()))
 
115
    for base, sufix in [(root_base, "root"),
 
116
                        (udf_dir, "udf")]:
 
117
        do_fileops(bench, base, sufix)
 
118
 
 
119
    with bench("getvolume"):
 
120
        generation = volume.get_volume().generation
 
121
 
 
122
    with bench("deltafull"):
 
123
        volume.get_delta(0)
 
124
 
 
125
    with bench("deltaempty"):
 
126
        volume.get_delta(generation)
 
127
 
 
128
    with bench("getmusic"):
 
129
        volume.get_all_nodes(mimetypes=MIME_TYPES)
 
130
 
 
131
    with bench("get_shared_volumes"):
 
132
        user.get_share_volumes()
 
133
 
 
134
    with bench("get_udfs"):
 
135
        user.get_udfs()
 
136
 
 
137
 
 
138
if __name__ == "__main__":
 
139
    parser = OptionParser()
 
140
    parser.add_option("--shard_id", dest="shard_id", default=None,
 
141
                      help="The shard to run the test against.")
 
142
    options, args = parser.parse_args()
 
143
 
 
144
    import os
 
145
    from stormbench.benchmark import run_benchmark
 
146
    from stormbench.reporter import generate_report
 
147
 
 
148
    nb = AccumulatingNotifyBus()
 
149
    register_notifier_for_bus(nb)
 
150
    shard_id = options.shard_id
 
151
    timestampdir = datetime.now().strftime('%Y%m%d%H%M%S')
 
152
    root_dir = 'tmp/stormbench'
 
153
    if not os.path.exists(root_dir):
 
154
        os.makedirs(root_dir)
 
155
    datafile = os.path.join(root_dir, "%s.dat" % timestampdir)
 
156
    reports_dir = os.path.join(root_dir, 'reports', timestampdir)
 
157
    outfile = run_benchmark(benchmark_one, datafile, shard_id=shard_id)
 
158
    generate_report(outfile, reports_dir)