Difference between revisions of "Mysqlslap: cara melakukan pengukuran"

From OnnoWiki
Jump to navigation Jump to search
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
MySQL comes with a handy little diagnostic tool called mysqlslap that's been around since version 5.1.4. It's a benchmarking tool that can help DBAs and developers load test their database servers.
+
mysqlslap dapat meng-emulasi sejumlah besar koneksi klien meng-hit server database pada saat yang sama. Parameter pengujian dapat beban sepenuhnya dikonfigurasi dan hasil dari tes berjalan yang berbeda dapat digunakan untuk menyempurnakan desain database atau sumber daya perangkat keras.
  
mysqlslap can emulate a large number of client connections hitting the database server at the same time. The load testing parameters are fully configurable and the results from different test runs can be used to fine-tune database design or hardware resources.
+
Dalam tutorial ini kita akan belajar bagaimana menggunakan mysqlslap untuk menguji beban database MySQL dengan beberapa query dasar dan melihat bagaimana benchmarking dapat membantu kita dalam menyempurnakan query tersebut. Setelah beberapa demonstrasi dasar, kita akan menjalankan melalui skenario pengujian yang cukup realistis di mana kita membuat copy dari database yang sudah ada untuk pengujian, mengumpulkan pertanyaan dari log, dan menjalankan tes dari script.
  
In this tutorial we will learn how to use mysqlslap to load test a MySQL database with some basic queries and see how benchmarking can help us fine-tune those queries. After some basic demonstrations, we will run through a fairly realistic test scenario where we create a copy of an existing database for testing, glean queries from a log, and run the test from a script.
+
Jika anda tertarik untuk pembandingan server database tertentu, anda harus menguji pada server dengan spesifikasi yang sama dan dengan salinan dari database anda yang terinstal.
  
The commands, packages, and files shown in this tutorial were tested on CentOS 7. The concepts remain the same for other distributions.
+
Jika anda ingin menjalankan melalui tutorial ini untuk tujuan belajar dan melaksanakan setiap perintah di dalamnya, kami sarankan setidaknya 2 GB Droplet. Karena perintah dalam tutorial ini dimaksudkan untuk memaksa server, anda mungkin menemukan mereka akan time out pada server yang lebih kecil.
What size server should I use?
 
  
If you're interested in benchmarking a specific database server, you should test on a server with the same specifications and with an exact copy of your database installed.
+
Sampel output dalam tutorial ini diproduksi dalam berbagai cara untuk mengoptimalkan contoh untuk mengajar.
 
 
If you want to run through this tutorial for learning purposes and execute every command in it, we recommend at least a 2 GB Droplet. As the commands in this tutorial are meant to tax the server, you may find that they time out on a smaller server.
 
 
 
The sample output in this tutorial was produced in a variety of ways to optimize the examples for teaching.
 
  
 
==Langkah Pertama - Install MySQL di sistem yang akan di test==
 
==Langkah Pertama - Install MySQL di sistem yang akan di test==
Line 19: Line 14:
  
 
  apt-get update
 
  apt-get update
  apt-get install mysql-server mysql-client
+
  apt-get install -y mysql-server mysql-client
  
 
Biasanya akan di tanya root password.
 
Biasanya akan di tanya root password.
Line 57: Line 52:
 
==Langkah Kedua - Install Sample Database==
 
==Langkah Kedua - Install Sample Database==
  
Next, we need to install a sample database for testing. This database is called employees and it's freely accessible from the MySQL web site. The database can also be downloaded from Launchpad. The employees database was developed by Patrick Crews and Giuseppe Maxia. The original data was created by Fusheng Wang and Carlo Zaniolo at Siemens Corporate Research.
 
  
We are choosing the employees database because it features a large data set. The database structure is simple enough: it's got only six tables; but the data it contains has more than 3,000,000 employee records (the salaries table itself has nearly three million rows). This will help us emulate a more realistic production workload.
+
Kita perlu mengambil contoh database untuk test.
  
First, let's make sure we're in the /mysqlslap_tutorial directory:
+
apt-get install bzip2
 +
mkdir -p /mysqlslap_tutorial
 +
cd /mysqlslap_tutorial
 +
sudo wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2
 +
sudo bzip2 -dfv employees_db-full-1.0.6.tar.bz2
 +
sudo tar -xf employees_db-full-1.0.6.tar
  
cd /mysqlslap_tutorial
+
Kita menggunakan database employees dengan 3 juta entry jadi cukup mendekati kondisi di lapangan.
  
Download the latest version of the employees sample database:
+
cd /mysqlslap_tutorial/employees_db
 +
ls -lh
  
sudo wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2
+
total 161M
 +
-rw-r--r-- 1 501 staff  752 Mar 30  2009 Changelog
 +
-rw-r--r-- 1 501 staff 6,4K Okt  9  2008 employees_partitioned2.sql
 +
-rw-r--r-- 1 501 staff 7,5K Peb  6  2009 employees_partitioned3.sql
 +
-rw-r--r-- 1 501 staff 5,6K Peb  6  2009 employees_partitioned.sql
 +
-rw-r--r-- 1 501 staff 3,8K Nov 28  2008 employees.sql
 +
-rw-r--r-- 1 501 staff  241 Jul 30  2008 load_departments.dump
 +
-rw-r--r-- 1 501 staff  14M Mar 30  2009 load_dept_emp.dump
 +
-rw-r--r-- 1 501 staff 1,1K Jul 30  2008 load_dept_manager.dump
 +
-rw-r--r-- 1 501 staff  17M Jul 30  2008 load_employees.dump
 +
-rw-r--r-- 1 501 staff 111M Jul 30  2008 load_salaries.dump
 +
-rw-r--r-- 1 501 staff  21M Jul 30  2008 load_titles.dump
 +
-rw-r--r-- 1 501 staff 3,8K Mar 30  2009 objects.sql
 +
-rw-r--r-- 1 501 staff 2,2K Jul 30  2008 README
 +
-rw-r--r-- 1 501 staff 4,4K Mar 30  2009 test_employees_md5.sql
 +
-rw-r--r-- 1 501 staff 4,4K Mar 30  2009 test_employees_sha.sql
  
Install the bzip2 tool so we can unzip the archive:
 
  
sudo yum install bzip2
+
Masukan database tersebut ke mysql
  
Unzip the database archive. This will take a minute. We are doing it in two steps here:
+
cd /mysqlslap_tutorial/employees_db
 +
sudo mysql -u root -p123456 -t < employees.sql
  
sudo bzip2 -dfv employees_db-full-1.0.6.tar.bz2
+
Akan muncul
sudo tar -xf employees_db-full-1.0.6.tar
 
  
The contents will be uncompressed into a separate, new directory called employees_db. We need to navigate into this directory to run the query that installs the database. The contents include a README document, a change log, data dumps, and various SQL query files that will create the database structures:
+
+-----------------------------+
 +
| INFO                        |
 +
+-----------------------------+
 +
| CREATING DATABASE STRUCTURE |
 +
+-----------------------------+
 +
+------------------------+
 +
| INFO                  |
 +
+------------------------+
 +
| storage engine: InnoDB |
 +
+------------------------+
 +
+---------------------+
 +
| INFO                |
 +
+---------------------+
 +
| LOADING departments |
 +
+---------------------+
 +
+-------------------+
 +
| INFO              |
 +
+-------------------+
 +
| LOADING employees |
 +
+-------------------+
 +
+------------------+
 +
| INFO            |
 +
+------------------+
 +
| LOADING dept_emp |
 +
+------------------+
 +
+----------------------+
 +
| INFO                |
 +
+----------------------+
 +
| LOADING dept_manager |
 +
+----------------------+
 +
+----------------+
 +
| INFO          |
 +
+----------------+
 +
| LOADING titles |
 +
+----------------+
 +
+------------------+
 +
| INFO            |
 +
+------------------+
 +
| LOADING salaries |
 +
+------------------+
  
cd employees_db
+
Login ke MySQL dan cek tabel / data yang ada
ls -l
 
  
Here's what you should see:
+
mysql -u root -p123456
  
-rw-r--r--. 1 501 games      752 Mar 30  2009 Changelog
+
Lakukan
-rw-r--r--. 1 501 games      6460 Oct  9  2008 employees_partitioned2.sql
 
-rw-r--r--. 1 501 games      7624 Feb  6  2009 employees_partitioned3.sql
 
-rw-r--r--. 1 501 games      5660 Feb  6  2009 employees_partitioned.sql
 
-rw-r--r--. 1 501 games      3861 Nov 28  2008 employees.sql
 
-rw-r--r--. 1 501 games      241 Jul 30  2008 load_departments.dump
 
-rw-r--r--. 1 501 games  13828291 Mar 30  2009 load_dept_emp.dump
 
-rw-r--r--. 1 501 games      1043 Jul 30  2008 load_dept_manager.dump
 
-rw-r--r--. 1 501 games  17422825 Jul 30  2008 load_employees.dump
 
-rw-r--r--. 1 501 games 115848997 Jul 30  2008 load_salaries.dump
 
-rw-r--r--. 1 501 games  21265449 Jul 30  2008 load_titles.dump
 
-rw-r--r--. 1 501 games      3889 Mar 30  2009 objects.sql
 
-rw-r--r--. 1 501 games      2211 Jul 30  2008 README
 
-rw-r--r--. 1 501 games      4455 Mar 30  2009 test_employees_md5.sql
 
-rw-r--r--. 1 501 games      4450 Mar 30  2009 test_employees_sha.sql
 
  
Run this command to connect to MySQL and run the employees.sql script, which will create the database and load the data:
+
show databases;
  
sudo mysql -h localhost -u sysadmin -p -t < employees.sql
+
Output
  
