Home Database Mysql Tutorial mysql测试小工具mybench试用

mysql测试小工具mybench试用

Jun 07, 2016 pm 04:32 PM
mysql tool test try out

小型的mysql测试工具,主要有自带的mysqlslap、super-smack和mybench。嗯,我这里的小型的意思是指工具安装过程简单。mysqlslap的使用方法遍地都是,就不先详细写了。根据个人偏好写写mybench吧,毕竟是perl的。安装很简单,如下: cpanm DBI DBD::mysql Tim

小型的mysql测试工具,主要有自带的mysqlslap、super-smack和mybench。嗯,我这里的小型的意思是指工具安装过程简单。 mysqlslap的使用方法遍地都是,就不先详细写了。根据个人偏好写写mybench吧,毕竟是perl的。 安装很简单,如下:

cpanm DBI DBD::mysql Time::HiRes
wget http://jeremy.zawodny.com/mysql/mybench/mybench-1.0.tar.gz
tar zxvf mybench-1.0.tar.gz
cd mybench-1.0
perl MakeFile.PL && make && make install
Copy after login

但是使用就不是太简单了——mysqlslap会自己生成(-a选项)sql,super-smack则带了一个gen-data程序生成数据然后自动导入,但是mybench没有,所以只能自己搞定数据。 不过mybench还是自己生成了一个测试模版的脚本在/usr/bin/bench_example,很简单的就知道怎么做了。 example如下:

#!/usr/bin/perl -w
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
use strict;
use MyBench;
use Getopt::Std;
use Time::HiRes qw(gettimeofday tv_interval);
use DBI;
my %opt;
Getopt::Std::getopt('n:r:h:', \%opt);
#这是我见过的最hardcode的perl脚本了(呃,除了我自己写的垃圾),连db库、用户名、密码都不给运行参数的
my $num_kids  = $opt{n} || 10;
my $num_runs  = $opt{r} || 100;
my $db        = "test";
my $user      = "test";
my $pass      = "";
my $port      = 3306;
my $host      = $opt{h} || "192.168.0.1";
my $dsn       = "DBI:mysql:$db:$host;port=$port";
my $callback = sub
{
    my $id  = shift;
    my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 });
#为测试准备的请求,测select就写select,测insert就写insert呗~
#如果不修改,也就是说测试用的是test.mytable表,而且必须有一个列叫id
    my $sth = $dbh->prepare("SELECT * FROM mytable WHERE ID = ?");
    my $cnt = 0;
    my @times = ();
    ## wait for the parent to HUP me
    local $SIG{HUP} = sub { };
    sleep 600;
#脚本定义的每个进程执行多少次请求
    while ($cnt execute($v);
#通过前后两次gettimeofday获得sql的exec耗时
        my $t1 = tv_interval($t0, [gettimeofday]);
#完成一次请求执行,加入数组
        push @times, $t1;
        $sth->finish();
        $cnt++;
    }
    ## cleanup
    $dbh->disconnect();
#计算本进程全部请求的各项数据,几个大小和均来自MyBench模块
    my @r = ($id, scalar(@times), min(@times), max(@times), avg(@times), tot(@times));
    return @r;
};
#将上面这个函数交给MyBench模块的fork_and_work执行,即并发指定数量请求,返回总的结果
my @results = MyBench::fork_and_work($num_kids, $callback);
#计算总的数据
MyBench::compute_results('test', @results);
exit;
__END__
Copy after login

然后看看/usr/lib/perl5/site_perl/5.8.8/MyBench.pm,主要内容就是fork和compute:

package MyBench;
use strict;
$main::VERSION = '1.0';
use Exporter;
@MyBench::ISA = 'Exporter';
#导出求最大值、最小值、平均值、综合值的函数给外面用
@MyBench::EXPORT = qw(max min avg tot);
sub fork_and_work($$)
{
#关闭输出缓冲
    $|=1;
    use strict;
    use IO::Pipe;
    use IO::Select;
    $SIG{CHLD} = 'IGNORE';      ## let the kids die
    my $kids_to_fork = shift;
    my $callback     = shift;
    my $num_kids     = 0;
    my @pipes        = ();
    my @pids         = ();
    my $pid          = undef;
    print "forking: ";
    while ($num_kids reader();
            push @pipes, $pipe;
            push @pids,  $pid;
        }
        elsif (defined $pid)
        {
            ## child
#打开管道写入数据的功能
            $pipe->writer();
#执行select_example脚本传入的mysql请求测试函数
            my @result = $callback->($num_kids);
#把结果写入管道
            print $pipe "@result\n";
#关闭管道
            $pipe->close();
            exit 0;
        }
        else
        {
            print "fork failed: $!\n";
        }
    }
    print "\n";
    ## give them a bit of time to setup
    my $time = int($num_kids / 10) + 1;
    print "sleeping for $time seconds while kids get ready\n";
    sleep $time;
 #发送SIGHUP信号给callback函数
    kill 1, @pids;
    ## collect the results
    my @results;
    print "waiting: ";
