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

1. 直接返回DataSet对象

特点:通常组件化的处理机制,不加任何修饰及

处理;

优点:代码精减、易于处理,小数据量处理较快;

缺点:大数据量的传递处理慢,消耗网络资源;

建议:当应用系统在内网、专网(局域网)的应用

时,或外网(广域网)且数据量在KB级时的

应用时,采用此种模式。


2.返回DataSet对象用Binary序列化后的字节数组

特点:字节数组流的处理模式;

优点:易于处理,可以中文内容起到加密作用;

缺点:大数据量的传递处理慢,较消耗网络资源;

建议:当系统需要进行较大数据交换时采用。

3.返回DataSetSurrogate对象用Binary序列化后的字节数组

特点:微软提供的开源组件;

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

优点:易于处理,可以中文内容起到加密作用;

缺点:大数据量的传递处理慢,较消耗网络资源;

建议:当系统需要传输中文数据或需要加密时采用此种方式

4.返回DataSetSurrogate对象用Binary序列化并Zip压缩后的字节数组

特点:对字节流数组进行压缩后传递;

优点:当数据量大时,性能提高效果明显,

压缩比例大;

缺点:相比第三方组件,压缩比例还有待提高;

建议:当系统需要进行大数据量网络数据传递时,

建议采用此种可靠、高效、免费的方法。

测试用例:SqlServer2000数据库,数据量大小40000行,

字段数10个,结果如下:

使用方法

用时(秒)

数据量(Byte)

大小

百分比(%)

直接返回DataSet

12.625

19629414

100%

返回二进制序列化后DataSet

9.712

12049645

61.38%

返回转化DataSetSurrogate的DataSet并且二进制序列化后

7.943

5138990

26.18%

返回转化DataSetSurrogate的DataSet并且二进制序列化后使用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对象")]
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对象用Binary序列化后的字节数组")]
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对象用Binary序列化后的字节数组")]
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对象用Binary序列化并ZIP压缩后的字节数组")]
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;
}
}
}

客户端调用: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("耗时:{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("耗时:{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("耗时:{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("耗时:{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;
}
}
}
}

转自: http://hbluojiahui.blog.163.com/blog/static/310647672009491142070/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/444039.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【中国互联网江湖30年历史】再无风清扬,再有少年郎

0 马云退了。 在萧山奥体中心&#xff0c;无数阿里人的祝福中&#xff0c;流着眼泪&#xff0c;结束了自己在阿里的最后一天。 从此互联网江湖再无风清扬&#xff0c;反而多了一个叫做马云的乡村教师。 他临别一挥手&#xff0c;似乎带走了中国互联网的一个时代。 20年浮沉&…

互联网30年,泡沫如梦

人人都说互联网改变世界&#xff0c;这话没错。 但我认为互联网改变的方式&#xff0c;是泡沫。 资金&#xff0c;资源&#xff0c;人才因为一堆概念聚在一起&#xff0c;形成一个又一个的泡沫&#xff0c;然后泡沫破裂&#xff0c;大部分人失败&#xff0c;少数能够留下来的&a…

cpp知识汇总(1) 指针vs引用、static、const

引用和指针的区别&#xff1f; 指针是一个实体&#xff0c;需要分配内存空间。引用只是变量的别名&#xff0c;不需要分配内存空间。引用在定义的时候必须进行初始化&#xff0c;并且不能够改变。指针在定义的时候不一定要初始化&#xff0c;并且指向的空间可变。&#xff08;…

【精品计划 附录2】- 算法分析

数学模型 1. 近似2. 增长数量级3. 内循环4. 成本模型 注意事项 1. 大常数2. 缓存3. 对最坏情况下的性能的保证4. 随机化算法5. 均摊分析 ThreeSum 1. ThreeSumSlow2. ThreeSumBinarySearch3. ThreeSumTwoPointer 倍率实验 数学模型 1. 近似 N3/6-N2/2N/3 ~ N3/6。使用 ~f(N) …

俄罗斯方块(C++)

#include<iostream> #include<stdlib.h> #include<windows.h> #include<time.h> #include<conio.h> using namespace std;#define A1 0//A代表长条型&#xff0c;B为方块&#xff0c;C为L型&#xff0c;D为闪电型&#xff08;实在无法描述那个形…

leetcode445. 两数相加 II

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。 你可以假设除了数字 0 之外&#xff0c;这两个数字都不会以零开头。 进阶&#xff1a; 如果输入链表不能修改该如何处理&#xff1f;换…

Java中如何实现每天定时对数据库的操作

现在有一个很棘手的问题&#xff1a;客户要赠加一个功能&#xff0c;就是每天晚上11点要统计一下数据&#xff0c;并存到一个文件中&#xff0c;我试着用线程&#xff0c;但是总达不到理想的效果。请给点思路&#xff0c;多谢了。 我们的开发环境是tomcat和servlet&#xff0c;…

leetcode面试题 02.08. 环路检测