At the prompt, enter the password you created for the sysadmin MySQL user in the previous section.
+
+--------------------+
 +
| Database          |
 +
+--------------------+
 +
| information_schema |
 +
| employees          |
 +
| mysql              |
 +
| performance_schema |
 +
+--------------------+
 +
4 rows in set (0.00 sec)
  
The process output will look like this. It will take a minute or so to run:
 
  
+-----------------------------+
+
use employees;
| INFO                        |
+
show tables;
+-----------------------------+
 
| CREATING DATABASE STRUCTURE |
 
+-----------------------------+
 
+------------------------+
 
| INFO                  |
 
+------------------------+
 
| storage engine: InnoDB |
 
+------------------------+
 
+---------------------+
 
| INFO                |
 
+---------------------+
 
| LOADING departments |
 
+---------------------+
 
+-------------------+
 
| INFO              |
 
+-------------------+
 
| LOADING employees |
 
+-------------------+
 
+------------------+
 
| INFO            |
 
+------------------+
 
| LOADING dept_emp |
 
+------------------+
 
+----------------------+
 
| INFO                |
 
+----------------------+
 
| LOADING dept_manager |
 
+----------------------+
 
+----------------+
 
| INFO          |
 
+----------------+
 
| LOADING titles |
 
+----------------+
 
+------------------+
 
| INFO            |
 
+------------------+
 
| LOADING salaries |
 
+------------------+
 
 
 
Now you can log into MySQL and run some basic queries to check that the data was imported successfully.
 
 
 
sudo mysql -h localhost -u sysadmin -p
 
 
 
Enter the password for the sysadmin MySQL user.
 
 
 
Check the list of databases for the new employees database:
 
 
 
show databases;
 
  
 
Output:
 
Output:
  
+--------------------+
+
+---------------------+
| Database          |
+
| Tables_in_employees |
+--------------------+
+
+---------------------+
| information_schema |
+
| departments        |
| employees         |
+
| dept_emp            |
| mysql              |
+
| dept_manager        |
| performance_schema |
+
| employees           |
+--------------------+
+
| salaries            |
4 rows in set (0.01 sec)
+
| titles              |
 
+
+---------------------+
Use the employees database:
+
6 rows in set (0.00 sec)
 
 
use employees;
 
  
Check the tables in it:
 
  
show tables;
+
describe titles;
  
 
Output:
 
Output:
  
+---------------------+
+
+-----------+-------------+------+-----+---------+-------+
| Tables_in_employees |
+
| Field    | Type        | Null | Key | Default | Extra |
+---------------------+
+
+-----------+-------------+------+-----+---------+-------+
| departments        |
+
| emp_no    | int(11)    | NO  | PRI | NULL    |       |
| dept_emp            |
+
| title    | varchar(50) | NO  | PRI | NULL    |      |
| dept_manager       |
+
| from_date | date       | NO  | PRI | NULL    |      |
| employees          |
+
| to_date  | date        | YES  |     | NULL    |       |
| salaries            |
+
+-----------+-------------+------+-----+---------+-------+
| titles              |
+
4 rows in set (0.00 sec)
+---------------------+
 
6 rows in set (0.01 sec)
 
  
If you want to, you can check the details for each of these tables. We'll just check the information for the titles table:
 
  
describe titles;
+
select count(*) from titles;
  
 
Output:
 
Output:
  
+-----------+-------------+------+-----+---------+-------+
+
+----------+
| Field    | Type        | Null | Key | Default | Extra |
+
| count(*) |
+-----------+-------------+------+-----+---------+-------+
+
+----------+
| emp_no    | int(11)    | NO   | PRI | NULL    |      |
+
443308 |
| title    | varchar(50) | NO  | PRI | NULL    |      |
+
  +----------+
| from_date | date        | NO  | PRI | NULL    |      |
+
1 row in set (0.14 sec)
| to_date  | date        | YES |    | NULL    |      |
 
+-----------+-------------+------+-----+---------+-------+
 
4 rows in set (0.01 sec)
 
  
Check the number of entries:
 
  
mysql> select count(*) from titles;
 
+----------+
 
| count(*) |
 
+----------+
 
|  443308 |
 
+----------+
 
1 row in set (0.14 sec)
 
  
Check any of the other data you want. We can now go back to our operating system prompt:
+
select count(*) from employees;
  
quit;
+
Output:
  
Step Three — Using mysqlslap
+
+----------+
 +
| count(*) |
 +
+----------+
 +
|  300024 |
 +
+----------+
 +
1 row in set (0.11 sec)
  
We can now start using mysqlslap. mysqlslap can be invoked from a regular shell prompt so there's no need to explicitly log in to MySQL. For this tutorial, though, we will open another terminal connection to our Linux server and start a new MySQL session from there with the sysadmin user we created before, so we can check and update a few things in MySQL more easily. So, we'll have one prompt open with our sudo user, and one prompt logged into MySQL.
 
  
Before we get into specific commands for testing, you may want to take a look at this list of the most useful mysqlslap options. This can help you design your own mysqlslap commands later.
+
Cek data-data lainnya, untuk keluar ketik
Option What it means
 
--user MySQL username to connect to the database server
 
--password Password for the user account. It's best to leave it blank in command line
 
--host MySQL database server name
 
--port Port number for connecting to MySQL if the default is not used
 
--concurrency The number of simultaneous client connections mysqlslap will emulate
 
--iterations The number of times the test query will be run
 
--create-schema The database where the query will be run
 
--query The query to execute. This can either be a SQL query string or a path to a SQL script file
 
--create The query to create a table. Again, this can be a query string or a path to a SQL file
 
--delimiter The delimiter used to separate multiple SQL statements
 
--engine The MySQL database engine to use (e.g., InnoDB)
 
--auto-generate-sql Lets MySQL perform load testing with its own auto-generated SQL command
 
Use Case: Benchmarking with Auto-generated SQL and Data
 
  
We will begin by using mysqlslap's auto-generate-sql feature. When we use auto-generated SQL, mysqlslap will create a separate temporary database - aptly called mysqlslap. This database will have a simple table in it with one integer and one varchar type column populated with sample data. This can be a quick and easy way to check the overall performance of the database server.
+
quit;
  
We start by testing a single client connection doing one iteration of an auto-generated SQL:
+
==Langkah ketiga - Menggunakan mysqlslap==
  
sudo mysqlslap --user=sysadmin --password --host=localhost  --auto-generate-sql --verbose
+
Opsi / Fitur mysqlslap bisa di peroleh dari
  
The output should look like this:
+
mysqlslap --help
  
Benchmark
+
Beberapa yang penting
        Average number of seconds to run all queries: 0.009 seconds
+
        Minimum number of seconds to run all queries: 0.009 seconds
+
--user MySQL username to connect to the database server
        Maximum number of seconds to run all queries: 0.009 seconds
+
--password Password for the user account. It's best to leave it blank in command line
        Number of clients running queries: 1
+
--host MySQL database server name
        Average number of queries per client: 0
+
--port Port number for connecting to MySQL if the default is not used
 +
--concurrency The number of simultaneous client connections mysqlslap will emulate
 +
--iterations The number of times the test query will be run
 +
--create-schema The database where the query will be run
 +
--query The query to execute. This can either be a SQL query string or a path to a SQL script file
 +
--create The query to create a table. Again, this can be a query string or a path to a SQL file
 +
--delimiter The delimiter used to separate multiple SQL statements
 +
--engine The MySQL database engine to use (e.g., InnoDB)
 +
--auto-generate-sql Lets MySQL perform load testing with its own auto-generated SQL command
  
mysqlslap reports a few benchmarking statistics as shown in the output. It reports the average, minimum, and maximum number of seconds it took to run the query. We can also see that the number of client connections used for this load test was one.
+
===CONTOH: Benchmark menggunakan Auto-generated SQL dan Data===
  
Now try 50 concurrent connections, and have the auto-generated query run 10 times:
+
sudo mysqlslap --user=root --password=123456 --host=localhost  --auto-generate-sql --verbose
  
sudo mysqlslap --user=sysadmin --password --host=localhost  --concurrency=50 --iterations=10 --auto-generate-sql --verbose
+
Output:
  
What this command means is that fifty simulated client connections will each throw the same test query at the same time, and this test will be repeated ten times.
+
Benchmark
 +
Average number of seconds to run all queries: 0.009 seconds
 +
Minimum number of seconds to run all queries: 0.009 seconds
 +
Maximum number of seconds to run all queries: 0.009 seconds
 +
Number of clients running queries: 1
 +
Average number of queries per client: 0
  
The output shows us a marked difference with the increased load:
 
  
Benchmark
+
Coba 50 concurrent connections, gunakan auto-generated query run 10 kali:
        Average number of seconds to run all queries: 0.197 seconds
 
        Minimum number of seconds to run all queries: 0.168 seconds
 
        Maximum number of seconds to run all queries: 0.399 seconds
 
        Number of clients running queries: 50
 
        Average number of queries per client: 0
 
  
Note how the Number of clients running queries: field is now showing a value of 50. The average number of queries per client is zero.
+
sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=50 --iterations=10 --auto-generate-sql --verbose
  
Auto-generated SQL creates a simple table with two fields. In most production environments the table structures will be much larger than that. We can instruct mysqlslap to emulate this by adding additional fields to the test table. To do so, we can make use of two new parameters: --number-char-cols and --number-int-cols. These parameters specify the number of varchar and int types of columns to add to the test table.
+
Benchmark
 +
Average number of seconds to run all queries: 0.137 seconds
 +
Minimum number of seconds to run all queries: 0.117 seconds
 +
Maximum number of seconds to run all queries: 0.159 seconds
 +
Number of clients running queries: 50
 +
