WebServices返回數據的4種方法比較

之前常常在羣裏聽到朋友們說WebServices的性能特別的慢,說的如何如何。說實話,WebServices的確比調用本地數據要慢一些,但是究竟有多慢,真的如朋友們說的那麼難以忍受嗎?我我的感受,多半緣由在處理的方式上。讓咱們親自編寫測試代碼,來證實這一切吧。文章因爲是我一段時間的總結篇,所以不免參雜我的主觀因素,說的不對的地方,還請多多批評。如下咱們主要從調用WebServices的方法的特色、應用場景、測試結果三個方面來進行下說明分析。
1. 直接返回DataSet對象
特色:
直接返回DataSet對象。 應用場景:
1.內網。2.外網且數據量在kb級別時。 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應用場景:
外網環境須要進行大數據量網絡數據傳遞時,建議採用此種方法。也是筆者強烈向你們推薦使用的一種方法。 WebServices的代碼以下:
客戶端調用WebServices的代碼以下:

測試的結果按照前後順序以下圖所示:


關於測試結果的特殊說明,因爲測試環境是在本地,數據量也不是很大,測試的結果離實際狀況還不是很接近,若是你們有條件的話,能夠測試一下,同時但願把測試的結果提供給你們參考。
最後,爲了方便你們,這裏還提供了×××,
下載地址以下:WebServiceSummary.rar (點擊下載)
關於源代碼的特殊說明:筆者這裏的開發環境爲VS2008中文版sp1+SQLServer2008sp1。數據庫爲Northwind數據庫。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebServicesClient.localhost;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Diagnostics;
namespace WebServicesClient
{
    public partial class _Default : System.Web.UI.Page
    {
        Service1 s = new Service1();
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        //直接返回DataSet對象
        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());
        }
        //獲得DataSet對象用Binary序列化後的字節數組
        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());
        }
        //獲得DataSetSurrogate對象用Binary序列化後的字節數組
        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());
        }
        //獲得DataSetSurrogate對象用Binary序列化並ZIP壓縮後的字節數組
        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());
        }
    }
}

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
namespace WebService1
{
    /// <summary>
    /// Service1 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod(Description="直接返回DataSet對象")]
        public DataSet GetDataSet()
        {
            string sql = "select * from Customers";
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = db.ExecuteDataSet(CommandType.Text,sql);
            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;
        }
    }
}web

相關文章
相關標籤/搜索