给定一个有环链表&#xff0c;实现一个算法返回环路的开头节点。 有环链表的定义&#xff1a;在链表中某个节点的next元素指向在它前面出现过的节点&#xff0c;则表明该链表存在环路。 示例 1&#xff1a; 输入&#xff1a;head [3,2,0,-4], pos 1 输出&#xff1a;tail co…

leetcode485. 最大连续1的个数 *py:“又是一行就解决了,没意思”

给定一个二进制数组&#xff0c; 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1&#xff0c;所以最大连续1的个数是 3. 注意&#xff1a; 输入的数组只包含 0 和1。 输入数组的长度是正整数&#xff0c;且不超过 1…

leetcode645. 错误的集合

645. 错误的集合 难度简单98 集合 S 包含从1到 n 的整数。不幸的是&#xff0c;因为数据错误&#xff0c;导致集合里面某一个元素复制了成了集合里面的另外一个元素的值&#xff0c;导致集合丢失了一个整数并且有一个元素重复。 给定一个数组 nums 代表了集合 S 发生错误后的…

一篇文章揭穿创业公司的套路

初衷 每个初入社会的求职者&#xff0c;都曾经有过找工作被坑的经历。总结了以下潜台词&#xff0c;如果你能全部GET到&#xff0c;那么恭喜你&#xff0c;已被老板们拉入黑名单。 核心 「工资4k - 8k」——那工资就是4k 「工资上不封顶」——就是说说而已&#xff0c;没人会给…

java获取IP地址:

public class IPDemo {public static void main(String [] args) throws UnknownHostException{//获取本地主机InetAddress localHost InetAddress.getLocalHost();System.out.println(localHost);//Lenovo-sxg/192.168.1.106//获取本地主机的IP地址String ipaddress localHo…

《三天给你聊清楚redis》第1天先唠唠redis是个啥(18629字)

后端需要知道的关于redis的事&#xff0c;基本都在这里了。 此文后续会改为粉丝可见&#xff0c;所以喜欢的请提前关注。 你的点赞和评论是我创作的最大动力&#xff0c;谢谢。 1、入门 Redis是一款基于键值对的NoSQL数据库&#xff0c;它的值支持多种数据结构&#xff1a;…

获取IP地址:

public class IPDemo {public static void main(String [] args) throws UnknownHostException{//获取本地主机InetAddress localHost InetAddress.getLocalHost();System.out.println(localHost);//Lenovo-sxg/192.168.1.106//获取本地主机的IP地址String ipaddress localHo…

使用github+jsdelivr作为视频床

感谢JefferyIF大佬提供的神奇方法。 1. 配置FFmpeg 注&#xff1a;IOS因为不支持HLS&#xff0c;所以对IOS上无法正常播放视频&#xff0c;其他端都可以正常播放。 因为脚本要使用到FFmeg对源视频文件切分成m3u8格式&#xff0c;所以在使用脚本之前&#xff0c;请配置好 FFm…

使用PicGo+github+jsdelivr作为图床

1.什么是图床&#xff1f; 所谓图床工具&#xff0c;就是自动把本地图片转换成链接的一款工具&#xff0c;网络上有很多图床工具&#xff0c;就目前使用种类而言&#xff0c;PicGo 算得上一款比较优秀的图床工具。它是一款用 Electron-vue 开发的软件&#xff0c;可以支持微博…

万字干货:教新手从0到1搭建完整的增长数据体系

在实际的业务中&#xff0c;大多数人可能只会遇到以下一种或几种常见的场景&#xff0c;并且对于各个细分场景&#xff0c;所需要解决的问题和关注重点都是不一样的。 场景一&#xff1a;你刚加入一个成熟产品的用户增长部门&#xff0c;会发现业务当前有非常全面详实的用户和业…

《三天给你聊清楚redis》第2天看看redis怎么被搞出来的(22036字)

后端需要知道的关于redis的事&#xff0c;基本都在这里了。 此文后续会改为粉丝可见&#xff0c;所以喜欢的请提前关注。 你的点赞和评论是我创作的最大动力&#xff0c;谢谢。 3、单机实现 3.1、数据库概述 redis服务器将所有数据库都保存在redis/redisServer中&#xff…

JAVA中对象的序列化的作用?

1、序列化是干什么的&#xff1f; 简单说就是为了保存在内存中的各种对象的状态&#xff0c;并且可以把保存的对象状态再读出来。虽然你可以用你自己的各种各样的方法来保存Object States&#xff0c;但是Java给你提供一种应该比你自己好的保存对象状态的机制,那就是序列化。 2…

leetcode559. N叉树的最大深度

给定一个 N 叉树&#xff0c;找到其最大深度。 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。 例如&#xff0c;给定一个 3叉树 : 我们应返回其最大深度&#xff0c;3。 说明: 树的深度不会超过 1000。 树的节点总不会超过 5000。 思路见代码 /* // De…