Average number of queries per client: 0
  
In the following example, we are testing with an auto-generated SQL query that runs against a table with 5 numeric columns and 20 character type columns. We are also simulating 50 client connections and we want the test to repeat 100 times:
 
  
sudo mysqlslap --user=sysadmin --password --host=localhost  --concurrency=50 --iterations=100 --number-int-cols=5 --number-char-cols=20 --auto-generate-sql --verbose
+
Coba menggqunakan auto-generated SQL query untuk membuat tabel dengan 5 numeric kolom dan 20 character setiap kolom.
 +
Kita akan mensimulasi 50 client connection dan test diulangi 100 kali,
  
This one should take a bit longer. While the test is running, we can switch to the other terminal window where we have a MySQL session running and see what's going on. Note that if you wait too long, the test will complete and you won't be able to see the test database.
+
sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=50 --iterations=100 --number-int-cols=5 --number-char-cols=20 \
 +
--auto-generate-sql --verbose
  
From the MySQL prompt:
 
  
show databases;
+
Jika kita berada di terminal MySQL monitor, padea saat test ini berjalan kita dapat melihat,
  
Note the mysqlslap database:
+
show databases;
  
+--------------------+
+
Output:
| Database          |
 
+--------------------+
 
| information_schema |
 
| employees          |
 
| mysql              |
 
| mysqlslap          |
 
| performance_schema |
 
+--------------------+
 
5 rows in set (0.01 sec)
 
  
You can check the table in the test database if you want to; it's called t1.
+
+--------------------+
 +
| Database          |
 +
+--------------------+
 +
| information_schema |
 +
| employees          |
 +
| mysql              |
 +
| mysqlslap          |
 +
| performance_schema |
 +
+--------------------+
 +
5 rows in set (0.01 sec)
  
Check your other terminal window again. When the test finishes, you'll find that the performance has slowed down even more with the increased load:
+
Hasil benchmark
  
Benchmark
+
Benchmark
        Average number of seconds to run all queries: 0.695 seconds
+
Average number of seconds to run all queries: 0.400 seconds
        Minimum number of seconds to run all queries: 0.627 seconds
+
Minimum number of seconds to run all queries: 0.349 seconds
        Maximum number of seconds to run all queries: 1.442 seconds
+
Maximum number of seconds to run all queries: 0.713 seconds
        Number of clients running queries: 50
+
Number of clients running queries: 50
        Average number of queries per client: 0
+
Average number of queries per client: 0
  
Go back to your MySQL terminal session. We can see that mysqlslap has dropped its throwaway database. At the MySQL prompt:
+
pada saat test ini selesai, mysqlslap akan membuang database test-nya
  
show databases;
+
show databases;
  
+--------------------+
+
+--------------------+
| Database          |
+
| Database          |
+--------------------+
+
+--------------------+
| information_schema |
+
| information_schema |
| employees          |
+
| employees          |
| mysql              |
+
| mysql              |
| performance_schema |
+
| performance_schema |
+--------------------+
+
+--------------------+
4 rows in set (0.00 sec)
+
4 rows in set (0.00 sec)
  
Use Case: Benchmarking with Custom Queries
+
===CONTOH: Benchmarking dengan Custom Queries===
  
Auto-generated SQL is good if you are evaluating the server's physical resources. It's useful when you want to find the level of load a given system can take.
+
Auto-generated SQL baik untuk meng-evaluasi sumber daya fisik pada server.  
 +
Ini akan sangat baik untuk mengetahui level beban yang dapat di terima server.
  
When you want to troubleshoot performance for a specific database-dependent application, however, you'll want to test real queries on real data. These queries might be coming from your web or application server.
+
Jika kita ingin melakukan troubleshoot performance untuk aplikasi spesifik yang tergantung database, dan kita ingin melakukan real query test.
 +
Query ini kemungkinan akan datang dari web atau server aplikasi.
  
For now, we'll assume you know the specific query you want to test. In the next section we'll show you a way to find queries that are running on your server.
+
Kita akan menggunakan database employees di --create-scheme untuk melakukan query ke table "dept_emp",
  
We will begin with in-line queries. You can give an in-line query to mysqlslap with the --query option. The SQL statements can't have line breaks in them, and they need to be delimited by semicolons (;). The queries also need to be enclosed in double quotes.
+
sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=50 --iterations=10 --create-schema=employees \
 +
--query="SELECT * FROM dept_emp;" --verbose
  
In the following code snippet we are running a simple query against the deptemp table. The `deptemp` table has more than three hundred thousand records. Note how we have specified the employees database with the --create-schema option:
+
Jika berjalan dengan baik, maka akan keluar kira-kira,
  
sudo mysqlslap --user=sysadmin --password --host=localhost --concurrency=50 --iterations=10 --create-schema=employees --query="SELECT * FROM dept_emp;" --verbose
+
  Benchmark
 +
Average number of seconds to run all queries: 10.401 seconds
 +
Minimum number of seconds to run all queries: 9.613 seconds
 +
Maximum number of seconds to run all queries: 13.575 seconds
 +
Number of clients running queries: 50
 +
Average number of queries per client: 1
  
This will take a while to run. You should receive a performance benchmark like this after a minute or two:
+
(Catatan: Jika query membuat hang atau lebih dari 10 menit tanpa output,
 +
coba kurangi  --concurrency and/or --iterations, atau coba di server yang lebih besar)
  
Benchmark
+
Selanjutnya, kita akan menggunakan multiple SQL statements di --query parameter.
        Average number of seconds to run all queries: 18.486 seconds
+
Terminasi setiap query menggunakan titik koma (semicolon).  
        Minimum number of seconds to run all queries: 15.590 seconds
+
Kita memberitahukan kode terminasi melalui opsi --delimiter :
        Maximum number of seconds to run all queries: 28.381 seconds
 
        Number of clients running queries: 50
 
        Average number of queries per client: 1
 
  
(Note: If this query hangs for more than ten minutes or does not give any output, you should try it again with a lower number for --concurrency and/or --iterations, or try it on a bigger server.)
+
sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=20 --iterations=10 --create-schema=employees \
 +
--query="SELECT * FROM employees;SELECT * FROM titles;SELECT * FROM dept_emp;SELECT * FROM dept_manager; \
 +
SELECT * FROM departments;" --delimiter=";" --verbose
  
Next, we will use multiple SQL statements in the --query parameter. In the following example we are terminating each query with a semicolon. mysqlslap knows we are using a number of separate SQL commands because we have specified the --delimiter option:
+
Walaupun menggunakan iterasi yang sama, kinerja perintah multiple SELECT akan jauh lebih lambat  (averaging 11.6 seconds vs. 10.4 seconds):
  
sudo mysqlslap --user=sysadmin --password --host=localhost --concurrency=20 --iterations=10 --create-schema=employees --query="SELECT * FROM employees;SELECT * FROM titles;SELECT * FROM dept_emp;SELECT * FROM dept_manager;SELECT * FROM departments;" --delimiter=";" --verbose
+
  Benchmark
 +
Average number of seconds to run all queries: 11.640 seconds
 +
Minimum number of seconds to run all queries: 11.395 seconds
 +
Maximum number of seconds to run all queries: 13.239 seconds
 +
Number of clients running queries: 20
 +
Average number of queries per client: 5
  
This test uses the same number of connections and the same number of iterations. However, the performance was incrementally slower for multiple SELECT statements (averaging 23.8 seconds vs. 18.486 seconds):
 
  
Benchmark
+
Production SQL statement bisa menjadi sangat rumit.  
        Average number of seconds to run all queries: 23.800 seconds
+
Akan lebih mudah untuk menambahkan complicated SQL statement ke script yang akan di cobakan untuk test.
        Minimum number of seconds to run all queries: 22.751 seconds
+
Kita cukup memberitahukan mysqlslap untuk membaca script tersebut.
        Maximum number of seconds to run all queries: 26.788 seconds
 
        Number of clients running queries: 20
 
        Average number of queries per client: 5
 
  
Production SQL statements can be complicated. It's easier to add a complicated SQL statement to a script than to type it out for tests. So, we can instruct mysqlslap to read the query from a script file.
+
Untuk memberikan contoh, mari kita membuat script file untuk perintah SQL sebagai berikut
  
To illustrate this, let's create a script file from the SQL commands. We can use the code snippet below to create such a file:
+
sudo echo "SELECT * FROM employees;SELECT * FROM titles;SELECT * FROM dept_emp;SELECT * FROM dept_manager;SELECT * FROM departments;" > ~/select_query.sql
 +
sudo cp ~/select_query.sql /mysqlslap_tutorial/
  
sudo echo "SELECT * FROM employees;SELECT * FROM titles;SELECT * FROM dept_emp;SELECT * FROM dept_manager;SELECT * FROM departments;" > ~/select_query.sql
+
File select_query.sql mempunyai lima SELECT statements.
  
sudo cp ~/select_query.sql /mysqlslap_tutorial/
+
mysqlslap dapat mem-paralel query.
 +
Kita dapat men-set jumlah query untuk setiap test client.
 +
mysqlslap menset ini melalui opsi --number-of-queries option.
 +
Jadi jika kita mempunyai 50 connection and 1000 query, setiap client akan menjalankan 20 query.
  
The select_query.sql file now holds all five SELECT statements.
+
Kita juga akan menggunakan switch --debug-info, yang akan memberikan indikasi sumber daya komputasi yang digunakan.
  
Since this script has multiple queries, we can introduce a new testing concept. mysqlslap can parallelize the queries. We can do this by specifying the number of queries each test client should execute. mysqlslap does this with the --number-of-queries option. So, if we have 50 connections and 1000 queries to run, each client will execute approximately 20 queries each.
+
sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=20 --number-of-queries=1000 --create-schema=employees \
 +
