日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

怎样提高WebService性能大数据量网络传输处理(转)

發(fā)布時(shí)間:2023/12/10 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 怎样提高WebService性能大数据量网络传输处理(转) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 直接返回DataSet對(duì)象

特點(diǎn):通常組件化的處理機(jī)制,不加任何修飾及

處理;

優(yōu)點(diǎn):代碼精減、易于處理,小數(shù)據(jù)量處理較快;

缺點(diǎn):大數(shù)據(jù)量的傳遞處理慢,消耗網(wǎng)絡(luò)資源;

建議:當(dāng)應(yīng)用系統(tǒng)在內(nèi)網(wǎng)、專網(wǎng)(局域網(wǎng))的應(yīng)用

時(shí),或外網(wǎng)(廣域網(wǎng))且數(shù)據(jù)量在KB級(jí)時(shí)的

應(yīng)用時(shí),采用此種模式。


2.返回DataSet對(duì)象用Binary序列化后的字節(jié)數(shù)組

特點(diǎn):字節(jié)數(shù)組流的處理模式;

優(yōu)點(diǎn):易于處理,可以中文內(nèi)容起到加密作用;

缺點(diǎn):大數(shù)據(jù)量的傳遞處理慢,較消耗網(wǎng)絡(luò)資源;

建議:當(dāng)系統(tǒng)需要進(jìn)行較大數(shù)據(jù)交換時(shí)采用。

3.返回DataSetSurrogate對(duì)象用Binary序列化后的字節(jié)數(shù)組

特點(diǎn):微軟提供的開源組件;

下載地址 http://support.microsoft.com/kb/829740/zh-cn

優(yōu)點(diǎn):易于處理,可以中文內(nèi)容起到加密作用;

缺點(diǎn):大數(shù)據(jù)量的傳遞處理慢,較消耗網(wǎng)絡(luò)資源;

建議:當(dāng)系統(tǒng)需要傳輸中文數(shù)據(jù)或需要加密時(shí)采用此種方式

4.返回DataSetSurrogate對(duì)象用Binary序列化并Zip壓縮后的字節(jié)數(shù)組

特點(diǎn):對(duì)字節(jié)流數(shù)組進(jìn)行壓縮后傳遞;

優(yōu)點(diǎn):當(dāng)數(shù)據(jù)量大時(shí),性能提高效果明顯,

壓縮比例大;

缺點(diǎn):相比第三方組件,壓縮比例還有待提高;

建議:當(dāng)系統(tǒng)需要進(jìn)行大數(shù)據(jù)量網(wǎng)絡(luò)數(shù)據(jù)傳遞時(shí),

建議采用此種可靠、高效、免費(fèi)的方法。

測試用例:SqlServer2000數(shù)據(jù)庫,數(shù)據(jù)量大小40000行,

字段數(shù)10個(gè),結(jié)果如下:

使用方法

用時(shí)(秒)

數(shù)據(jù)量(Byte)

大小

百分比(%)

直接返回DataSet

12.625

19629414

100%

返回二進(jìn)制序列化后DataSet

9.712

12049645

61.38%

返回轉(zhuǎn)化DataSetSurrogate的DataSet并且二進(jìn)制序列化后

7.943

5138990

26.18%

返回轉(zhuǎn)化DataSetSurrogate的DataSet并且二進(jìn)制序列化后使用zip壓縮

7.619

978033

4.98%

源碼:

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

using System.IO;
using System.IO.Compression;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters.Binary;


namespace DataSetWebService
{
/// <summary>
/// Service1 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class DataSetService : System.Web.Services.WebService
{

[WebMethod(Description="直接返回DataSet對(duì)象")]
public DataSet GetDataSet()
{
//http://www.dzbsoft.com XT_TEXT
string sql = "select * from XT_TEXT";
SqlConnection conn = new SqlConnection("Server=60.28.25.58;DataBase=s168593;user id=s168593;password=h0y+FeC*;");
conn.Open();
SqlDataAdapter dataAd = new SqlDataAdapter(sql, conn);
DataSet DS = new DataSet("XT_TEXT");
dataAd.Fill(DS);
conn.Close();
return DS;
}


[WebMethod(Description = "返回DataSet對(duì)象用Binary序列化后的字節(jié)數(shù)組")]
public byte[] GetDataSetBytes()
{
DataSet DS = GetDataSet();
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, DS);
byte[] buffer = ms.ToArray();
return buffer;
}

[WebMethod(Description = "返回DataSetSurrogate對(duì)象用Binary序列化后的字節(jié)數(shù)組")]
public byte[] GetDataSetSurrogateBytes()
{
DataSet DS = GetDataSet();
DataSetSurrogate dss = new DataSetSurrogate(DS);
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, dss);
byte[] buffer = ms.ToArray();
return buffer;
}

