webservices dataset压缩之研究及其结果(转)

看了几篇大神的文章,选择了以下组件实现:
wse3.0
DataSetSurrogate
Managed C++ wrapper for ZLib(不是zlib.dll,Managed C++就是托管环境c++编译的.net平台dll)


测试数据量40570条记录 9个字段
运行结果:
1   直接返回dataset                                                用时10.625
2   返回二进制序列化后dataset                                        用时9.609      12049645 byte
3   返回转化DataSetSurrogate的dataset 并且二进制序列化后              用时7.859      5138990 byte(2的42.6%)
4   返回转化DataSetSurrogate的dataset 并且二进制序列化后使用zip压缩    用时7.625       578033 byte (2的4.79%)

服务端部分代码:

C# code
[WebMethod(Description = "直接返回 DataSet 对象。")] public DataSet GetNorthwindDataSet() { DataSet dataSet = this.QueryDataSet("select * from tbl_log"); return dataSet; } [WebMethod(Description = "返回 DataSet 对象用 Binary 序列化后的字节数组。")] public byte[] GetDataSetBytes() { DataSet dataSet = GetNorthwindDataSet(); BinaryFormatter ser = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); ser.Serialize(ms, dataSet); byte[] buffer = ms.ToArray(); return buffer; } [WebMethod(Description = "返回 DataSetSurrogate 对象用 Binary 序列化后的字节数组。")] public byte[] GetDataSetSurrogateBytes() { DataSet dataSet = GetNorthwindDataSet(); DataSetSurrogate dss = new DataSetSurrogate(dataSet); BinaryFormatter ser = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); ser.Serialize(ms, dss); byte[] buffer = ms.ToArray(); return buffer; } [WebMethod(Description = "返回 DataSetSurrogate 对象用 Binary 序列化并 Zip 压缩后的字节数组。")] public byte[] GetDataSetSurrogateZipBytes() { DataSet dataSet = GetNorthwindDataSet(); DataSetSurrogate dss = new DataSetSurrogate(dataSet); BinaryFormatter ser = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); ser.Serialize(ms, dss); byte[] buffer = ms.ToArray(); byte[] zipBuffer = ZCompressor.Compress(buffer, CompressionLevel.Normal); return zipBuffer; }

客户端部分代码
C# code
private void button1_Click(object sender, EventArgs e) { WSdata.DataService ds = new WSdata.DataService(); DateTime dtBegin = DateTime.Now; DataSet dataSet = ds.GetNorthwindDataSet(); this.label1.Text= string.Format("耗时:{0}", DateTime.Now - dtBegin); binddata(dataSet); } private void button2_Click(object sender, EventArgs e) { WSdata.DataService ds = new WSdata.DataService(); DateTime dtBegin = DateTime.Now; byte[] buffer = ds.GetDataSetBytes(); BinaryFormatter ser = new BinaryFormatter(); DataSet dataSet = ser.Deserialize(new MemoryStream(buffer)) as DataSet; this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + " " + buffer.Length; binddata(dataSet); } private void button3_Click(object sender, EventArgs e) { WSdata.DataService ds = new WSdata.DataService(); DateTime dtBegin = DateTime.Now; byte[] buffer = ds.GetDataSetSurrogateBytes(); BinaryFormatter ser = new BinaryFormatter(); DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet dataSet = dss.ConvertToDataSet(); this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + " " + buffer.Length; binddata(dataSet); } private void button4_Click(object sender, EventArgs e) { WSdata.DataService ds = new WSdata.DataService(); DateTime dtBegin = DateTime.Now; byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes(); byte[] buffer = ZCompressor.Uncompress(zipBuffer); BinaryFormatter ser = new BinaryFormatter(); DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet dataSet = dss.ConvertToDataSet(); this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + " " + zipBuffer.Length; binddata(dataSet); } private void binddata(DataSet dataSet) { this.dataGridView1.DataSource = dataSet.Tables[0]; this.label5.Text = "共计:"+ dataSet.Tables[0].Rows.Count+"条记录"; }

posted @ 2009-02-25 13:15  丁守庆  阅读(359)  评论(0编辑  收藏  举报
专业成就价值