1
# Copyright 2008-2015 Canonical
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.
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.
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/>.
16
# For further info, check http://launchpad.net/filesync-server
18
"""Benchmark script for the DAL."""
21
from datetime import datetime
23
from ubuntuone.storageprotocol import content_hash
24
from optparse import OptionParser
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
40
u'audio/x-vorbis+ogg',
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),
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()))
64
with bench("delfile_" + sufix):
67
with bench("deldir_" + sufix):
70
with bench("5050_flat_" + sufix):
72
base.make_file(unicode(uuid.uuid4()))
73
base.make_subdirectory(unicode(uuid.uuid4()))
75
with bench("getchildren_" + sufix):
78
first = dir = base.make_subdirectory(unicode(uuid.uuid4()))
79
with bench("5050_nested_" + sufix):
81
dir.make_file(unicode(uuid.uuid4()))
82
dir = dir.make_subdirectory(unicode(uuid.uuid4()))
84
first.make_file(unicode(uuid.uuid4()))
85
with bench("getchildren_sparse_" + sufix):
88
hash = content_hash.content_hash_factory()
89
hash.update(str(uuid.uuid4()))
90
hashstr = hash.content_hash()
91
storage_key = uuid.uuid4()
93
with bench("make_with_content_" + sufix):
94
base.make_file_with_content(unicode(uuid.uuid4()), hashstr, 100, 100,
96
with bench("make_with_content_repeated_" + sufix):
97
base.make_file_with_content(unicode(uuid.uuid4()), hashstr, 100, 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"):
107
volume = user.volume()
109
with bench("make_random_dir"):
110
root_base = user.root.make_subdirectory(unicode(uuid.uuid4()))
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"),
117
do_fileops(bench, base, sufix)
119
with bench("getvolume"):
120
generation = volume.get_volume().generation
122
with bench("deltafull"):
125
with bench("deltaempty"):
126
volume.get_delta(generation)
128
with bench("getmusic"):
129
volume.get_all_nodes(mimetypes=MIME_TYPES)
131
with bench("get_shared_volumes"):
132
user.get_share_volumes()
134
with bench("get_udfs"):
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()
145
from stormbench.benchmark import run_benchmark
146
from stormbench.reporter import generate_report
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)