blob: 968ba5e878d676de2f386cb7127b3c5d3a323137 [file] [log] [blame]
mblighafce1032007-10-31 21:29:29 +00001import test, time, re, pwd
2from autotest_utils import *
3
4class sysbench(test.test):
5 version = 1
6
7 # http://osdn.dl.sourceforge.net/sourceforge/sysbench/sysbench-0.4.8.tar.gz
8 def setup(self, tarball = 'sysbench-0.4.8.tar.bz2'):
9 tarball = unmap_url(self.bindir, tarball, self.tmpdir)
10 extract_tarball_to_dir(tarball, self.srcdir)
11 self.job.setup_dep(['pgsql', 'mysql'])
12
13 os.chdir(self.srcdir)
14
15 pgsql_dir = os.path.join(self.autodir, 'deps/pgsql/pgsql')
16 mysql_dir = os.path.join(self.autodir, 'deps/mysql/mysql')
17
18 # configure wants to get at pg_config, so add its path
19 system('PATH=%s/bin:$PATH ./configure --with-mysql=%s --with-pgsql' % (pgsql_dir, mysql_dir))
20 system('make -j %d' % count_cpus())
21
22
23 def execute(self, db_type = 'pgsql', build = 1, \
24 num_threads = count_cpus(), max_time = 60, \
25 read_only = 0, args = ''):
26 plib = os.path.join(self.autodir, 'deps/pgsql/pgsql/lib')
27 mlib = os.path.join(self.autodir, 'deps/mysql/mysql/lib/mysql')
28 ld_path = prepend_path(plib, environ('LD_LIBRARY_PATH'))
29 ld_path = prepend_path(mlib, ld_path)
30 os.environ['LD_LIBRARY_PATH'] = ld_path
31
32 # The databases don't want to run as root so run them as nobody
33 self.dbuser = 'nobody'
34 self.dbuid = pwd.getpwnam(self.dbuser)[2]
35 self.sudo = 'sudo -u ' + self.dbuser + ' '
36
37 # Check for nobody user
38 try:
39 system(self.sudo + '/bin/true')
40 except:
mbligh642b03e2008-01-14 16:53:15 +000041 raise TestError('Unable to run as nobody')
mblighafce1032007-10-31 21:29:29 +000042
43 if (db_type == 'pgsql'):
44 self.execute_pgsql(build, num_threads, max_time, \
45 read_only, args)
46 elif (db_type == 'mysql'):
47 self.execute_mysql(build, num_threads, max_time, \
48 read_only, args)
49
50
51 def execute_pgsql(self, build, num_threads, max_time, read_only, args):
52 bin = os.path.join(self.autodir, 'deps/pgsql/pgsql/bin')
53 data = os.path.join(self.autodir, 'deps/pgsql/pgsql/data')
54 log = os.path.join(self.debugdir, 'pgsql.log')
55
56 if build == 1:
57 system('rm -rf ' + data)
58 os.mkdir(data)
59 os.chown(data, self.dbuid, 0)
60 system(self.sudo + bin + '/initdb -D ' + data)
61
62 # Database must be able to write its output into debugdir
63 os.chown(self.debugdir, self.dbuid, 0)
64 system(self.sudo + bin + '/pg_ctl -D ' + data + \
65 ' -l ' + log + ' start')
66
67 # Wait for database to start
68 time.sleep(5)
69
70 try:
71 base_cmd = self.srcdir + '/sysbench/sysbench ' + \
72 '--test=oltp --db-driver=pgsql ' + \
73 '--pgsql-user=' + self.dbuser
74
75 if build == 1:
76 system(self.sudo + bin + '/createdb sbtest')
77 cmd = base_cmd +' prepare'
78 system(cmd)
79
80 cmd = base_cmd + \
81 ' --num-threads=' + str(num_threads) + \
82 ' --max-time=' + str(max_time) + \
83 ' --max-requests=0'
84
85 if read_only:
86 cmd = cmd + ' --oltp-read-only=on'
87
88 profilers = self.job.profilers
mbligh642b03e2008-01-14 16:53:15 +000089 if not profilers.only():
jadmanskicc1fafd2008-05-13 19:41:02 +000090 results = system_output(cmd + ' run') + '\n'
mblighafce1032007-10-31 21:29:29 +000091
92 # Do a profiling run if necessary
93 if profilers.present():
94 profilers.start(self)
jadmanskicc1fafd2008-05-13 19:41:02 +000095 results = "Profiling run ...\n"
96 results += system_output(cmd + ' run') + '\n'
mblighafce1032007-10-31 21:29:29 +000097 profilers.stop(self)
98 profilers.report(self)
99 except:
100 system(self.sudo + bin + '/pg_ctl -D ' + data + ' stop')
mblighec755762008-01-17 16:25:22 +0000101 raise
mblighafce1032007-10-31 21:29:29 +0000102
jadmanskicc1fafd2008-05-13 19:41:02 +0000103 print results
mblighafce1032007-10-31 21:29:29 +0000104 system(self.sudo + bin + '/pg_ctl -D ' + data + ' stop')
105
jadmanskicc1fafd2008-05-13 19:41:02 +0000106 self.__format_results(results)
mblighafce1032007-10-31 21:29:29 +0000107
108
109 def execute_mysql(self, build, num_threads, max_time, read_only, args):
110 bin = os.path.join(self.autodir, 'deps/mysql/mysql/bin')
111 data = os.path.join(self.autodir, 'deps/mysql/mysql/var')
112 log = os.path.join(self.debugdir, 'mysql.log')
113
114 if build == 1:
115 system('rm -rf ' + data)
116 os.mkdir(data)
117 os.chown(data, self.dbuid, 0)
118 system(bin + '/mysql_install_db --user=' + self.dbuser)
119
120 system(bin + '/mysqld_safe --log-error=' + log + \
121 ' --user=' + self.dbuser + ' &')
122
123 # Wait for database to start
124 time.sleep(5)
125
126 try:
127 base_cmd = self.srcdir + '/sysbench/sysbench ' + \
128 '--test=oltp --db-driver=mysql ' + \
129 '--mysql-user=root'
130
131 if build == 1:
132 system('echo "create database sbtest" | ' + \
133 bin + '/mysql -u root')
134 cmd = base_cmd +' prepare'
135 system(cmd)
136
137 cmd = base_cmd + \
138 ' --num-threads=' + str(num_threads) + \
139 ' --max-time=' + str(max_time) + \
140 ' --max-requests=0'
141
142 if read_only:
143 cmd = cmd + ' --oltp-read-only=on'
144
145 profilers = self.job.profilers
146 if not profilers.only():
jadmanskicc1fafd2008-05-13 19:41:02 +0000147 results = system(cmd + ' run') + '\n'
mblighafce1032007-10-31 21:29:29 +0000148
149 # Do a profiling run if necessary
150 if profilers.present():
151 profilers.start(self)
jadmanskicc1fafd2008-05-13 19:41:02 +0000152 results = "Profiling run ...\n"
153 results += system_output(cmd + ' run') + '\n'
mblighafce1032007-10-31 21:29:29 +0000154 profilers.stop(self)
155 profilers.report(self)
156 except:
157 system(bin + '/mysqladmin shutdown')
mblighec755762008-01-17 16:25:22 +0000158 raise
mblighafce1032007-10-31 21:29:29 +0000159
jadmanskicc1fafd2008-05-13 19:41:02 +0000160 print results
mblighafce1032007-10-31 21:29:29 +0000161 system(bin + '/mysqladmin shutdown')
162
jadmanskicc1fafd2008-05-13 19:41:02 +0000163 self.__format_results(results)
mblighafce1032007-10-31 21:29:29 +0000164
165
166 def __format_results(self, results):
167 threads = 0
168 tps = 0
169
170 out = open(self.resultsdir + '/keyval', 'w')
171 for line in results.split('\n'):
172 threads_re = re.search('Number of threads: (\d+)', line)
173 if threads_re:
174 threads = threads_re.group(1)
175
176 tps_re = re.search('transactions:\s+\d+\s+\((\S+) per sec.\)', line)
177 if tps_re:
178 tps = tps_re.group(1)
179 break
180
181 print >> out, 'threads=%s\ntps=%s' % (threads, tps)
182 out.close()
183