HandlerSocket的安装实例及性能测试

 

一 HandlerSocket简介

Hanldersocket是一个MySQL守护进程插件,它让应用程序可以将MySQL当NoSQL使,Hanldersocket的主要目的是与存储引擎,如InnoDB交互,而不需要SQL相关的开销。访问MySQL表时,Hanldersocket仍然需要打开和关闭表,但不是每次访问都要求打开和关闭,因此减少了互斥争夺,极大地提高了系统性能,当流量变小时,Hanldersocket会关闭表,因此它永远不会阻止管理命令(DDL)。

 

下图是MySQL+Memcached的常见架构模式:

 

二 测试环境准备

(一) 源码安装mysql5.5.x

准备工作:

新建用户和用户组

groupadd mysql

useradd -g mysql mysql

 

1:下载:

cmake-2.8.3.tar.gz

make-3.82.tar.gz

mysql-5.5.8.tar.gz

 

2:解压安装前2个包

 

tar zxvf  cmake-2.8.3.tar.gz

cd cmake-2.8.3

./configure

make

make install

tar zxvf make-3.82.tar.gz

cd make-3.8.2

./configure

make

make install

 

3:解压并安装mysql-5.5.8.tar.gz

tar zxvf mysql-5.5.8.tar.gz

cd mysql-5.5.8

 

cmake . \

-DCMAKE_INSTALL_PREFIX=/mysql/mysqldir \

-DMYSQL_DATADIR=/mysql/mysqldir/data \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DMYSQL_TCP_PORT=3306 \

-DMYSQL_UNIX_ADDR==/mysql/mysqldir/data/mysql.sock \

-DMYSQL_USER=mysql \

-DWITH_DEBUG=0

 

make

make install

 

完成后进入MYSQL安装目录

 

cd /mysql/mysqldir

cp support-files/ my-medium.cnf /etc/my.cnf

cp support-files/mysql.server /etc/init.d/mysql.

vi /etc/my.cnf

将数据目录和套接字文件修改为实际值.

 

修改权限

chown mysql:mysql /etc/my.cnf

chown mysql:mysql /etc/init.d/mysql.

chown -R mysql:mysql /mysql/mysqldir

 

设置root用户密码

mysqladmin –uroot password “newpassword”

 

切换用户进入安装目录

su - mysql

cd /mysql/mysqldir

cp scripts/mysql_install_db .

./mysql_install_db

 

启动MYSQL

/etc/init.d/mysql start

(二) 安装HandlerSocket插件

大概安装步骤如下:
1 .下载

http://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL

2.编译 HandlerSocket客户端和服务器端程序:

./configure --with-mysql-source=... --with-mysql-bindir=... ; make; make install

在my.cnf中加入以下内容:

[mysqld]

  loose_handlersocket_port = 9998

    # the port number to bind to (for read requests)

  loose_handlersocket_port_wr = 9999

    # the port number to bind to (for write requests)

  loose_handlersocket_threads = 16

    # the number of worker threads (for read requests)

  loose_handlersocket_threads_wr = 1

    # the number of worker threads (for write requests)

  open_files_limit = 65535

    # to allow handlersocket accept many concurrent

    # connections, make open_files_limit as large as

    # possible.

3.安装插件

mysql> INSTALL PLUGIN 'HandlerSocket' SONAME 'HandlerSocket.so';

mysql>SHOW PLUGINS; # 查看插件是否加载成功

4.安装perl客户端

cd  perl-Net-HandlerSocket/

perl Makefile.PL ;

make ;

make test ;

make install ;

安装完毕,重启mysql server

/etc/init.d/mysql restart;

 

注:不需要修改mysql的源代码。
mysql需要5.1或者以后版本。

(三) 安装Perl的相关模块DBI、DBD

1.安装DBI

下载DBI-1.609.tar.g;

tar –xvzf DBI-1.609.tar.gz;

cd DBI-1.609;

perl Makefile.PL ;

make;

make test;

make install;

2.安装DBD

下载DBI-1.609.tar.g;

tar –xvzf DBD-mysql-4.012.tar.gz;

cd DBD-mysql-4.012;

perl Makefile.PL ;

make;

make test;

make install;

三 测试

(一) 创建测试数据库及测试表

CREATE DATABASE test;

