php调用.Net WebService消息序列化及性能测试

作者:敖士伟

索  引
一、环境
二、代码
三、测试结果
四、结果分析


说明:php调用.net WebService,解决返回消息格式问题,比如数据库查询结果。本方案将.net DataTable序列化为JSON返回给php,php使用json_decode()解码JSON数据为数组。


一、环境

1. dotNet环境

Win2003EE sp2,IIS 6.0,.NET 3.5 sp1

2.PHP环境

CentOS 6.2, Apache 2.2.21, PHP 5.3.8, APC 3.1.9,mysql 5.5.16
PHP和mysql在同一server上

3.测试工具

Apache ab.exe


二、代码

注:mysql连接都使用持久连接

1.dotNet代码

        [WebMethod]
        public string dsMysql()
        {
            DataSet dataset = new DataSet();
            MySqlConnection conn = new MySqlConnection("Database=test;User Id=root;Password=ikmbikmb;Data Source=192.168.1.121;Port=3306;Pooling=true;Max Pool Size=10;Min Pool Size=1");
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("SELECT * FROM t1", conn);
            adapter.Fill(dataset);
            JSON_Class Object_JSON_Class = new JSON_Class();
            string tb = Object_JSON_Class.CreateJsonParameters(dataset.Tables[0]);
            return tb;
        }

2.php代码

(1)直接查询mysql代码
<?php
$link = mysql_pconnect("192.168.1.121", "root", "ikmbikmb")
or die("Could not connect : " . mysql_error());
mysql_select_db("test") or die("Could not select database");
$query = "SELECT * FROM t1";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
print "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	print "\t<tr>\n";
	foreach ($line as $col_value) {
		print "\t\t<td>$col_value</td>\n";
	}
	print "\t</tr>\n";
}
print "</table>\n";
mysql_free_result($result);
mysql_close($link);
?>

(2)调用WebService代码
<?php
$objSoapClient = new SoapClient("http://192.168.1.119/Service1.asmx?WSDL");
$out=$objSoapClient->dsMysql();
$json=$out->dsMysqlResult;
$t=json_decode($json, true);
$t=$t['Head']; 
print "<table>\n";
foreach ($t as $line) {
	print "\t<tr>\n";
	foreach ($line as $col_value) {
		print "\t\t<td>$col_value</td>\n";
	}
	print "\t</tr>\n";
}
?>

三、测试结果

说明:只列出结果关键参数

1.第一组测试10个并发

(1)直接查询
Requests per second:    334.20 [#/sec] (mean)
Time per request:       29.922 [ms] (mean)
Time per request:       2.992 [ms] (mean, across all concurrent requests)
Transfer rate:          133.87 [Kbytes/sec] received

(2)调用WebService
Requests per second:    184.51 [#/sec] (mean)
Time per request:       54.197 [ms] (mean)
Time per request:       5.420 [ms] (mean, across all concurrent requests)
Transfer rate:          72.39 [Kbytes/sec] received

2.第二组测试50个并发

(1)直接查询
Requests per second:    318.36 [#/sec] (mean)
Time per request:       157.053 [ms] (mean)
Time per request:       3.141 [ms] (mean, across all concurrent requests)
Transfer rate:          127.99 [Kbytes/sec] received

(2)调用WebService
Requests per second:    182.86 [#/sec] (mean)
Time per request:       273.427 [ms] (mean)
Time per request:       5.469 [ms] (mean, across all concurrent requests)
Transfer rate:          71.63 [Kbytes/sec] received

四、结果分析

直接查询比调用WebService大概快77%
(((334.20+318.36)/2)/((184.51+182.86)/2)-1)*100%
可能的原因:
1. 调用WebService增加了网络连接成本
2. 调用WebService在.net端和php都增加了对象的序列化计算成本

你可能感兴趣的:(mysql,.net,PHP,json,webservice,dataset)