Super Smack安装和使用

上一篇 / 下一篇  2014-09-27 21:42:24 / 个人分类:MySQL

1.安装Super-smack 现在是1.3版,由于1.3版本没有资源,所以用的是1.2版本。源码下载地址如下:http://jeremy.zawodny.com/mysql/super-smack/super-smack-1.2.tar.gz
放在/root/weic下
解压:
[root@cmp ~]# cd /usr/local/src
[root@cmp src]# tar zxvf super-smack-1.2.tar.gz
[root@cmp src]# cd super-smack-1.2
配置
[root@cmp super-smack-1.2]#./configure --with-smacks-dir=/usr/local/supersmacks/ --with-datadir=/usr/local/supersmacks --prefix=/usr/local/supersmacks --with-mysql-

lib=/usr/local/mysql/lib --with-mysql
编译
[root@cmp super-smack-1.2]# make
安装
[root@cmp super-smack-1.2]# make install
复制libmysqlclient 到os 的lib位置
[root@cmp super-smack-1.2]# cp /usr/local/mysql/lib/mysql/libmysqlclient* /usr/lib

生成数据:

[root@localhost supersmacks]# ./gen-data -n 9000 -f %12-12s%n,%25-25s,%n,%d > /usr/local/supersmacks/words.dat

2.使用
[root@cmp super-smack-1.2]# cd
[root@cmp ~]# cd /usr/local/supersmacks
[root@cmp supersmacks]# bin/super-smack -d mysql select-key.smack 10 10000
10 是连接客户的数目,每个客户有10000次轮询。
3.结果
Query Barrel Report for client smacker1connect: max=66ms min=0ms avg= 66ms from 10 clientsQuerytype numqueries maxtime mintime qpersselect_index 20000 0 0 4360.61
这表示 super-smack 运行了10个客户,客户连接最长时间为66毫秒,最短的则没有延迟,平均时间为62毫秒。Super-smack 总共执行了20000此查询,每个查询所花时间都小于1毫秒。
整个测试期间,服务端平均每秒执行了4360.61次 select_index 型查询。另外,可以修改 smack文件中 相应的参数以符合你的测试要求。


常见错误及解决方法:
[root@cmp smacks]# bin/super-smack -d mysql select-key.smack 10 10000
super-smack: aborting because of a database error: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
这个问题主要提示是,不能通过 '/var/lib/mysql/mysql.sock'连到服务器,而mysql标准配置正是用过'/tmp/mysql.sock',
但是一些mysql安装方法将 mysql.sock放在/var/lib/mysql.sock或者其他的什么地方,你可以通过修改/etc/my.cnf文件来修正它,打开文件,可以看到如下的东东:
[mysqld]
socket=/var/lib/mysql.sock
改一下就好了,但也会引起其他的问题,如mysql程序连不上了,再加一点:
[mysql]
socket=/tmp/mysql.sock
或者用这样的方法:
ln -s /usr/local/mysql/data/mysql.sock  /var/lib/mysql/mysql.sock          //我的mysql.sock安装在了/usr/local/mysql/data/目录下
[root@cmp ~]# cd /usr/local/mysql
[root@cmp mysql]# ln -s /usr/local/mysql/data/mysql.sock  /var/lib/mysql/mysql.sock

或者是直接修改select-key.smack:
socket "/var/lib/mysql/mysql.sock";为
socket "/usr/local/mysql/data/mysql.sock";
启动supre-smack工具:
[root@cmp ~]# cd /usr/local/supersmacks
[root@cmp supersmacks]# bin/super-smack -d mysql select-key.smack 10 10000

[root@localhost bin]# ./super-smack -d mysql select-key.smack 10 10
super-smack: Could not open input file select-key.smack errno = 2, error is No such file or directory
出现这个问题是因为命令没有找到select-key.smack文件,跳转到select-key.smack所在目录执行就可以了:
[root@localhost bin]# cd ..
[root@localhost supersmacks]# ll
total 5336
drwxr-xr-x 2 root root    4096 Sep 28 23:10 bin
-rwxrwxrwx 1 1002 1002    3224 Sep 28 19:06 select-key.smack
-rwxrwxrwx 1 1002 1002    3547 Feb  2  2001 update-select.smack
-rw-r--r-- 1 root root 5421337 Sep 28 23:16 words.dat
[root@localhost supersmacks]# ./bin/super-smack -d mysql select-key.smack 10 10
Table 'http_auth' does not meet condtions, will be dropped
Creating table 'http_auth'
Loading data from file '/usr/local/supersmacks/words.dat' into table 'http_auth'
Table http_auth is now ready for the test
Query Barrel Report for client smacker1
connect: max=109ms  min=2ms avg= 28ms from 10 clients
Query_type      num_queries     max_time        min_time        q_per_s
select_index    200     0       0       1165.06
定义自己的测试配置文件