--query="/mysqlslap_tutorial/select_query.sql" --delimiter=";" --verbose --iterations=2 --debug-info
  
Finally, we can also use the --debug-info switch, which will give us an indication of the computing resources used.
+
Setelah perintah ini selesai, kita akan melihat hasil yang menarik:
  
In the following code snippet, we are asking mysqlslap to use the script file we just created. We are also specifying the number-of-queries parameter. The process will be repeated twice and we want debugging information in the output:
+
Benchmark
 +
Average number of seconds to run all queries: 117.846 seconds
 +
Minimum number of seconds to run all queries: 115.245 seconds
 +
Maximum number of seconds to run all queries: 120.448 seconds
 +
Number of clients running queries: 20
 +
Average number of queries per client: 50
 +
 +
User time 128.85, System time 73.92
 +
Maximum resident set size 827056, Integral resident set size 0
 +
Non-physical pagefaults 11192754, Physical pagefaults 0, Swaps 0
 +
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
 +
Voluntary context switches 314596, Involuntary context switches 6382
  
sudo mysqlslap --user=sysadmin --password --host=localhost  --concurrency=20 --number-of-queries=1000 --create-schema=employees --query="/mysqlslap_tutorial/select_query.sql" --delimiter=";" --verbose --iterations=2 --debug-info
+
Disini waktu yang dibutuhkan untuk melakukan query MySQL adalah 117.8 seconds, mendekati 2 menit.
 +
Ini jelas akan mempengaruhi RAM dan CPU di mesin virtual,
 +
ini juga karena banyak query dar client yang di ulang dua kali.
  
After this command completes, we can see some interesting results:
+
Kita juga melihat banyak non-physical page faults. Page fault terhadi ketika data tidak ada di memory dan system harus mengambilnya ke swap file di disk.
 +
Kita juga melihat informasi yang berkaitan dengan CPU, terlihat cukup banyak context switch.
  
Benchmark
+
==CONTOH: Skenario Benchmarking yang praktis dan Capturing Live Queries==
        Average number of seconds to run all queries: 217.151 seconds
 
        Minimum number of seconds to run all queries: 213.368 seconds
 
        Maximum number of seconds to run all queries: 220.934 seconds
 
        Number of clients running queries: 20
 
        Average number of queries per client: 50
 
  
 +
Sejauh ini dalam contoh kita, kita telah menjalankan query terhadap database asli employees. Itu sesuatu yang oleh Database Administrator (DBA) tentu tidak ingin anda lakukan. Dan ada alasan yang jelas untuk itu. Anda tidak ingin menambahkan beban database produksi anda dan anda tidak ingin menjalankan query tes yang mungkin menghapus, memperbarui, atau memasukkan data ke tabel produksi anda.
  
User time 58.16, System time 18.31
+
Kami akan menunjukkan cara untuk membuat backup dari database produksi dan menyalinnya ke lingkungan pengujian. Dalam contoh ini itu pada server yang sama, tetapi idealnya anda menyalinnya ke server terpisah dengan kapasitas hardware yang sama.
Maximum resident set size 909008, Integral resident set size 0
 
Non-physical pagefaults 2353672, Physical pagefaults 0, Swaps 0
 
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
 
Voluntary context switches 102785, Involuntary context switches 43
 
  
Here the average number of seconds to run all queries in our MySQL instance is 217 seconds, almost 4 minutes. While that was certainly affected by the amount of RAM and CPU available to our virtual machine, it was also due to the large number of queries from a moderate number of client connections repeating twice.
+
Lebih penting lagi, kami akan menunjukkan cara untuk merekam live queries dari database produksi dan menambahkannya ke skrip pengujian. Artinya, anda akan mendapatkan pertanyaan dari database produksi, tetapi menjalankan tes terhadap database test.
  
We can see there were a large number of non-physical page faults. Page faults happen when data cannot be found in memory and the system has to go and fetch it from the swap file on disk. The output also shows CPU-related information. In this case we see a large number of context switches.
+
Langkah-langkah umum adalah sebagai berikut, dan anda dapat menggunakannya untuk tes mysqlslap:
Use Case: Practical Benchmarking Scenario and Capturing Live Queries
 
  
So far in our examples, we have been running queries against the original employees database. That's something DBAs certainly wouldn't want you to do. And there's a good reason for it. You don't want to add load your production database and you don't want to run test queries that might delete, update, or insert data into your production tables.
+
# Copy production database ke test environment.
 +
# Konfigurasi MySQL untuk record dan capture semua connection requests dan queries di production database.
 +
# Simulasikan penggunaan yang akan kita coba untuk test. Contoh, kita kita menjalankan shopping cart, kita perlu membeli sesuatu agar mentrigger database query yang sesuati dari aplikasi kita.
 +
# Matikan query logging.
 +
# Lihat query log dan buat catatan dari quary yang akan kita test.
 +
# Buat sebuah test file untuk setiap query yang akan kita test.
 +
# Run test.
 +
# Gunakan output untuk meng-improve performance database.
  
We'll show you how to make a backup of a production database and copy it to a testing environment. In this example it's on the same server, but you ideally you would copy it to a separate server with the same hardware capacity.
+
Untuk memulai, mari kita backup employees database. Kita akan meletakannya di directory yang berbeda :
  
More importantly, we'll show you how to record queries live from the production database and add them to a testing script. That is, you'll get queries from the production database, but run tests against the test database.
+
sudo mkdir /mysqlslap_tutorial/mysqlbackup
 +
cd /mysqlslap_tutorial/mysqlbackup
 +
sudo mysqldump --user=root --password=123456 --host localhost employees > ~/employees_backup.sql
 +
sudo cp ~/employees_backup.sql /mysqlslap_tutorial/mysqlbackup/
  
The general steps are as follows, and you can use them for any mysqlslap test:
+
Jika anda menggunakan server yang berbeda, anda perlu mengcopy employees_backup.sql ke server tersebut.
 +
Masuk ke MySQL monitor dan lakukan
  
1. Copy the production database to a test environment.
+
mysql -u root -p123456
2. Configure MySQL to record and capture all connection requests and queries on the production database.
 
3. Simulate the use case you are trying to test. For example, if you run a shopping cart, you should buy something to trigger all the appropriate database queries from your application.
 
4. Turn off query logging.
 
5. Look at the query log and make a list of the queries you want to test.
 
6. Create a test file for each query you want to test.
 
7. Run the tests.
 
8. Use the output to improve your database performance.
 
  
To start, let's create a backup of the employees database. We will create a separate directory for its backup:
+
CREATE DATABASE employees_backup;
 +
quit;
  
sudo mkdir /mysqlslap_tutorial/mysqlbackup
+
Import employees_backup.sql ke MySQL.
  
cd /mysqlslap_tutorial/mysqlbackup
+
sudo mysql -u root -p123456 employees_backup < /mysqlslap_tutorial/mysqlbackup/employees_backup.sql
  
Create the backup and move it to the new directory:
+
Di production MySQL database server, enable MySQL general query log dan berikan file name untuk itu.
 +
General query log akan menangkap semua connection, disconnection, dan aktifitas query pada MySQL database yang berjalan.
  
sudo mysqldump --user sysadmin --password --host localhost employees > ~/employees_backup.sql
+
mysql -u root -p123456
  
sudo cp ~/employees_backup.sql /mysqlslap_tutorial/mysqlbackup/
+
SET GLOBAL general_log=1, general_log_file='capture_queries.log';
 +
quit;
  
Go to your MySQL test server. Create the employees_backup database:
 
  
CREATE DATABASE employees_backup;
+
Sekarang lakukan query yang akan kita test pada MySQL server production.
 +
Pada contoh ini, kita akan menjalankan query dari command line.
 +
Akan tetapi, kita dapat saja men-generate query dari aplikasi kita daripada menjalankannya secara langsung.
 +
Jika kita mempunyai proses yang lambat atau halaman web yang akan di test, kita sebaiknya run proses tersebut atau akses ke web tersebut sekarang.
 +
Contoh, jika kita menjalankan shopping cart, kita ingin menjalankan / menyelesaikan proses itu sekarang, ini akan men-trigger query yang sesuai di database server.
  
At this point, if you are using a separate server for testing, you should copy the employeesbackup.sql file over to it. From your main terminal session, import the backup data into the employeesbackup database:
+
Berikut ini adalah query yang akan kita run di MySQL server production pada database employees :
  
sudo mysql -u sysadmin -p employees_backup < /mysqlslap_tutorial/mysqlbackup/employees_backup.sql
+
mysql -u root -p123456
  
On your production MySQL database server, enable the MySQL general query log and provide a file name for it. The general query log captures connection, disconnection, and query activities for a MySQL database instance.
 
  
SET GLOBAL general_log=1, general_log_file='capture_queries.log';
+
USE employees;
 +
SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN  departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date;
  
Now run the queries that you want to test on the production MySQL server. In this example we will run a query from the command line. However, you may want to generate queries from your application instead of running them directly. If you have a slow process or website page that you want to test, you should run through that process or access that web page now. For example, if you are running a shopping cart, you may want to complete the checkout process now, which should trigger all the appropriate queries on the database server.
+
Output:
  
This is the query we will run on the production MySQL server. First use the right database:
+
489903 rows in set (4.02 sec)
  
USE employees;
+
Selesai menjalankan query, matikan general logging:
  
Now run the query:
+
SET GLOBAL general_log=0;
 +
quit;
  
SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date;
 
  
Expected output:
+
Pastikan log dimatikan setelah selesai test.
 +
Jika log tetap nyala, akan menyebabkan proses testing menjadi lebih berat.
 +
