aboutsummaryrefslogtreecommitdiff
path: root/bench_mysql.py
blob: 1e5c674d403b4c37bc88a47e6bb60cc9ab52f440 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import copy
import csv
import matplotlib.pyplot as plt
import multiprocessing
import numpy as np
import os
import pickle
import re
import shutil
import subprocess
from time import sleep

from benchmark import Benchmark
from common_targets import common_targets

cwd = os.getcwd()

prepare_cmd = ("sysbench oltp_read_only --db-driver=mysql --mysql-user=root "
              "--mysql-socket="+cwd+"/mysql_test/socket --table-size=1000000 prepare").split()

cmd = ("sysbench oltp_read_only --threads={} --time=10 "
       "--db-driver=mysql --mysql-user=root --mysql-socket={}/mysql_test/socket run")

server_cmd = ("mysqld -h {0}/mysql_test --socket={0}/mysql_test/socket "
             "--secure-file-priv=").format(cwd).split()


class Benchmark_MYSQL( Benchmark ):
    def __init__(self):
        self.file_name = "bench_mysql"
        self.name = "MYSQL Stress Benchmark"
        self.descrition = """See sysbench documentation."""
        self.targets = copy.copy(common_targets)
        del(self.targets["klmalloc"])
        self.nthreads = range(1, multiprocessing.cpu_count() * 2 + 1)

        self.results = {"args": {"nthreads" : self.nthreads},
                        "targets" : self.targets}

    def start_and_wait_for_server(self, verbose, log=None):
        if not log:
            log = os.devnull

        with open(log, "ab") as f:
            self.server = subprocess.Popen(server_cmd, env=os.environ,
                                               stdout=f,
                                               stderr=f,
                                               universal_newlines=True)
        #TODO make sure server comes up !!!!
        sleep(5)
        return True

    def prepare(self, verbose=False):
        ret = True
        # Setup mysqld
        if not os.path.exists("mysql_test"):
            print("Prepare mysqld directory and database")
            os.makedirs("mysql_test")

            # Init database
            if b"MariaDB" in subprocess.run(["mysqld", "--version"],
                                            stdout=subprocess.PIPE).stdout:
                init_db_cmd = ["mysql_install_db", "--basedir=/usr",
                                "--datadir={}/mysql_test".format(os.getcwd())]
                if verbose:
                    print("MariaDB detected")
            else:
                init_db_cmd = ["mysqld", "-h", "{}/mysql_test".format(os.getcwd()),
                                "--initialize-insecure"]
                if verbose:
                    print("Oracle MySQL detected")

            with open(os.devnull, "w") as devnull:
                p = subprocess.run(init_db_cmd,
                            stdout=devnull, stderr=devnull)
            ret = ret and p.returncode == 0
            if not ret:
                print(p.stderr)
                return ret

            if not self.start_and_wait_for_server(verbose, "mysqld.log"):
                print("Starting mysqld failed")
                return False

            p = subprocess.run("mysql -u root -S {}/mysql_test/socket".format(cwd).split(" "),
                input = b"CREATE DATABASE sbtest;\n")
            ret = ret and p.returncode == 0
            if not ret:
                print(p.stderr)
                self.server.kill()
                self.server.wait()
                return ret

            print("Prepare test table")
            p = subprocess.run(prepare_cmd)
            ret = ret == p.returncode == 0
            self.server.kill()
            ret = ret and self.server.wait() == -9

        return ret

    def cleanup(self):
        if os.path.exists("mysql_test"):
            print("Delete mysqld directory")
            shutil.rmtree("mysql_test")

    def run(self, verbose=False, save=False, runs=3):
        cwd = os.getcwd()
        for run in range(1, runs + 1):
            print(str(run) + ". run")

            # run cmd for each target
            n = len(self.nthreads)
            for tname, t in self.targets.items():
                # No custom build mysqld server supported yet.
                os.environ["LD_PRELOAD"] = t[1] # set LD_PRELOAD

                if not self.start_and_wait_for_server(verbose, "mysqld.log"):
                    print("Can't start server for", tname + ".")
                    print("Aborting Benchmark.")
                    return False

                for i, thread in enumerate(self.nthreads):
                    print(tname + ":", i + 1, "of", n, "\r", end='')

                    target_cmd = cmd.format(thread, cwd).split(" ")
                    p = subprocess.run(target_cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE,
                                        universal_newlines=True)

                    if p.returncode != 0:
                        print("\n" + " ".join(target_cmd), "exited with", p.returncode, ".\n Aborting Benchmark.")
                        print(tname, t)
                        print(p.stderr)
                        print(p.stdout)
                        self.server.kill()
                        self.server.wait()
                        return False

                    result = {}
                    result["transactions"] = re.search("transactions:\s*(\d*)", p.stdout).group(1)
                    result["queries"] = re.search("queries:\s*(\d*)", p.stdout).group(1)
                    # Latency
                    result["min"] = re.search("min:\s*(\d*.\d*)", p.stdout).group(1)
                    result["avg"] = re.search("avg:\s*(\d*.\d*)", p.stdout).group(1)
                    result["max"] = re.search("max:\s*(\d*.\d*)", p.stdout).group(1)

                    key = (tname, thread)
                    if not key in self.results:
                        self.results[key] = [result]
                    else:
                        self.results[key].append(result)

                print()

                # Get memory stats from server
                if "memusage" not in self.results:
                    self.results["memusage"] = {}

                ps = subprocess.run(["ps", "-F", str(self.server.pid)], stdout=subprocess.PIPE)
                tokens = str(ps.stdout.splitlines()[1]).split()
                self.results["memusage"][tname] = {"VSZ" : tokens[4], "RSS" : tokens[5]}

                self.server.kill()
                self.server.wait()

        if save:
            with open(self.name + ".save", "wb") as f:
                pickle.dump(self.results, f)
        return True

    def summary(self):
        # linear plot
        nthreads = self.results["args"]["nthreads"]
        targets = self.results["targets"]
        y_mapping = {v: i for i, v in enumerate(nthreads)}

        for target in targets:
            y_vals = [0] * len(nthreads)
            for mid, measures in self.results.items():
                if mid[0] == target:
                    d = []
                    for m in measures:
                        d.append(int(m["transactions"]))
                    y_vals[y_mapping[mid[1]]] = np.mean(d)
            plt.plot(nthreads, y_vals, label=target, linestyle='-', marker='.')

        plt.legend()
        plt.xlabel("threads")
        plt.ylabel("transactions")
        plt.title("sysbench oltp read only")
        plt.savefig(self.file_name + ".l.ro.png")
        plt.clf()

        # bar plot
        nthreads = list(self.results["args"]["nthreads"])
        targets = self.results["targets"]
        y_mapping = {v: i for i, v in enumerate(nthreads)}

        for i, target in enumerate(targets):
            y_vals = [0] * len(nthreads)
            for mid, measures in self.results.items():
                if mid[0] == target:
                    d = []
                    for m in measures:
                        d.append(int(m["transactions"]))
                    y_vals[y_mapping[mid[1]]] = np.mean(d)
            plt.bar([x-i/8 for x in range(1, len(nthreads) + 1)], y_vals, width=0.2, label=target, align="center")

        plt.legend()
        plt.xlabel("threads")
        plt.xticks(range(1, len(nthreads) + 1), nthreads)
        plt.ylabel("transactions")
        plt.title("sysbench oltp read only")
        plt.savefig(self.file_name + ".b.ro.png")
        plt.clf()

mysql = Benchmark_MYSQL()