要做自定义的MySQL测试,当然就必须编辑自己的配置文件(.smack)。Super Smack之所以很灵活,就在于可以在它的配置文件中自定义很多的东西。当然,随着而来的就是配置文件的相对复杂。理解它的配置文件的结构,是学会用Super Smack的关键所在。

下文将以select-key.smack为例,讲解.smack配置文件的结构。
网上也有一些相关的link:http://imysql.cn/docs/High_Performance_MySQL/0596003064/hpmysql-CHP-3-SECT-3.html

client "admin"
{
 user "root";
 host "localhost";
 db "test";
 pass "";
 socket "/data0/mysql/mysql.sock"; // this only applies to MySQL and is
// ignored for PostgreSQL
}

这部分定义了Super Smack连接数据库需要用到的一些信息,比如用户名,机器名,数据库名,密码等等。很容易理解,不解释。

// ensure the table exists and meets the conditions
table "http_auth"
{
  client "admin"; // connect with this client
 // if the table is not found or does not pass the checks, create it
 // with the following, dropping the old one if needed
  create "create table http_auth
    (username char(25) not null primary key,
     pass char(25),
     uid integer not null,
     gid integer not null
    )";
  min_rows "90000"; // the table must have at least that many rows
  data_file "words.dat"; // if the table is empty, load the data from
//this file
  gen_data_file "gen-data -n 90000 -f %12-12s%n,%25-25s,%n,%d";
// if the file above does not exist, generate it with the above shell command
// you can replace this command with anything that prints comma-delimited
// data to stdout, just make sure you have the right number of columns
}


这里定义了要进行测试的数据库表的信息。包括:
创建这个表所使用的sql语句。如果Super Smack发现数据库中没有这个表的话,会使用这条sql语句创建表。
注意,这里设置了client "admin",表明是用前面定义的admin这个client来创建表。
min_rows这个参数是一个对表的约束,要求表内的记录必须达到的数量。例子中的值是90000,表示表中必须要有90000条数据。
“ data_file "words.dat";  ”是定义了一个数据文件,这个文件中的每一行都可以作为表中的一条记录。如果表http_auth没有达到90000条数据,那么Super Smack会从words.dat中读取数据然后插入到表http_auth中。
gen_data_file "gen-data -n 90000 -f %12-12s%n,%25-25s,%n,%d"; -- 自动生成测试数据,如果words.dat为空的话,Super Smack使用这条语句生成测试数据然后写入到words.dat中。

gen-data是安装Super Smack后提供的一个产生随机数据的小程序。下面解读一下它的命令格式:
-n 90000表示生成90000条记录;
-f后面跟的是记录的格式;
%S表示产生字符串,%12-12s表示产生的字符串长度在12与12之间;
%D表示生成数字,%d生成随机数;
%N %n表示可以从1开始递增的数值,可用于表中的主键,比如第一条记录的%n就会被1代替,以此类推。
上面的这句“gen-data -n 90000 -f %12-12s%n,%25-25s,%n,%d” 输出的每行是大概这样的:
josamibdnjdb3,eyhkbsombltouujdrbwcrrcgb,3,485560280
qwdqweqwdwev4,regergergftyyujeqwqwecdfr,4,239013239
rhbtjrbtywlf5,edeffkergpqqmcxvkrtmbhwer,5,233021393
默认gen-data的输出都是打印了标准输出上,可以用重定向让gen-data的输出结果写入到文件中。

需要指出的是,这里采用的例子select-key.smack是对一个表做查询测试,所以要求table中必须首先有一定量的数据。如果想做插入的测试,完全可以将min_rows设置成0,这样,Super Smack也就不需要调用gen_data_file产生数据。