Cek file log yang berada di directory /var/lib/mysql :
  
489903 rows in set (4.33 sec)
+
sudo ls -l /var/lib/mysql/capt*
  
We will turn off general logging when the query completes:
+
-rw-rw---- 1 mysql mysql 949 Jun  8 06:09 /var/lib/mysql/capture_queries.log
  
SET GLOBAL general_log=0;
+
Copy file tersebut ke MySQL test directory.
 +
Jika kita menggunakan server yang berbeda untuk testing, copy ke server tersebut.
  
Note that if you leave logging on, queries will continue to be added to the log, which may make testing harder. So, make sure you disable the log right after finishing your test. Let's check that the log file was created in the /var/lib/mysql directory:
+
sudo cp /var/lib/mysql/capture_queries.log /mysqlslap_tutorial/
  
sudo ls -l /var/lib/mysql/capt*
+
Cek isi terakhir file log,
  
-rw-rw----. 1 mysql mysql 861 Sep 24 15:09 /var/lib/mysql/capture_queries.log
+
sudo tail /mysqlslap_tutorial/capture_queries.log
  
Let's copy this file to our MySQL test directory. If you're using a separate server for testing, copy it to that server.
+
Output:
  
sudo cp /var/lib/mysql/capture_queries.log /mysqlslap_tutorial/
+
  41 Query show databases
 +
  41 Query show tables
 +
  41 Field List departments
 +
  41 Field List dept_emp
 +
  41 Field List dept_manager
 +
  41 Field List employees
 +
  41 Field List salaries
 +
  41 Field List titles
 +
  41 Query SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN  departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date
 +
150608  6:09:42   41 Query SET GLOBAL general_log=0
  
There should be quite a bit of data in this log file. In this example, the query we want should be near the end. Check the last part of the file:
 
  
sudo tail /mysqlslap_tutorial/capture_queries.log
+
Log akan memperlihatkan perintah SQL dan timestamp.
 +
Perintah SQL SELECT yang ada di akhir file yang kita tertarik.
 +
Perintah ini yang ingin kita tangkap dan akan di buat replikasi untuk test-nya.
 +
Dalam contoh ini, kita sudah mengetahui perintah SQL SELECT tersebut yang kita gunakan.
 +
Tapi di production server yang sebenarnya, kita tidak akan mengetahui dan cara ini sangat bermanfaat untuk mengetahui query bermasalah yang dijalankan pada server production.
  
Expected output:
+
Perlu di catat, jika kita run / trigger query yang berbeda saat melakukan logging, file log akan sangat berbeda isinya.
 +
Pada skenario yang real, file log akan berisi banyak catatan / entry dari berbagai sambungan.
 +
Tugas kita adalah mencari query yang membuat masalah.
 +
Kita dapat saja mencatat semua query yang ada, me-list-nya dan menjalankan-nya di database yang akan di test.
  
        6294 Query show databases
+
Untuk setiap query yang akan di test, kita copy ke file dengan akhiran .sql.
        6294 Query show tables
 
        6294 Field List    departments
 
        6294 Field List    dept_emp
 
        6294 Field List    dept_manager
 
        6294 Field List    employees
 
        6294 Field List    salaries
 
        6294 Field List    titles
 
140930 15:34:52  6294 Query SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date
 
140930 15:35:06  6294 Query SET GLOBAL general_log=0
 
  
This log shows SQL commands and their timestamps. The SQL SELECT statement near the end of the file is what we are interested in. It should be exactly the same as the command we ran on the production database, since that's where we captured it.
+
Contoh:
  
In this example, we already knew the query. But, in a production environment, this method can be very useful for finding queries that you may not necessarily know about that are running on your server.
+
sudo vi /mysqlslap_tutorial/capture_queries.sql
  
Note that if you ran or triggered different queries while logging, this file will look completely different. In a real scenario this file could be inundated with hundreds of entries coming from all different connections. Your goal is to find the query or queries that are causing a bottleneck. You can start by making a list of every line that includes the text Query. Then you'll have a list of exactly what queries were run on your database during the test.
+
Isi dengan MySQL query yang akan kita gunakan untuk test, tanpa line break tanpa semicolon di akhi
  
For each query that you want to test, copy it into a file that ends with a .sql extension.
+
SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date
  
For example:
+
Selanjutnya, pastikan hasil query tidak di cached.
 +
Kembali ke MySQL monitor, jalankan perintah berikut:
  
sudo vi /mysqlslap_tutorial/capture_queries.sql
+
mysql -u root -p123456
  
The contents should be the MySQL query you want to test, without any line breaks and without a semicolon at the end:
+
RESET QUERY CACHE;
 +
quit
  
SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date
+
Sekarang coba menjalankan mysqlslap dengan script file.
 +
Pastikan kita menggunakan script file yang benar di parameter --query .
 +
Kita akan menggunakan hanya 10 concurrent connections dan mengulang dua kali.
 +
Run di server test:
  
Next, make sure the query results are not cached. Go back to your test MySQL session. Run the following command:
+
sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=2 --iterations=2 \
 +
--create-schema=employees_backup --query="/mysqlslap_tutorial/capture_queries.sql" --verbose
  
RESET QUERY CACHE;
+
Output:
  
Now it's time to run the mysqlslap utility with the script file. Make sure you use the correct script file name in the --query parameter. We will use only ten concurrent connections and repeat the test twice. Run this from your test server:
+
Benchmark
 +
        Average number of seconds to run all queries: 68.692 seconds
 +
        Minimum number of seconds to run all queries: 59.301 seconds
 +
        Maximum number of seconds to run all queries: 78.084 seconds
 +
        Number of clients running queries: 10
 +
        Average number of queries per client: 1
  
sudo mysqlslap --user=sysadmin --password --host=localhost  --concurrency=10 --iterations=2 --create-schema=employees_backup --query="/mysqlslap_tutorial/capture_queries.sql" --verbose
+
===Bagaimana kita dapat memperbaiki benchmark?===
  
The benchmark output looks like this in our system:
+
Kita akan perlu familiar dengan query MySQL untuk menilai  apa yang sedang dilakukan saat query.
  
Benchmark
+
Melihat query yang dilakukan, kita dapat melihat bahwa query melalukan sejumlah join pada multiple table. Query memperlihatkan employee job histories dan untuk melakukan itu, query harus join beberapa table  by empno field. Query juga menggunakan deptno field untuk join, akan tetapi karena hanya sedikit departement record, kita akan mengabaikan ini. Karena banyak empno entries  di database, sangat logical untuk berasumsi bahwa dengan membuat index dari empno field akan meng-improve query.
        Average number of seconds to run all queries: 68.692 seconds
 
        Minimum number of seconds to run all queries: 59.301 seconds
 
        Maximum number of seconds to run all queries: 78.084 seconds
 
        Number of clients running queries: 10
 
        Average number of queries per client: 1
 
  
So how can we improve this benchmark?
+
Dengan banyak praktek, setelah kita menemukan query yang menyebabkan server menjadi lambat (ini dengan pertolongan mysqlslap), kita dapat melakukan assessment tentang query berbasis pada pengetahuan MySQL dan database yang kita miliki.
  
You'll need a certain amount of familiarity with MySQL queries to assess what the query is doing.
+
Selanjutnya, kita akan berusaha untuk memperbaiki database atau query yang akan di jalankan padanya.
  
Looking back at the query, we can see it's doing a number of joins across multiple tables. The query shows employee job histories and in doing so, it's joining different tables by the empno field. It's also using the deptno field for joining, but since there are only a few department records, we will ignore this. Since there are many empno entries in the database, it's logical to assume that creating indexes on the empno field could improve the query.
+
Pada kasus ini, mari kita tambahkan index seperti dijelaskan di atas.
 +
Kita akan membuat tiga index pada empno.  
 +
Satu index akan dibuat pada empno field di employees table.
 +
Satu index akan dibuat pada empno field di deptemp table,
 +
dan index terakhir pada emp_no field pada title table.
  
With a little practice, once you've located queries that are taxing the server (that's the part that mysqlslap helps with!), you'll be able to make assessments about the queries based on your knowledge of MySQL and your database.
 
  
Next, you can try to improve your database or the queries that are being executed on it.
+
mysql -u root -p123456
  
In our case, let's add the indexes we mentioned above. We will create three indexes on empno. One index will be created on the empno field in the employees table, another index will be created on the empno field in the deptemp table, and the last one will be created on the emp_no field in the titles table.
+
USE employees_backup;
 +
CREATE INDEX employees_empno ON employees(emp_no);
 +
CREATE INDEX dept_emp_empno ON dept_emp(emp_no);
 +
CREATE INDEX titles_empno ON titles(emp_no);
 +
quit;
  
Let's go to our test MySQL session and execute the following commands:
+
Kembali ke terminal, jalankan mysqlslap dengan paramter yang sama, kita akan melihat benchmark yang berbeda.
  
USE employees_backup;
+
sudo mysqlslap --user=sysadmin --password --host=localhost  --concurrency=2 --iterations=2 --create-schema=employees_backup \
 +
--query="/mysqlslap_tutorial/capture_queries.sql" --verbose
  
CREATE INDEX employees_empno ON employees(emp_no);
+
Output:
  
CREATE INDEX dept_emp_empno ON dept_emp(emp_no);
+
Benchmark
 +
        Average number of seconds to run all queries: 55.869 seconds
 +
        Minimum number of seconds to run all queries: 55.706 seconds
 +
        Maximum number of seconds to run all queries: 56.033 seconds
 +
        Number of clients running queries: 10
 +
        Average number of queries per client: 1
  
