NinGoo@Net --- Just a simple Oracle & MySQL DBA
常用标签:

编写插件修改Wordpress的RSS输出

Wordpress是一款著名的开源blog平台,基于php,其灵活的模板(theme)和插件(plugin)架构,使得扩展性非常的好,通过模板可以随心所欲的变化前端展示,通过插件则可以实现额外的功能。比如可以通过插件修改rss输出,在其中加上诸如版权声明,相关文章等功能。另外,强烈建议在rss全文输出内容,经常在Google Reader中看到一篇好文章却只有摘要,点过去发现网站又无法打开,这种感觉真让人抓狂。

MyWordpressFeed.txt下载后另存为MyWordpressFeed.php,修改FeedRelatedPost函数中相应的地方,传到wp-content/plugins目录,到后台管理激活MyWordpressFeed插件,即可获得和我的blog一样的rss输出效果。php我只是略知皮毛,代码贴出来给有需要的朋友参考(Update:贴了半天代码,发现coolcode插件无法解析这种php里又输出html的代码,只好打包成txt文件,点击这里可查看或者下载)。

MySQL的benchmark函数

MySQL实现了很多独特的函数,有时候使用起来是非常的方便,或许这就是开源的好处吧。这里记录一下benchmark函数,一个用于测试MySQL函数性能的函数。benchmark函数只有两个参数,第一个是执行次数,第二个是要测试的函数或者表达式。返回的结果始终是0,执行时间才是我们需要的结果:

mysql> select benchmark(1e8,current_date());
+-------------------------------+
| benchmark(1e8,current_date()) |
+-------------------------------+
|              0 |
+-------------------------------+
1 row in set (31.08 sec)

mysql> select benchmark(1e8,abs(1));           
+-----------------------+
| benchmark(1e8,abs(1)) |
+-----------------------+
|          0 |
+-----------------------+
1 row in set (1.31 sec)

除法的效率明显不如乘法:

mysql> select benchmark(1e8,1*1); 
+--------------------+
| benchmark(1e8,1*1) |
+--------------------+
|        0 |
+--------------------+
1 row in set (1.45 sec)

mysql> select benchmark(1e8,1/1); 
+--------------------+
| benchmark(1e8,1/1) |
+--------------------+
|        0 |
+--------------------+
1 row in set (19.47 sec)

Oracle11g部分新特性移植到9i和10g

Oracle10.2.0.4出来的时候包含了属于Oracle11g的新特性Real Application Testing,现在,通过patch,可以在9i~10g各版本中获得包括SQL Performance AnalyzerDatabase Replay在内的Oracle11g新特性。

SQL Performance Analyzer可用于诊断由于系统改变等引起的SQL性能问题,通过在做出改变前后收集一个性能统计报告,然后比较得到性能改变的主要原因。

Database Replay则可以捕获产品数据库的压力等,在测试数据库中进行重演,可以更加准确的模拟产品库的压力对应用进行测试,这也是Oracle11g主推的Real Application Testing的主要功能。

要获得这两个新特性,需要安装Patch,对应的Patch请参考:Metalink Note:560977.1

MySQL压力测试工具mysqlslap

MySQL从5.1.4版开始带有一个压力测试工具mysqlslap,通过模拟多个并发客户端访问mysql来执行测试,使用起来非常的简单。通过mysqlslap –help可以获得可用的选项,这里列一些主要的参数,更详细的说明参考官方手册

–auto-generate-sql, -a
自动生成测试表和数据

–auto-generate-sql-load-type=type
测试语句的类型。取值包括:read,key,write,update和mixed(默认)。

–number-char-cols=N, -x N
自动生成的测试表中包含多少个字符类型的列,默认1

–number-int-cols=N, -y N
自动生成的测试表中包含多少个数字类型的列,默认1

–number-of-queries=N
总的测试查询次数

–query=name,-q
使用自定义脚本执行测试,例如可以调用自定义的一个存储过程或者sql语句来执行测试。

–create-schema
测试的schema,MySQL中schema也就是database

–commint=N
多少条DML后提交一次

–compress, -C
如果服务器和客户端支持都压缩,则压缩信息传递

–concurrency=N, -c N
并发量,也就是模拟多少个客户端同时执行select。可指定多个值,以逗号或者–delimiter参数指定的值做为分隔符

–engine=engine_name, -e engine_name
创建测试表所使用的存储引擎,可指定多个

–iterations=N, -i N
测试执行的迭代次数

–detach=N
执行N条语句后断开重连