#从管道中读取数据到数组
    for my $pipe (@pipes)
    {
        my $data = ;
        push @results, $data;
        $pipe->close();
        print "-";
    }
    print "\n";
    return @results;
}
sub compute_results(@)
{
    my $name = shift;
    my $recs = 0;
    my ($Cnt, $Min, $Max, $Avg, $Tot, @Min, @Max);
    while (@_)
    {
        ## 6 elements per record
        my $rec = shift; chomp $rec;
        my ($id, $cnt, $min, $max, $avg, $tot) = split /\s+/, $rec;
        $Cnt += $cnt;
        $Avg += $avg;
        $Tot += $tot;
        push @Min, $min;
        push @Max, $max;
        $recs++;
    }
    $Avg = $Avg / $recs;
    $Min = min(@Min);
    $Max = max(@Max);
    my $Qps = $Cnt / ($Tot / $recs);
    print "$name: $Cnt $Min $Max $Avg $Tot $Qps\n";
    print "  clients : $recs\n";
    print "  queries : $Cnt\n";
    print "  fastest : $Min\n";
    print "  slowest : $Max\n";
    print "  average : $Avg\n";
    print "  serial  : $Tot\n";
    print "  q/sec   : $Qps\n";
}
## some numerical helper functions for arrays
sub max
{
    my $val = $_[0];
    for (@_)
    {
        if ($_ > $val) { $val = $_; }
    }
    return $val;
}
sub min
{
    my $val = $_[0];
    for (@_)
    {
        if ($_ 
<p>好了,开始准备数据,比较懒,直接用super-smack的gen-data先出了一些./gen-data  -n 100000 -f %n,%80-12s%12n,%512-512s,%d > /root/data,然后进mysql里执行:</p>
<p class="highlight"></p><pre class="brush:php;toolbar:false">USE test;
CREATE TABLE mytable (id INT(11) NOT NULL AUTO_INCREMENT, col1 CHAR(100), col2 CHAR(100), col3 INT(11), PRIMARY KEY (id) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOAD DATA LOCAL INFILE 'data' REPLACE INTO TABLE 'mytable' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
INSERT INTO mytable (col1,col2,col3) SELECT col1,col2,col3 FROM mytable;
Copy after login

最后执行./select_bench -h 10.168.170.92 -n 10 -r 1000就能看到结果了: forking: ++++++++++ sleeping for 2 seconds while kids get ready waiting: ———- test: 10000 0.00017 0.006809 0.0010413514 10.413514 9602.9063772325 clients : 10 queries : 10000 fastest : 0.00017 slowest : 0.006809 average : 0.0010413514 serial : 10.413514 q/sec : 9602.9063772325

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
How to enter the bull market in May 2025? Recommended on entry exchanges How to enter the bull market in May 2025? Recommended on entry exchanges May 12, 2025 pm 08:51 PM

Recommended exchanges for bull market in May 2025: 1. Binance; 2. OKX; 3. Huobi; 4. gate.io; 5. Sesame Open Door and other exchanges. The above exchanges are safe and reliable, and support a variety of currencies. This article provides detailed download portal addresses.

Recommended for Bull Market Exchange in 2025 Recommended for Bull Market Exchange in 2025 May 12, 2025 pm 08:45 PM

Recommended exchanges for bull market in May 2025: 1. Binance; 2. OKX; 3. Huobi; 4. gate.io; 5. Sesame Open Door and other exchanges. The above exchanges are safe and reliable, and support a variety of currencies. This article provides detailed download portal addresses.

How to set, get and delete WordPress cookies (like a professional) How to set, get and delete WordPress cookies (like a professional) May 12, 2025 pm 08:57 PM

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

How to add your WordPress site in Yandex Webmaster Tools How to add your WordPress site in Yandex Webmaster Tools May 12, 2025 pm 09:06 PM

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

The latest ranking of the top ten cryptocurrency trading platforms in 2025 virtual currency exchange apps The latest ranking of the top ten cryptocurrency trading platforms in 2025 virtual currency exchange apps May 12, 2025 pm 08:12 PM

The latest rankings of the top ten cryptocurrency trading platforms: 1. OKX, 2. Binance, 3. Huobi, 4. Coinbase, 5. Kraken, 6. Bitfinex, 7. KuCoin, 8. Gemini, 9. Crypto.com, 10. Bitstamp, these applications are highly regarded for their user experience, security, functionality and market reputation.

How to Fix WordPress 429 Too Many Request Errors How to Fix WordPress 429 Too Many Request Errors May 12, 2025 pm 08:54 PM

Do you see the "429 too many requests" error on your WordPress website? This error message means that the user is sending too many HTTP requests to the server of your website. This error can be very frustrating because it is difficult to find out what causes the error. In this article, we will show you how to easily fix the "WordPress429TooManyRequests" error. What causes too many requests for WordPress429? The most common cause of the "429TooManyRequests" error is that the user, bot, or script attempts to go to the website

The latest ranking of the top ten digital currency exchange apps Recommended the top ten digital currency exchange apps The latest ranking of the top ten digital currency exchange apps Recommended the top ten digital currency exchange apps May 12, 2025 pm 08:15 PM

The latest rankings of the top ten digital currency exchange apps are: 1. OKX, 2. Binance, 3. Huobi, 4. Coinbase, 5. Kraken, 6. KuCoin, 7. Bitfinex, 8. Gemini, 9. Bitstamp, 10. Poloniex. The steps to using these apps include: downloading and installing the app, registering an account, completing KYC certification, top-up and starting a transaction.

Top 10 Virtual Currency Exchange Apps Rankings in 2025, Top 10 latest rankings of digital currency trading apps Top 10 Virtual Currency Exchange Apps Rankings in 2025, Top 10 latest rankings of digital currency trading apps May 12, 2025 pm 08:18 PM

The top ten virtual currency exchange apps in 2025 are ranked as follows: 1. OKX, 2. Binance, 3. Huobi, 4. Coinbase, 5. Kraken, 6. KuCoin, 7. Bybit, 8. FTX, 9. Bitfinex, 10. Gate.io. These exchanges are selected based on dimensions such as user experience, security and transaction volume. Each platform provides unique functions and services to meet the needs of different users.

See all articles