CREATE INDEX titles_empno ON titles(emp_no);
+
Kita akan melihat bahwa terjadi perbaikan di average, minimum, dan maximum time untuk menjalankan query. Dari yang tadinya rata2 68 seconds, query sekarang di jalankan dalam 55 seconds. Ini perbaikan 13 seconds untuk beban yang sama.
  
Coming back to our main terminal window on the test server, if we execute mysqlslap with the same parameters, we will see a difference in the benchmark:
+
Karena perubahan database menghasilkan hasil yang baik di lingkungan test, kita dapat menggunakannya di production database server.
 +
Perlu di ingat bahwa perubahan database selalu ada keuntungan dan kerugian.
  
sudo mysqlslap --user=sysadmin --password --host=localhost  --concurrency=10 --iterations=2 --create-schema=employees_backup --query="/mysqlslap_tutorial/capture_queries.sql" --verbose
+
Kita dapat mengulangi proses dari perintah test dan improvement di semua query yang kita lihat di log.
  
Benchmark
+
==Troubleshooting - mysqlslap tidak menghasilkan Output==
        Average number of seconds to run all queries: 55.869 seconds
 
        Minimum number of seconds to run all queries: 55.706 seconds
 
        Maximum number of seconds to run all queries: 56.033 seconds
 
        Number of clients running queries: 10
 
        Average number of queries per client: 1
 
  
We can see that there is an immediate improvement in the average, minimum, and maximum time to execute the query. Instead of an average 68 seconds, the query now executes in 55 seconds. That's an improvement of 13 seconds for the same load.
+
Jika perintah test tidak menghasilkan outout, ini adalah indikasi bahwa sumber daya / resource pada server sudah habis.
 +
Simptom seperti tidak ada Benchmark output, server mungkin hang atau error seperti
  
Since this database change produced a good result in the test environment, you can now consider rolling it out to your production database server, although keep in mind that database changes typically have trade-offs in their advantages and disadvantages.
+
mysqlslap: Error when storing result: 2013 Lost connection to MySQL server during query.
  
You can repeat the process of testing commands and improvements with all of the queries you gleaned from your log.
+
Kita dapat mencoba test dengan parameter --concurrency atau --iterations yang lebih kecil.
Troubleshooting - mysqlslap Doesn't Show Output
+
Alternatif lain, kita dapat meng-upgrade test server.
  
If you run a test command and don't get any output, this is a good indication that your server resources could be maxed out. Symptoms may include a lack of the Benchmark output, or an error like mysqlslap: Error when storing result: 2013 Lost connection to MySQL server during query.
+
Ini adalah cara yang baik untuk mengetahui limit dari kapasitas database server yang kita miliki.
  
You may want to try the test again with a smaller number in the --concurrency or --iterations parameter. Alternately, you can try upgrading your test server environment.
+
==Penutup==
  
This can be a good way to find the outer limits of your database server's capacity.
+
mysqlslap adalah tool yang simple, light-weight dan sangat mudah digunakan yang terintegrasi dalam engine MySQL database.
Conclusion
 
  
mysqlslap is a simple, light-weight tool that's easy to use and that integrates natively with the MySQL database engine. It's available for all editions of MySQL from version 5.1.4.
+
Pada tutorial ini, kita dapat melihat bagaimana menggunakan  mysqlslap dengan berbagai opsi-nya pada database contoh / sample.
 +
Kita dapat men-download berbagai sample database pada MySQL site dan mem-praktekan-nya.
  
In this tutorial we have seen how to use mysqlslap with its various options and played around with a sample database. You can download other sample databases from the MySQL site and practice with those too. As we mentioned before, please don't run tests on a production database server.
+
Jangan pernah menjalankan / run test di production database server.
  
 
The last use case in this tutorial involved only one query. While we improved the performance of that query somewhat by adding extra indexes to all three tables, the process may not be so simple in real life. Adding extra indexes can sometimes degrade system performance and DBAs often need to weigh the benefits of adding an extra index against the performance cost it may incur.
 
The last use case in this tutorial involved only one query. While we improved the performance of that query somewhat by adding extra indexes to all three tables, the process may not be so simple in real life. Adding extra indexes can sometimes degrade system performance and DBAs often need to weigh the benefits of adding an extra index against the performance cost it may incur.
  
Real life testing scenarios are more complex, but this should give you the tools to get started with testing and improving your database performance.
+
Skenario Real life testing biasanya lebih kompleks, tapi ini harusnya cukup memberikan tool yang dibutuhkan untuk memulai test dan memperbaiki performance database.
  
 
==Referensi==
 
==Referensi==
  
 
* https://www.digitalocean.com/community/tutorials/how-to-measure-mysql-query-performance-with-mysqlslap
 
* https://www.digitalocean.com/community/tutorials/how-to-measure-mysql-query-performance-with-mysqlslap

Latest revision as of 07:23, 8 June 2015

mysqlslap dapat meng-emulasi sejumlah besar koneksi klien meng-hit server database pada saat yang sama. Parameter pengujian dapat beban sepenuhnya dikonfigurasi dan hasil dari tes berjalan yang berbeda dapat digunakan untuk menyempurnakan desain database atau sumber daya perangkat keras.

Dalam tutorial ini kita akan belajar bagaimana menggunakan mysqlslap untuk menguji beban database MySQL dengan beberapa query dasar dan melihat bagaimana benchmarking dapat membantu kita dalam menyempurnakan query tersebut. Setelah beberapa demonstrasi dasar, kita akan menjalankan melalui skenario pengujian yang cukup realistis di mana kita membuat copy dari database yang sudah ada untuk pengujian, mengumpulkan pertanyaan dari log, dan menjalankan tes dari script.

Jika anda tertarik untuk pembandingan server database tertentu, anda harus menguji pada server dengan spesifikasi yang sama dan dengan salinan dari database anda yang terinstal.

Jika anda ingin menjalankan melalui tutorial ini untuk tujuan belajar dan melaksanakan setiap perintah di dalamnya, kami sarankan setidaknya 2 GB Droplet. Karena perintah dalam tutorial ini dimaksudkan untuk memaksa server, anda mungkin menemukan mereka akan time out pada server yang lebih kecil.

Sampel output dalam tutorial ini diproduksi dalam berbagai cara untuk mengoptimalkan contoh untuk mengajar.

Langkah Pertama - Install MySQL di sistem yang akan di test

Instalasi MySQL di Ubuntu relatif sederhana hanya menggunakan perintah,

apt-get update
apt-get install -y mysql-server mysql-client

Biasanya akan di tanya root password. Jika kita masih belajar dapat memasukan root password

123456

Agar lebih aman lakukan secure installation

mysql_secure_installation 

Jawab

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Change the root password? [Y/n] n
 ... skipping.

Remove anonymous users? [Y/n] 
 ... Success!

Disallow root login remotely? [Y/n] Y
 ... Success!

Remove test database and access to it? [Y/n] Y
 ... Success!

Reload privilege tables now? [Y/n] Y
 ... Success!


Untuk menjalankan MySQL Server cukup menggunakan perintah

/etc/init.d/mysql restart

Langkah Kedua - Install Sample Database

Kita perlu mengambil contoh database untuk test.

apt-get install bzip2
mkdir -p /mysqlslap_tutorial
cd /mysqlslap_tutorial
sudo wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2
sudo bzip2 -dfv employees_db-full-1.0.6.tar.bz2
sudo tar -xf employees_db-full-1.0.6.tar

Kita menggunakan database employees dengan 3 juta entry jadi cukup mendekati kondisi di lapangan.

cd /mysqlslap_tutorial/employees_db
ls -lh
total 161M
-rw-r--r-- 1 501 staff  752 Mar 30  2009 Changelog
-rw-r--r-- 1 501 staff 6,4K Okt  9  2008 employees_partitioned2.sql
-rw-r--r-- 1 501 staff 7,5K Peb  6  2009 employees_partitioned3.sql
-rw-r--r-- 1 501 staff 5,6K Peb  6  2009 employees_partitioned.sql
-rw-r--r-- 1 501 staff 3,8K Nov 28  2008 employees.sql
-rw-r--r-- 1 501 staff  241 Jul 30  2008 load_departments.dump
-rw-r--r-- 1 501 staff  14M Mar 30  2009 load_dept_emp.dump
-rw-r--r-- 1 501 staff 1,1K Jul 30  2008 load_dept_manager.dump
-rw-r--r-- 1 501 staff  17M Jul 30  2008 load_employees.dump
-rw-r--r-- 1 501 staff 111M Jul 30  2008 load_salaries.dump
-rw-r--r-- 1 501 staff  21M Jul 30  2008 load_titles.dump
-rw-r--r-- 1 501 staff 3,8K Mar 30  2009 objects.sql
-rw-r--r-- 1 501 staff 2,2K Jul 30  2008 README
-rw-r--r-- 1 501 staff 4,4K Mar 30  2009 test_employees_md5.sql
-rw-r--r-- 1 501 staff 4,4K Mar 30  2009 test_employees_sha.sql


Masukan database tersebut ke mysql

cd /mysqlslap_tutorial/employees_db
sudo mysql -u root -p123456 -t < employees.sql

Akan muncul

+-----------------------------+
| INFO                        |
+-----------------------------+
| CREATING DATABASE STRUCTURE |
+-----------------------------+
+------------------------+
| INFO                   |
+------------------------+
| storage engine: InnoDB |
+------------------------+
+---------------------+
| INFO                |
+---------------------+
| LOADING departments |
+---------------------+
+-------------------+
| INFO              |
+-------------------+
| LOADING employees |
+-------------------+
+------------------+
| INFO             |
+------------------+
| LOADING dept_emp |
+------------------+
+----------------------+
| INFO                 |
+----------------------+
| LOADING dept_manager |
+----------------------+
+----------------+
| INFO           |
+----------------+
| LOADING titles |
+----------------+
+------------------+
| INFO             |
+------------------+
| LOADING salaries |
+------------------+

