
Hôm nay Onet sẽ giới thiệu đến các bạn một công cụ dùng để benchmark hệ thống Linux (CPU, File IO, MySQL). Đó là lệnh sysbench.
Sysbench là một công cụ benchmark cho phép bạn nhanh chóng biết được hiệu năng hệ thống, điều này rất quan trọng nếu bạn có đang chạy một cơ sở dữ liệu dưới tải trọng lớn. Sysbench là nền benchmark chuẩn có thể tùy chỉnh cao và phổ biến nhất cho MySQL.
Bài viết này sẽ giới thiệu cách benchmark CPU, file IO, RAM và hiệu suất MySQL của hệ thống của bạn với sysbench.
1. Cài đặt
Trên Debian/Ubuntu:
apt-get update -y apt-get install sysbench -y
Trên CentOS/Fedora:
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY* yum -y install epel-release yum -y update yum install sysbench -y
Để biết thêm các thông số của lệnh sysbench, dùng lệnh:
man sysbench
Vài kiểu benchmark với sysbench:
CPU – Mất bao lâu để CPU tính được số chính tối đa (maximum primary number)? Bạn cũng có thể thêm bao nhiêu thread tham gia vào quá trình benchmark này. Đây là cách rất trực tiếp và dễ dàng để đánh giá hiệu năng của CPU.
File IO – Benchmark file IO gồm tất cả các tình huống như like direct io, sync, async, vv… kiểm tra đọc/ghi tuần tự và đọc/ghi ngẫu nhiên, Điều này làm cho Benchmark IO của diskrất có thể mở rộng.
OLTP – Benchmark MySQL bao gồm các truy vấn SELECT /INSERT / UPDATE / DELETE trên nhiều tình huống sử dụng như bulk data loading, các cập nhật range-based , xóa high volume data. Không có benchmark mysql nào có thể hoàn thành mà không có sysbench.
2. Benchmark CPU
Dùng lệnh:
sysbench --test=cpu --cpu-max-prime=20000 run
Output mẫu:
[root@localhost ~]# sysbench --test=cpu --cpu-max-prime=20000 run WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.17 (using system LuaJIT 2.0.4) Running the test with following options: Number of threads: 1 Initializing random number generator from current time Prime numbers limit: 20000 Initializing worker threads... Threads started! CPU speed: events per second: 279.75 General statistics: total time: 10.0012s total number of events: 2799 Latency (ms): min: 3.28 avg: 3.57 max: 11.95 95th percentile: 3.75 sum: 9993.02 Threads fairness: events (avg/stddev): 2799.0000/0.00 execution time (avg/stddev): 9.9930/0.00 [root@localhost ~]#
Bạn chú ý vào dòng total time: 10.0012s
Bạn cần phải so sánh trên nhiều hệ thống khác nhau để hiểu giá trị này có ý nghĩa như thế nào.
3. Benchmark File IO
Đầu tiên cần tạo 1 file lớn hơn nhiều so với RAM, tôi tạo 1 file 150GB.
sysbench --test=fileio --file-total-size=150G prepare
[root@localhost ~]# sysbench --test=fileio --file-total-size=150G prepare WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. sysbench 1.0.17 (using system LuaJIT 2.0.4) 128 files, 409600Kb each, 51200Mb total Creating files for the test... Extra file open flags: (none) Creating file test_file.0 Creating file test_file.1 Creating file test_file.2 Creating file test_file.3 Creating file test_file.4 Creating file test_file.5 Creating file test_file.6 Creating file test_file.7 Creating file test_file.8 Creating file test_file.9 Creating file test_file.10 ...
Chạy benchmark
sysbench --test=fileio --file-total-size=150G --file-test-mode=rndrw --init-rng=on --max-time=300 --max-requests=0 run
[root@localhost ~]# sysbench --test=fileio --file-total-size=150G --file-test-mode=rndrw --init-rng=on --max-time=300 --max-requests=0 run sysbench: /usr/lib/libmysqlclient.so.18: no version information available (required by sysbench) sysbench 0.4.12: multi-threaded system evaluation benchmark Running the test with following options: Number of threads: 1 Initializing random number generator from timer. Extra file open flags: 0 128 files, 1.1719Gb each 150Gb total file size Block size 16Kb Number of random requests for random IO: 0 Read/Write ratio for combined random IO test: 1.50 Periodic FSYNC enabled, calling fsync() each 100 requests. Calling fsync() at the end of test, Enabled. Using synchronous I/O mode Doing random r/w test Threads started! Time limit exceeded, exiting... Done. Operations performed: 600 Read, 400 Write, 1186 Other = 2186 Total Read 9.375Mb Written 6.25Mb Total transferred 15.625Mb (53.316Kb/sec) 3.33 Requests/sec executed Test execution summary: total time: 300.0975s total number of events: 1000 total time taken by event execution: 158.7611 per-request statistics: min: 0.01ms avg: 158.76ms max: 2596.96ms approx. 95 percentile: 482.29ms Threads fairness: events (avg/stddev): 1000.0000/0.00 execution time (avg/stddev): 158.7611/0.00 [root@localhost ~]#
Dòng bạn cần quan tâm là Read 9.375Mb Written 6.25Mb Total transferred 15.625Mb (53.316Kb/sec)
Xóa file 150 GB đã tạo:
sysbench --test=fileio --file-total-size=150G cleanup
4. Benchmark mysql
Truy cập vào mysql và tạo db tên sysbench
Sau đó, dùng lệnh sau để tạo 1000000 bản ghi
sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password='passwordmysql' --mysql-db=sysbench --db-driver=mysql --tables=2 --table-size=1000000 prepare
Thay đổi passwordmysql với password mysql của bạn.
Output mẫu:
[root@localhost ~]# sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password='passwordmysql' --mysql-db=sysbench --db-driver=mysql --tables=2 --table-size=1000000 prepare sysbench 1.0.17 (using system LuaJIT 2.0.4) Creating table 'sbtest1'... Inserting 1000000 records into 'sbtest1' Creating a secondary index on 'sbtest1'... Creating table 'sbtest2'... Inserting 1000000 records into 'sbtest2' Creating a secondary index on 'sbtest2'... [root@localhost ~]#
Trong đó:
--mysql-user
: user sử dụng mysql--mysql-password
: password của user--tables
: Số bảng tạo--table-size
: Số bản ghi trong 1 bảng
Kiểm tra DB vừa tạo:
mysql -u root -p > use sysbench; > show table status like 'sbtest%' G;
Mẫu:
mysql> show table status like 'sbtest%' G; *************************** 1. row *************************** Name: sbtest1 Engine: InnoDB Version: 10 Row_format: Compact Rows: 987448 Avg_row_length: 209 Data_length: 207290368 Max_data_length: 0 Index_length: 14172160 Data_free: 0 Auto_increment: 1000001 Create_time: 2020-05-13 16:55:39 Update_time: NULL Check_time: NULL Collation: latin1_swedish_ci Checksum: NULL Create_options: Comment: *************************** 2. row *************************** Name: sbtest2 Engine: InnoDB Version: 10 Row_format: Compact Rows: 986400 Avg_row_length: 228 Data_length: 225132544 Max_data_length: 0 Index_length: 14172160 Data_free: 0 Auto_increment: 1000001 Create_time: 2020-05-13 16:56:06 Update_time: NULL Check_time: NULL Collation: latin1_swedish_ci Checksum: NULL Create_options: Comment: 2 rows in set (0.00 sec) ERROR: No query specified
Đẩy tải MySQL:
sysbench /usr/share/sysbench/select_random_points.lua --table-size=2000000 --num-threads=100 --rand-type=uniform --db-driver=mysql --mysql-db=sysbench --mysql-user=root --mysql-password=passwordmysql --time=30 run
Output mẫu:
[root@localhost ~]# sysbench /usr/share/sysbench/select_random_points.lua --table-size=2000000 --num-threads=100 --rand-type=uniform --db-driver=mysql --mysql-db=sysbench --mysql-user=root --mysql-password=passwordmysql run WARNING: --num-threads is deprecated, use --threads instead sysbench 1.0.17 (using system LuaJIT 2.0.4) Running the test with following options: Number of threads: 100 Initializing random number generator from current time Initializing worker threads... Threads started! SQL statistics: queries performed: read: 82667 write: 0 other: 0 total: 82667 transactions: 82667 (8218.14 per sec.) queries: 82667 (8218.14 per sec.) ignored errors: 0 (0.00 per sec.) reconnects: 0 (0.00 per sec.) General statistics: total time: 10.0569s total number of events: 82667 Latency (ms): min: 0.15 avg: 12.11 max: 1714.04 95th percentile: 22.69 sum: 1001460.17 Threads fairness: events (avg/stddev): 826.6700/277.16 execution time (avg/stddev): 10.0146/0.01 [root@localhost ~]#
Thông số bạn cần quan tâm là transactions: 82667 (8218.14 per sec.)
Làm sạch DB sau khi test xong:
sysbench /usr/share/sysbench/oltp_read_write.lua --db-driver=mysql --mysql-db=sysbench --mysql-user=root --mysql-password='passwordmysql' --tables=1 cleanup
5. Benchmark RAM
Dùng lệnh:
sysbench --test=memory --num-threads=4 run
Output mẫu:
[root@localhost ~]# sysbench --test=memory --num-threads=4 run WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options. WARNING: --num-threads is deprecated, use --threads instead sysbench 1.0.17 (using system LuaJIT 2.0.4) Running the test with following options: Number of threads: 4 Initializing random number generator from current time Running memory speed test with the following options: block size: 1KiB total size: 102400MiB operation: write scope: global Initializing worker threads... Threads started! Total operations: 47007331 (4699425.66 per second) 45905.60 MiB transferred (4589.28 MiB/sec) General statistics: total time: 10.0006s total number of events: 47007331 Latency (ms): min: 0.00 avg: 0.00 max: 22.02 95th percentile: 0.00 sum: 19982.18 Threads fairness: events (avg/stddev): 11751832.7500/122424.02 execution time (avg/stddev): 4.9955/0.08 [root@localhost ~]#
Bạn cần chú ý đến dòng:
Total operations: 47007331 (4699425.66 per second)
45905.60 MiB transferred (4589.28 MiB/sec)
Kết luận
Như vậy chúng tôi đã giới thiệu đến cho bạn vài bài test cơ bản với sysbench. Chúc các bạn thành công!!