//define a dictionary
dictionary "word"
{
  type "rand"; // words are retrieved in random order
  source_type "file"; // words come from a file
  source "words.dat"; // file location
  delim ","; // take the part of the line before ,
  file_size_equiv "45000"; // if the file is greater than this
//divive the real file size by this value obtaining N and take every Nth
//line skipping others. This is needed to be able to target a wide key
// range without using up too much memory with test keys


//define a query
query "select_by_username"
{
  query "select * from http_auth where username = '$word'";
// $word will be substitute with the read from the 'word' dictionary
  type "select_index";
// query stats will be grouped by type
  has_result_set "y";
// the query is expected to return a result set
  parsed "y";
// the query string should be first processed by super-smack to do
// dictionary substitution
}


dictionary和query组合在一起,定义了需要的测试查询语句。
一条普通的查询语句,比如:select * from http_auth where username = 'test'; 它其实是由一个模板和一个或者几个参数构成的。select * from http_auth where username = '$word'就是这条语句的模板,而$word = test则是设置参数。
query部分定义了查询语句的模板,而dictionary则产生随机的各种参数。

query的结构很简单,最重要的就是“ query "select * from http_auth where username = '$word'"; ”,这定义了查询的模板;

dictionary的结构则非常的灵活,它可以有多种配置:
source_type "file"; -- 表示这个dictionary是一个文件的形式;
source "words.dat"; -- 表示文件名为words.dat;
delim ","; -- 表示words.dat中第一个","之前的部分是这个dictionary的内容;
type:rand -- 表示随机的从中抽取值;
把它们综合起来理解。
之前看过,words.dat的数据是这样的:
josamibdnjdb3,eyhkbsombltouujdrbwcrrcgb,3,485560280
qwdqweqwdwev4,regergergftyyujeqwqwecdfr,4,239013239
rhbtjrbtywlf5,edeffkergpqqmcxvkrtmbhwer,5,233021393
那么,这里定义的dictionary其实是读取word.dat,并将每一行的第一个字符串(比如josamibdnjdb3)作为字典内的元素,再和query组合起来,就变成了实际的查询语句:
select * from http_auth where username = 'qwdqweqwdwev4'
select * from http_auth where username = 'rhbtjrbtywlf5'
select * from http_auth where username = 'josamibdnjdb3'

type除了可以定义为rand,还可以定义为
    seq,表示Values are used sequentially;
    unique,Generate unique values using the same method as gen-data;
source_type除了可以定义为 file,还可以定义为
    list,表示a user-supplied list of words, comma-separated;
    template,表示the format to use when type is unique. For example, "jzawodn_%07d" generates values composed of jzawodn_ and a seven-digit number

举例来说,可以这样定义:
dictionary "gender" {
  type: rand
  source_type "list";
  source "male,female";
}

它表示这个gender的dictionary只有两个值,要么是male,要么是female
dictionary "name"
{
  type "unique";
  source_type "template";
  source "%15s";
}

表示name这个dictionary是一个长度为15的字符串,Super Smack将用gen-data命令产生这个字符串。

很多时候,如果你的查询模板需要多个参数的话,那么你可能需要准备多个dictionary。比如要产生如 select * from tablename where age < 20 AND id = 1234这样的查询,就需要两个dictionary,一个是age,一个是id。
当然,还有一种讨巧的方案,只需要生成一个dictionary,定义它的每一行是这样的:
Age < *** AND id = ***
而对应的query模板就是
select * from tablename where dictionary;


// define database client type
client "smacker1"
{
 user "test"; // connect as this user
 pass ""; // use this password
 host "localhost"; // connect to this host
 db "test"; // switch to this database
 socket "/data0/mysql/mysql.sock"; // this only applies to MySQL and is
// ignored for PostgreSQL
 query_barrel "2 select_by_username"; // on each round,
// run select_by_username query 2 times
}

main
 {
  smacker1.init(); // initialize the client
  smacker1.set_num_rounds($2); // second arg on the command line defines
// the number of rounds for each client
  smacker1.create_threads($1);
// first argument on the command line defines how many client instances
// to fork. Anything after this will be done once for each client until
// you collect the threads
  smacker1.connect();
// you must connect after you fork
  smacker1.unload_query_barrel(); // for each client fire the query barrel
// it will now do the number of rounds specified by set_num_rounds()
// on each round, query_barrel of the client is executed

  smacker1.collect_threads();
// the master thread waits for the children, each child reports the stats
// the stats are printed
  smacker1.disconnect();
// the children now disconnect and exit
 }


这里又定义了一个client,它是执行查询语句所用到的client(最早的client是创建表的client,这两个client权限应该不一样。)
query_barrel "2 select_by_username"; -- 表示这个client在一次循环中执行select_by_username这个查询两次;
main中则定义了Super Smack具体的执行流程。很简单,不解释。

TAG:

 

评分:0

我来说两句

jiahou166

jiahou166

立志要如山,行道要如水。不如山,不能坚定,不如水,不能曲达。

日历

« 2024-03-28  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 76054
  • 日志数: 87
  • 建立时间: 2012-04-02
  • 更新时间: 2021-05-29

RSS订阅

Open Toolbar