–debug-info, -T
打印内存和CPU的信息

–only-print
只打印测试语句而不实际执行

测试的过程需要生成测试表,插入测试数据,这个mysqlslap可以自动生成,默认生成一个mysqlslap的schema,如果已经存在则先删除,这里要注意了,不要用–create-schema指定已经存在的库,否则后果可能很严重。可以用–only-print来打印实际的测试过程:

$mysqlslap -a --only-print
DROP SCHEMA IF EXISTS `mysqlslap`;
CREATE SCHEMA `mysqlslap`;
use mysqlslap;
CREATE TABLE `t1` (intcol1 INT(32) ,charcol1 VARCHAR(128));
INSERT INTO t1 VALUES (1804289383,'mxvtvmC9127qJNm06sGB8R92q2j7vTiiITRDGXM9ZLzkdekbWtmXKwZ2qG1llkRw5m9DHOFilEREk3q7oce8O3BEJC0woJsm6uzFAEynLH2xCsw1KQ1lT4zg9rdxBL');
...
SELECT intcol1,charcol1 FROM t1;
INSERT INTO t1 VALUES (364531492,'qMa5SuKo4M5OM7ldvisSc6WK9rsG9E8sSixocHdgfa5uiiNTGFxkDJ4EAwWC2e4NL1BpAgWiFRcp1zIH6F1BayPdmwphatwnmzdwgzWnQ6SRxmcvtd6JRYwEKdvuWr');
DROP SCHEMA IF EXISTS `mysqlslap`;

可以看到最后由删除一开始创建的schema的动作,整个测试完成后不会在数据库中留下痕迹。假如我们执行一次测试,分别50和100个并发,执行1000次总查询,那么:

$mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --debug-info
Benchmark
  Average number of seconds to run all queries: 0.375 seconds
  Minimum number of seconds to run all queries: 0.375 seconds
  Maximum number of seconds to run all queries: 0.375 seconds
  Number of clients running queries: 50
  Average number of queries per client: 20

Benchmark
  Average number of seconds to run all queries: 0.453 seconds
  Minimum number of seconds to run all queries: 0.453 seconds
  Maximum number of seconds to run all queries: 0.453 seconds
  Number of clients running queries: 100
  Average number of queries per client: 10

User time 0.29, System time 0.11
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 4032, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 7319, Involuntary context switches 681

上结果可以看出,50和100个并发分别得到一次测试结果(Benchmark),并发数越多,执行完所有查询的时间越长。为了准确起见,可以多迭代测试几次:

$ mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --debug-info
Benchmark
  Average number of seconds to run all queries: 0.380 seconds
  Minimum number of seconds to run all queries: 0.377 seconds
  Maximum number of seconds to run all queries: 0.385 seconds
  Number of clients running queries: 50
  Average number of queries per client: 20

Benchmark
  Average number of seconds to run all queries: 0.447 seconds
  Minimum number of seconds to run all queries: 0.444 seconds
  Maximum number of seconds to run all queries: 0.451 seconds
  Number of clients running queries: 100
  Average number of queries per client: 10

User time 1.44, System time 0.67
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 17922, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 36796, Involuntary context switches 4093

测试同时不同的存储引擎的性能进行对比:

$ mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodbmysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb --debug-info
Benchmark
  Running for engine myisam
  Average number of seconds to run all queries: 0.200 seconds
  Minimum number of seconds to run all queries: 0.188 seconds
  Maximum number of seconds to run all queries: 0.210 seconds
  Number of clients running queries: 50
  Average number of queries per client: 20

Benchmark
  Running for engine myisam
  Average number of seconds to run all queries: 0.238 seconds
  Minimum number of seconds to run all queries: 0.228 seconds
  Maximum number of seconds to run all queries: 0.251 seconds
  Number of clients running queries: 100
  Average number of queries per client: 10

Benchmark
  Running for engine innodb
  Average number of seconds to run all queries: 0.375 seconds
  Minimum number of seconds to run all queries: 0.370 seconds
  Maximum number of seconds to run all queries: 0.379 seconds
  Number of clients running queries: 50
  Average number of queries per client: 20

Benchmark
  Running for engine innodb
  Average number of seconds to run all queries: 0.443 seconds
  Minimum number of seconds to run all queries: 0.440 seconds
  Maximum number of seconds to run all queries: 0.447 seconds
  Number of clients running queries: 100
  Average number of queries per client: 10

User time 2.83, System time 1.66
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 34692, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 87306, Involuntary context switches 10326