Login ke MySQL dan cek tabel / data yang ada

mysql -u root -p123456

Lakukan

show databases;

Output

+--------------------+
| Database           |
+--------------------+
| information_schema |
| employees          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)


use employees;
show tables;

Output:

+---------------------+
| Tables_in_employees |
+---------------------+
| departments         |
| dept_emp            |
| dept_manager        |
| employees           |
| salaries            |
| titles              |
+---------------------+
6 rows in set (0.00 sec)


describe titles;

Output:

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| emp_no    | int(11)     | NO   | PRI | NULL    |       |
| title     | varchar(50) | NO   | PRI | NULL    |       |
| from_date | date        | NO   | PRI | NULL    |       |
| to_date   | date        | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


select count(*) from titles;

Output:

+----------+
| count(*) |
+----------+
|   443308 |
+----------+
1 row in set (0.14 sec)


select count(*) from employees;

Output:

+----------+
| count(*) |
+----------+
|   300024 |
+----------+
1 row in set (0.11 sec)


Cek data-data lainnya, untuk keluar ketik

quit;

Langkah ketiga - Menggunakan mysqlslap

Opsi / Fitur mysqlslap bisa di peroleh dari

mysqlslap --help

Beberapa yang penting

--user 	MySQL username to connect to the database server
--password 	Password for the user account. It's best to leave it blank in command line
--host 	MySQL database server name
--port 	Port number for connecting to MySQL if the default is not used
--concurrency 	The number of simultaneous client connections mysqlslap will emulate
--iterations 	The number of times the test query will be run
--create-schema 	The database where the query will be run
--query 	The query to execute. This can either be a SQL query string or a path to a SQL script file
--create 	The query to create a table. Again, this can be a query string or a path to a SQL file
--delimiter 	The delimiter used to separate multiple SQL statements
--engine 	The MySQL database engine to use (e.g., InnoDB)
--auto-generate-sql 	Lets MySQL perform load testing with its own auto-generated SQL command

CONTOH: Benchmark menggunakan Auto-generated SQL dan Data

sudo mysqlslap --user=root --password=123456 --host=localhost  --auto-generate-sql --verbose

Output:

Benchmark
	Average number of seconds to run all queries: 0.009 seconds 
	Minimum number of seconds to run all queries: 0.009 seconds
	Maximum number of seconds to run all queries: 0.009 seconds
	Number of clients running queries: 1
	Average number of queries per client: 0


Coba 50 concurrent connections, gunakan auto-generated query run 10 kali:

sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=50 --iterations=10 --auto-generate-sql --verbose
Benchmark
	Average number of seconds to run all queries: 0.137 seconds
	Minimum number of seconds to run all queries: 0.117 seconds
	Maximum number of seconds to run all queries: 0.159 seconds
	Number of clients running queries: 50
	Average number of queries per client: 0


Coba menggqunakan auto-generated SQL query untuk membuat tabel dengan 5 numeric kolom dan 20 character setiap kolom. Kita akan mensimulasi 50 client connection dan test diulangi 100 kali,

sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=50 --iterations=100 --number-int-cols=5 --number-char-cols=20 \
--auto-generate-sql --verbose


Jika kita berada di terminal MySQL monitor, padea saat test ini berjalan kita dapat melihat,

show databases;

Output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| employees          |
| mysql              |
| mysqlslap          |
| performance_schema |
+--------------------+
5 rows in set (0.01 sec)

Hasil benchmark

Benchmark
	Average number of seconds to run all queries: 0.400 seconds
	Minimum number of seconds to run all queries: 0.349 seconds
	Maximum number of seconds to run all queries: 0.713 seconds
	Number of clients running queries: 50
	Average number of queries per client: 0

pada saat test ini selesai, mysqlslap akan membuang database test-nya

show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| employees          |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

CONTOH: Benchmarking dengan Custom Queries

Auto-generated SQL baik untuk meng-evaluasi sumber daya fisik pada server. Ini akan sangat baik untuk mengetahui level beban yang dapat di terima server.

Jika kita ingin melakukan troubleshoot performance untuk aplikasi spesifik yang tergantung database, dan kita ingin melakukan real query test. Query ini kemungkinan akan datang dari web atau server aplikasi.

Kita akan menggunakan database employees di --create-scheme untuk melakukan query ke table "dept_emp",

sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=50 --iterations=10 --create-schema=employees \
--query="SELECT * FROM dept_emp;" --verbose

Jika berjalan dengan baik, maka akan keluar kira-kira,

Benchmark
	Average number of seconds to run all queries: 10.401 seconds
	Minimum number of seconds to run all queries: 9.613 seconds
	Maximum number of seconds to run all queries: 13.575 seconds
	Number of clients running queries: 50
	Average number of queries per client: 1

(Catatan: Jika query membuat hang atau lebih dari 10 menit tanpa output, coba kurangi --concurrency and/or --iterations, atau coba di server yang lebih besar)

Selanjutnya, kita akan menggunakan multiple SQL statements di --query parameter. Terminasi setiap query menggunakan titik koma (semicolon). Kita memberitahukan kode terminasi melalui opsi --delimiter :

sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=20 --iterations=10 --create-schema=employees \
--query="SELECT * FROM employees;SELECT * FROM titles;SELECT * FROM dept_emp;SELECT * FROM dept_manager; \
SELECT * FROM departments;" --delimiter=";" --verbose

Walaupun menggunakan iterasi yang sama, kinerja perintah multiple SELECT akan jauh lebih lambat (averaging 11.6 seconds vs. 10.4 seconds):

Benchmark
	Average number of seconds to run all queries: 11.640 seconds
	Minimum number of seconds to run all queries: 11.395 seconds
	Maximum number of seconds to run all queries: 13.239 seconds
	Number of clients running queries: 20
	Average number of queries per client: 5


Production SQL statement bisa menjadi sangat rumit. Akan lebih mudah untuk menambahkan complicated SQL statement ke script yang akan di cobakan untuk test. Kita cukup memberitahukan mysqlslap untuk membaca script tersebut.

Untuk memberikan contoh, mari kita membuat script file untuk perintah SQL sebagai berikut

sudo echo "SELECT * FROM employees;SELECT * FROM titles;SELECT * FROM dept_emp;SELECT * FROM dept_manager;SELECT * FROM departments;" > ~/select_query.sql
sudo cp ~/select_query.sql /mysqlslap_tutorial/

File select_query.sql mempunyai lima SELECT statements.

mysqlslap dapat mem-paralel query. Kita dapat men-set jumlah query untuk setiap test client. mysqlslap menset ini melalui opsi --number-of-queries option. Jadi jika kita mempunyai 50 connection and 1000 query, setiap client akan menjalankan 20 query.

Kita juga akan menggunakan switch --debug-info, yang akan memberikan indikasi sumber daya komputasi yang digunakan.

sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=20 --number-of-queries=1000 --create-schema=employees \
--query="/mysqlslap_tutorial/select_query.sql" --delimiter=";" --verbose --iterations=2 --debug-info

Setelah perintah ini selesai, kita akan melihat hasil yang menarik:

Benchmark
	Average number of seconds to run all queries: 117.846 seconds
	Minimum number of seconds to run all queries: 115.245 seconds
	Maximum number of seconds to run all queries: 120.448 seconds
	Number of clients running queries: 20
	Average number of queries per client: 50

User time 128.85, System time 73.92
Maximum resident set size 827056, Integral resident set size 0
Non-physical pagefaults 11192754, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 314596, Involuntary context switches 6382

Disini waktu yang dibutuhkan untuk melakukan query MySQL adalah 117.8 seconds, mendekati 2 menit. Ini jelas akan mempengaruhi RAM dan CPU di mesin virtual, ini juga karena banyak query dar client yang di ulang dua kali.

Kita juga melihat banyak non-physical page faults. Page fault terhadi ketika data tidak ada di memory dan system harus mengambilnya ke swap file di disk. Kita juga melihat informasi yang berkaitan dengan CPU, terlihat cukup banyak context switch.

CONTOH: Skenario Benchmarking yang praktis dan Capturing Live Queries

Sejauh ini dalam contoh kita, kita telah menjalankan query terhadap database asli employees. Itu sesuatu yang oleh Database Administrator (DBA) tentu tidak ingin anda lakukan. Dan ada alasan yang jelas untuk itu. Anda tidak ingin menambahkan beban database produksi anda dan anda tidak ingin menjalankan query tes yang mungkin menghapus, memperbarui, atau memasukkan data ke tabel produksi anda.

Kami akan menunjukkan cara untuk membuat backup dari database produksi dan menyalinnya ke lingkungan pengujian. Dalam contoh ini itu pada server yang sama, tetapi idealnya anda menyalinnya ke server terpisah dengan kapasitas hardware yang sama.

Lebih penting lagi, kami akan menunjukkan cara untuk merekam live queries dari database produksi dan menambahkannya ke skrip pengujian. Artinya, anda akan mendapatkan pertanyaan dari database produksi, tetapi menjalankan tes terhadap database test.

Langkah-langkah umum adalah sebagai berikut, dan anda dapat menggunakannya untuk tes mysqlslap:

  1. Copy production database ke test environment.
  2. Konfigurasi MySQL untuk record dan capture semua connection requests dan queries di production database.
  3. Simulasikan penggunaan yang akan kita coba untuk test. Contoh, kita kita menjalankan shopping cart, kita perlu membeli sesuatu agar mentrigger database query yang sesuati dari aplikasi kita.
  4. Matikan query logging.
  5. Lihat query log dan buat catatan dari quary yang akan kita test.
  6. Buat sebuah test file untuk setiap query yang akan kita test.
  7. Run test.
  8. Gunakan output untuk meng-improve performance database.

