在使用WebService进行远程数据操作时,细心的你会发现WebServices的性能特别的慢,当然也曾听见很多网友也如此如何如何。说实话,WebServices的确比调用本地数据要慢一些,可究竟有多慢?真的如网友们说的那么难以忍受吗?我个人感觉,多半原因在处理的方式上。让我们亲自编写测试代码,来证明这一切吧。文章由我参考一网友的写法来测试的,因此难免会参杂个人主观因素,如你有新的想法或建议,还请多多提出。以下我们主要从调用WebServices的方法的特点、应用场景、测试结果三个方面来进行下说明分析。
转载自:https://blog.csdn.net/chenxiaoqiang/article/details/50260977/
1、直接返回DataSet对象
特点:直接返回DataSet对象。
应用场景:a、内网;b、外网且数据量在kb级别时;c、远程sql2000数据库
2、返回DataSet对象用Binary序列化后的字节数组
特点:字节数组流的处理模式。
应用场景:较大数据交换。
3、返回DataSetSurrogate对象用Binary 序列化后的字节数组
特点:使用微软提供的开源组件进行序列化,依然是字节流的处理模式。详情请参考:http://support.microsoft.com/kb/829740/zh-cn
应用场景: 较大数据交换。
4、返回DataSetSurrogate对象用Binary 序列化并Zip压缩后的字节数组
特点:使用微软提供的开源组件对字节流数组进行压缩后传递,依然是字节流的处理模式。详情请参考:http://support.microsoft.com/kb/829740/zh-cn
应用场景:外网环境需要进行大数据量网络数据传递时,建议采用此种方法。也是笔者强烈向大家推荐使用的一种方法。
WebService源码如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Services;
-
using System.Data;
-
using System.Runtime.Serialization.Formatters.Binary;
-
using System.IO;
-
using System.IO.Compression;
-
using System.Text;
-
using DAL;
-
/// <summary>
-
///WebServiceTest 的摘要说明
-
/// </summary>
-
[WebService(Namespace = "http://tempuri.org/")]
-
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
-
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
-
// [System.Web.Script.Services.ScriptService]
-
public class WebServiceTest : System.Web.Services.WebService {
-
public WebServiceTest () {
-
//如果使用设计的组件,请取消注释以下行
-
//InitializeComponent();
-
}
-
[WebMethod]
-
public string HelloWorld() {
-
return "Hello World";
-
}
-
[WebMethod(Description="直接返回DataSet对象")]
-
public DataSet GetDataSet()
-
{
-
StringBuilder builder = new StringBuilder();
-
builder.Append("select * from ServiceInfo");
-
DataSet ds = SQLHelper.Query(builder.ToString());
-
return ds;
-
}
-
[WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
-
public byte[] GetBytes()
-
{
-
DataSet ds = GetDataSet();
-
BinaryFormatter bf = new BinaryFormatter();
-
MemoryStream ms = new MemoryStream();
-
bf.Serialize(ms, ds);
-
byte[] buffer = ms.ToArray();
-
return buffer;
-
}
-
[WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化后的字节数组")]
-
public byte[] GetDataSetSurrogateBytes()
-
{
-
DataSet ds = GetDataSet();
-
DataSetSurrogate dss = new DataSetSurrogate(ds);
-
BinaryFormatter bf = new BinaryFormatter();
-
MemoryStream ms = new MemoryStream();
-
bf.Serialize(ms, dss);
-
byte[] buffer = ms.ToArray();
-
return buffer;
-
}
-
[WebMethod(Description = "返回DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组")]
-
public byte[] GetDataSetSurrogateZipBytes()
-
{
-
DataSet DS = GetDataSet();
-
DataSetSurrogate dss = new DataSetSurrogate(DS);
-
BinaryFormatter bf = new BinaryFormatter();
-
MemoryStream ms = new MemoryStream();
-
bf.Serialize(ms, dss);
-
byte[] buffer = ms.ToArray();
-
byte[] Zipbuffer = Compress(buffer);
-
return Zipbuffer;
-
}
-
//压缩压缩后的字节数组
-
public byte[] Compress(byte[] data)
-
{
-
MemoryStream ms = new MemoryStream();
-
Stream zipStream = new GZipStream(ms, CompressionMode.Compress, true);
-
zipStream.Write(data, 0, data.Length);
-
zipStream.Close();
-
ms.Position = 0;
-
byte[] buffer = new byte[ms.Length];
-
ms.Read(buffer, 0, int.Parse(ms.Length.ToString()));
-
return buffer;
-
}
-
}
客户端调用WebService前台源码如下:
-
<%@ Page Language="C#" CodeFile="WebTest.aspx.cs" Inherits="WebTest" %>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title></title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
-
Text="直接得到DataSet对象" />
-
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
-
Text="得到DataSet对象用Binary序列化后的字节数组" />
-
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
-
style="height: 26px" Text="得到DataSetSurrogate对象用Binary序列化后的字节数组" />
-
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
-
Text="得到DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组" />
-
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
-
<br />
-
<asp:GridView ID="GridView1" runat="server" Width="100%">
-
</asp:GridView>
-
</div>
-
</form>
-
</body>
-
</html>
客户端调用WebService后台源码如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Data;
-
using System.Diagnostics;
-
using System.Runtime.Serialization.Formatters.Binary;
-
using WebServicesClient;
-
using System.IO;
-
public partial class WebTest : System.Web.UI.Page
-
{
-
WebServiceTest s = new WebServiceTest();
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
}
-
protected void Button1_Click(object sender, EventArgs e)
-
{
-
Stopwatch sw = new Stopwatch();
-
sw.Start();
-
DataSet ds = s.GetDataSet();
-
GridView1.DataSource = ds.Tables[0].DefaultView;
-
GridView1.DataBind();
-
sw.Stop();
-
Label1.Text = string.Format("耗时:{0}毫秒", sw.ElapsedMilliseconds.ToString());
-
}
-
protected void Button2_Click(object sender, EventArgs e)
-
{
-
Stopwatch sw = new Stopwatch();
-
sw.Start();
-
byte[] buffer = s.GetBytes();
-
BinaryFormatter bf = new BinaryFormatter();
-
DataSet ds = bf.Deserialize(new MemoryStream(buffer)) as DataSet;
-
GridView1.DataSource = ds.Tables[0].DefaultView;
-
GridView1.DataBind();
-
sw.Stop();
-
Label2.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
-
}
-
protected void Button3_Click(object sender, EventArgs e)
-
{
-
Stopwatch sw = new Stopwatch();
-
sw.Start();
-
byte[] buffer = s.GetDataSetSurrogateBytes();
-
BinaryFormatter bf = new BinaryFormatter();
-
DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
-
DataSet ds = dss.ConvertToDataSet();
-
GridView1.DataSource = ds.Tables[0].DefaultView;
-
GridView1.DataBind();
-
sw.Stop();
-
Label3.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
-
}
-
protected void Button4_Click(object sender, EventArgs e)
-
{
-
Stopwatch sw = new Stopwatch();
-
sw.Start();
-
byte[] zipBuffer = s.GetDataSetSurrogateZipBytes();
-
byte[] buffer = UnZip.Decompress(zipBuffer);
-
BinaryFormatter bf = new BinaryFormatter();
-
DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
-
DataSet ds = dss.ConvertToDataSet();
-
GridView1.DataSource = ds.Tables[0].DefaultView;
-
GridView1.DataBind();
-
sw.Stop();
-
Label4.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", zipBuffer.Length.ToString(), sw.ElapsedMilliseconds.ToString());
-
}
-
}
测试的结果按照先后顺序如下图所示:
通过WebService返回数据的四种方法比较
关于测试结果的说明,由于测试环境是数据库在局域网的另一台机器上,数据量也不是很大,测试的结果离实际情况还不是很接近,如果大家有条件的话,可以测试一下,同时希望把测试的结果提供给大家参考。
但是在测试过程中你会发现,第一次点击每个按钮测试和第二次点击按钮或者更多次点击按钮测试的结果是有所变化的,相关细节还需要你自己在测试中体会。
关于源代码的说明:开发环境为VS2010中文版,数据库为局域网内SQL2000数据库。