[WebMethod(Description = "返回DataSetSurrogate對(duì)象用Binary序列化并ZIP壓縮后的字節(jié)數(shù)組")]
public byte[] GetDataSetSurrogateZipBytes()
{
DataSet DS = GetDataSet();
DataSetSurrogate dss = new DataSetSurrogate(DS);
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, dss);
byte[] buffer = ms.ToArray();
byte[] Zipbuffer = Compress(buffer);
return Zipbuffer;
}

public byte[] Compress(byte[] data)
{
MemoryStream ms = new MemoryStream();
Stream zipStream = null;
zipStream = new GZipStream(ms, CompressionMode.Compress, true);
zipStream.Write(data, 0, data.Length);
zipStream.Close();
ms.Position = 0;
byte[] compressed_data = new byte[ms.Length];
ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
return compressed_data;
}
}
}

客戶端調(diào)用:C/S

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.IO.Compression;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void BindDataSet(DataSet DS)
{
this.dataGridView1.DataSource = DS.Tables[0];
}


private void button1_Click(object sender, EventArgs e)
{
com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
DateTime dtBegin = DateTime.Now;
DataSet DS = ds.GetDataSet();
this.label1.Text = string.Format("耗時(shí):{0}", DateTime.Now - dtBegin);
BindDataSet(DS);
}

private void button2_Click(object sender, EventArgs e)
{
com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
DateTime dtBegin = DateTime.Now;
byte[] buffer = ds.GetDataSetBytes();
DataSet DS = ds.GetDataSet();
BinaryFormatter ser = new BinaryFormatter();
DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet;
this.label2.Text = string.Format("耗時(shí):{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
BindDataSet(DS);
}

private void button3_Click(object sender, EventArgs e)
{
com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
DateTime dtBegin = DateTime.Now;
byte[] buffer = ds.GetDataSetSurrogateBytes();
BinaryFormatter ser = new BinaryFormatter();
DataSet DS = ds.GetDataSet();
DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
DataSet dataset = dss.ConvertToDataSet();
this.label3.Text = string.Format("耗時(shí):{0}", DateTime.Now - dtBegin + " " + buffer.Length.ToString());
BindDataSet(DS);
}

private void button4_Click(object sender, EventArgs e)
{
com.dzbsoft.www.DataSetService ds = new Test.com.dzbsoft.www.DataSetService();
DateTime dtBegin = DateTime.Now;
byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();
byte[] buffer = UnZipClass.Decompress(zipBuffer);
BinaryFormatter ser = new BinaryFormatter();
DataSet DS = ds.GetDataSet();
DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
DataSet dataset = dss.ConvertToDataSet();
this.label4.Text = string.Format("耗時(shí):{0}", DateTime.Now - dtBegin + " " + zipBuffer.Length.ToString());
BindDataSet(DS);
}

}
}

UnZipClass.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace Test
{
public static class UnZipClass
{
/// <summary>
/// Decompresses the specified data.
/// </summary>
/// <param name="data">The data.</param>
/// <returns></returns>
public static byte[] Decompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
Stream zipStream = null;
zipStream = new GZipStream(ms, CompressionMode.Decompress);
byte[] dc_data = null;
dc_data = EtractBytesFormStream(zipStream, data.Length);
return dc_data;
}
catch
{
return null;
}
}


public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
{
try
{
byte[] data = null;
int totalBytesRead = 0;
while (true)
{
Array.Resize(ref data, totalBytesRead + dataBlock + 1);
int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
if (bytesRead == 0)
{
break;
}
totalBytesRead += bytesRead;
}
Array.Resize(ref data, totalBytesRead);
return data;
}
catch
{
return null;
}
}
}
}

轉(zhuǎn)自: http://hbluojiahui.blog.163.com/blog/static/310647672009491142070/

總結(jié)

以上是生活随笔為你收集整理的怎样提高WebService性能大数据量网络传输处理(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。