Untuk memulai, mari kita backup employees database. Kita akan meletakannya di directory yang berbeda :

sudo mkdir /mysqlslap_tutorial/mysqlbackup
cd /mysqlslap_tutorial/mysqlbackup
sudo mysqldump --user=root --password=123456 --host localhost employees > ~/employees_backup.sql
sudo cp ~/employees_backup.sql /mysqlslap_tutorial/mysqlbackup/

Jika anda menggunakan server yang berbeda, anda perlu mengcopy employees_backup.sql ke server tersebut. Masuk ke MySQL monitor dan lakukan

mysql -u root -p123456
CREATE DATABASE employees_backup;
quit;

Import employees_backup.sql ke MySQL.

sudo mysql -u root -p123456 employees_backup < /mysqlslap_tutorial/mysqlbackup/employees_backup.sql

Di production MySQL database server, enable MySQL general query log dan berikan file name untuk itu. General query log akan menangkap semua connection, disconnection, dan aktifitas query pada MySQL database yang berjalan.

mysql -u root -p123456
SET GLOBAL general_log=1, general_log_file='capture_queries.log';
quit;


Sekarang lakukan query yang akan kita test pada MySQL server production. Pada contoh ini, kita akan menjalankan query dari command line. Akan tetapi, kita dapat saja men-generate query dari aplikasi kita daripada menjalankannya secara langsung. Jika kita mempunyai proses yang lambat atau halaman web yang akan di test, kita sebaiknya run proses tersebut atau akses ke web tersebut sekarang. Contoh, jika kita menjalankan shopping cart, kita ingin menjalankan / menyelesaikan proses itu sekarang, ini akan men-trigger query yang sesuai di database server.

Berikut ini adalah query yang akan kita run di MySQL server production pada database employees :

mysql -u root -p123456


USE employees;
SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN  departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date;

Output:

489903 rows in set (4.02 sec)

Selesai menjalankan query, matikan general logging:

SET GLOBAL general_log=0;
quit;


Pastikan log dimatikan setelah selesai test. Jika log tetap nyala, akan menyebabkan proses testing menjadi lebih berat. Cek file log yang berada di directory /var/lib/mysql :

sudo ls -l /var/lib/mysql/capt*
-rw-rw---- 1 mysql mysql 949 Jun  8 06:09 /var/lib/mysql/capture_queries.log

Copy file tersebut ke MySQL test directory. Jika kita menggunakan server yang berbeda untuk testing, copy ke server tersebut.

sudo cp /var/lib/mysql/capture_queries.log /mysqlslap_tutorial/

Cek isi terakhir file log,

sudo tail /mysqlslap_tutorial/capture_queries.log

Output:

		   41 Query	show databases
		   41 Query	show tables
		   41 Field List	departments 
		   41 Field List	dept_emp 
		   41 Field List	dept_manager 
		   41 Field List	employees 
		   41 Field List	salaries 
		   41 Field List	titles 
		   41 Query	SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN  departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date
150608  6:09:42	   41 Query	SET GLOBAL general_log=0


Log akan memperlihatkan perintah SQL dan timestamp. Perintah SQL SELECT yang ada di akhir file yang kita tertarik. Perintah ini yang ingin kita tangkap dan akan di buat replikasi untuk test-nya. Dalam contoh ini, kita sudah mengetahui perintah SQL SELECT tersebut yang kita gunakan. Tapi di production server yang sebenarnya, kita tidak akan mengetahui dan cara ini sangat bermanfaat untuk mengetahui query bermasalah yang dijalankan pada server production.

Perlu di catat, jika kita run / trigger query yang berbeda saat melakukan logging, file log akan sangat berbeda isinya. Pada skenario yang real, file log akan berisi banyak catatan / entry dari berbagai sambungan. Tugas kita adalah mencari query yang membuat masalah. Kita dapat saja mencatat semua query yang ada, me-list-nya dan menjalankan-nya di database yang akan di test.

Untuk setiap query yang akan di test, kita copy ke file dengan akhiran .sql.

Contoh:

sudo vi /mysqlslap_tutorial/capture_queries.sql

Isi dengan MySQL query yang akan kita gunakan untuk test, tanpa line break tanpa semicolon di akhi

SELECT e.first_name, e.last_name, d.dept_name, t.title, t.from_date, t.to_date FROM employees e INNER JOIN  dept_emp de ON e.emp_no=de.emp_no INNER JOIN departments d ON de.dept_no=d.dept_no INNER JOIN titles t ON e.emp_no=t.emp_no ORDER BY  e.first_name, e.last_name, d.dept_name, t.from_date

Selanjutnya, pastikan hasil query tidak di cached. Kembali ke MySQL monitor, jalankan perintah berikut:

mysql -u root -p123456
RESET QUERY CACHE;
quit

Sekarang coba menjalankan mysqlslap dengan script file. Pastikan kita menggunakan script file yang benar di parameter --query . Kita akan menggunakan hanya 10 concurrent connections dan mengulang dua kali. Run di server test:

sudo mysqlslap --user=root --password=123456 --host=localhost  --concurrency=2 --iterations=2 \
--create-schema=employees_backup --query="/mysqlslap_tutorial/capture_queries.sql" --verbose

Output:

Benchmark
        Average number of seconds to run all queries: 68.692 seconds
        Minimum number of seconds to run all queries: 59.301 seconds
        Maximum number of seconds to run all queries: 78.084 seconds
        Number of clients running queries: 10
        Average number of queries per client: 1

Bagaimana kita dapat memperbaiki benchmark?

Kita akan perlu familiar dengan query MySQL untuk menilai apa yang sedang dilakukan saat query.

Melihat query yang dilakukan, kita dapat melihat bahwa query melalukan sejumlah join pada multiple table. Query memperlihatkan employee job histories dan untuk melakukan itu, query harus join beberapa table by empno field. Query juga menggunakan deptno field untuk join, akan tetapi karena hanya sedikit departement record, kita akan mengabaikan ini. Karena banyak empno entries di database, sangat logical untuk berasumsi bahwa dengan membuat index dari empno field akan meng-improve query.

Dengan banyak praktek, setelah kita menemukan query yang menyebabkan server menjadi lambat (ini dengan pertolongan mysqlslap), kita dapat melakukan assessment tentang query berbasis pada pengetahuan MySQL dan database yang kita miliki.

Selanjutnya, kita akan berusaha untuk memperbaiki database atau query yang akan di jalankan padanya.

Pada kasus ini, mari kita tambahkan index seperti dijelaskan di atas. Kita akan membuat tiga index pada empno. Satu index akan dibuat pada empno field di employees table. Satu index akan dibuat pada empno field di deptemp table, dan index terakhir pada emp_no field pada title table.


mysql -u root -p123456
USE employees_backup;
CREATE INDEX employees_empno ON employees(emp_no);
CREATE INDEX dept_emp_empno ON dept_emp(emp_no);
CREATE INDEX titles_empno ON titles(emp_no);
quit;

Kembali ke terminal, jalankan mysqlslap dengan paramter yang sama, kita akan melihat benchmark yang berbeda.

sudo mysqlslap --user=sysadmin --password --host=localhost  --concurrency=2 --iterations=2 --create-schema=employees_backup \
--query="/mysqlslap_tutorial/capture_queries.sql" --verbose

Output:

Benchmark
        Average number of seconds to run all queries: 55.869 seconds
        Minimum number of seconds to run all queries: 55.706 seconds
        Maximum number of seconds to run all queries: 56.033 seconds
        Number of clients running queries: 10
        Average number of queries per client: 1

Kita akan melihat bahwa terjadi perbaikan di average, minimum, dan maximum time untuk menjalankan query. Dari yang tadinya rata2 68 seconds, query sekarang di jalankan dalam 55 seconds. Ini perbaikan 13 seconds untuk beban yang sama.

Karena perubahan database menghasilkan hasil yang baik di lingkungan test, kita dapat menggunakannya di production database server. Perlu di ingat bahwa perubahan database selalu ada keuntungan dan kerugian.

Kita dapat mengulangi proses dari perintah test dan improvement di semua query yang kita lihat di log.

Troubleshooting - mysqlslap tidak menghasilkan Output

Jika perintah test tidak menghasilkan outout, ini adalah indikasi bahwa sumber daya / resource pada server sudah habis. Simptom seperti tidak ada Benchmark output, server mungkin hang atau error seperti

mysqlslap: Error when storing result: 2013 Lost connection to MySQL server during query.

Kita dapat mencoba test dengan parameter --concurrency atau --iterations yang lebih kecil. Alternatif lain, kita dapat meng-upgrade test server.

Ini adalah cara yang baik untuk mengetahui limit dari kapasitas database server yang kita miliki.

Penutup

mysqlslap adalah tool yang simple, light-weight dan sangat mudah digunakan yang terintegrasi dalam engine MySQL database.

Pada tutorial ini, kita dapat melihat bagaimana menggunakan mysqlslap dengan berbagai opsi-nya pada database contoh / sample. Kita dapat men-download berbagai sample database pada MySQL site dan mem-praktekan-nya.

Jangan pernah menjalankan / run test di production database server.

The last use case in this tutorial involved only one query. While we improved the performance of that query somewhat by adding extra indexes to all three tables, the process may not be so simple in real life. Adding extra indexes can sometimes degrade system performance and DBAs often need to weigh the benefits of adding an extra index against the performance cost it may incur.

Skenario Real life testing biasanya lebih kompleks, tapi ini harusnya cukup memberikan tool yang dibutuhkan untuk memulai test dan memperbaiki performance database.

Referensi