CREATE TABLE `test` (

 `test_id` int(10) unsigned NOT NULL,

 `test_score` int(10),

 PRIMARY KEY (`test_id`),

 KEY `INDEX_01` (`test_score`)

 ) ENGINE=InnoDB;

(二) 插入数据脚本

1.HandlerSocket插入脚本

HandlerInsert.pl:

 

#!/usr/bin/perl

 

use strict;

use warnings;

 

use Net::HandlerSocket;

my $args = { host => 'localhost', port => 9997 };

my $hs = new Net::HandlerSocket($args);

my $res = $hs->open_index(0, 'test', 'test', 'PRIMARY', 'test_id,test_score');

die $hs->get_error() if $res != 0;

 

#INSERT

for (my $line = 1; $line < $ARGV[0]; ++$line)

{

    $res = $hs->execute_single(0,'+', [ $line, $line + 1 ],1,0);

}

print "$ARGV[0] rows affected\n";

 

$hs->close();

 

使用说明:
./HandlerInsert.pl 需要插入的记录数

如:

./HandlerInsert.pl 100000

则插入100000条记录。

 

2.mysql插入数据脚本

MysqlInsert.pl:

#!/usr/bin/perl

 

#load module

use DBI;

 

#connect

my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "root", "uestc8020", {'RaiseError'

=> 1});

 

#execute INSERT query

 

for (my $line = 0; $line < $ARGV[0]; ++$line)

{

    my $rows = $dbh->do("INSERT INTO test (test_id, test_score) VALUES ($line, $line + 1)");

}

print "$ARGV[0] row(s) affected\n";

 

# clean up

$dbh->disconnect();

使用说明:
./ MysqlInsert.pl 需要插入的记录数

如:

./ MysqlInsert.pl 100000

则插入100000条记录。

(三) 查询数据脚本

1.HandlerSocket查询脚本

HandlerQuery.pl:

#!/usr/bin/perl

use strict;

use warnings;

use Net::HandlerSocket;

#1. establishing a connection

my $args = { host => 'localhost', port => 9998 };

my $hs = new Net::HandlerSocket($args);

 

#2. initializing an index so that we can use in main logics.

# MySQL tables will be opened here (if not opened)

my $res = $hs->open_index(0, 'test', 'test', 'INDEX_01', 'test_id,test_score');

die $hs->get_error() if $res != 0;

 

#3. main logic

#fetching rows by id

#execute_single (index id, cond, cond value, max rows, offset)

for (my $number = 0; $number < $ARGV[0]; ++$number)

{

    $res = $hs->execute_single(0, '=', [ 500000 ], 1, 0);

}

#4. closing the connection

$hs->close();

./ HandlerQuery.pl 查询次数

如:

./ HandlerQuery.pl 100000

查询100000次。

2.mysql查询脚本

MysqlQuery.pl:

#!/usr/bin/perl

 

#load module

use DBI;

 

#connect

my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "root", "uestc8020", {'RaiseError'

=> 1});

 

 

#execute SELECT query

for (my $number = 0; $number < $ARGV[0]; ++$number)

{

    my $sth = $dbh->prepare("SELECT * from  test where test_score = 500000");

    $sth->execute();

}

 

$dbh->disconnect();

./ MysqlQuery.pl 查询次数

如:

./ MysqlQuery.pl 100000

查询100000次。

(四) 测试结果

1.插入数据

插入100万条数据所需要的时间

HandlerSocket:

Mysql:

   

插入数据性能(消耗时间)、

   

2.查询数据

进行10万次查询所需要的时间

HandlerSocket:

Mysql:

 

 

查询数据性能(QPS)

    

 

总结

对于插入时的QPS,HandlerSocket内部采用的是Bulk Insert操作,按理来说HandlerSocket应该占有很大优势。但从测试结果可以看出,插入时的QPS上HandlerSocket与Mysql差距不打,稍微占有优势,这和网上的测试结果有一定的出入。笔者认为,这可能是因为插入的数据量有关,由于相关的硬件限制,这次测试最多只插入了100万条记录,而网上的测试则是上千万的数据量。

对于查询时的QPS,测试结果与网上的测试相符,从可以看出,HandlerSocket明显占有比较大的优势,这与它的实现方式有关。

发布了41 篇原创文章 · 获赞 136 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/songguangfan/article/details/87294344