怎样提高WebService的性能
服务器端WebService程序:
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- using System.IO.Compression;
- using System.Data.SqlClient;
- ………
- public class Service1 : System.Web.Services.WebService
- {
- [WebMethod(Description = "直接返回 DataSet 对象。")]
- public DataSet GetNorthwindDataSet()
- {
- 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 dataadapter = new SqlDataAdapter(sql, conn);
- DataSet ds = new DataSet();
- dataadapter.Fill(ds, "XT_TEXT");
- conn.Close();
- return ds;
- }
- [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 = Compress(buffer);
- return zipBuffer;
- }
- public byte[] Compress(byte[] data)
- {
- try
- {
- 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;
- }
- catch
- {
- return null;
- }
- }
- }
- 客户端WebService程序
- [code ="C#"]
- private void button1_Click(object sender, EventArgs e)
- {
- com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
- 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)
- {
- com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
- 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)
- {
- com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
- 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)
- {
- com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
- DateTime dtBegin = DateTime.Now;
- byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();
- byte[] buffer = UnZipClass.Decompress(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 + "条记录";
- }
- 客户端UnZipClass程序
- [code ="C#"]
- public static class UnZipClass
- {
- 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 = ExtractBytesFromStream(zipStream, data.Length);
- return dc_data;
- }
- catch
- {
- return null;
- }
- }
- public static byte[] ExtractBytesFromStream(Stream zipStream, int dataBlock)
- {
- byte[] data = null;
- int totalBytesRead = 0;
- try
- {
- 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;
- }
- }
- }
怎样提高WebService的性能的更多相关文章
- cxf怎样提高webservice性能,及访问速度调优
性能: 1. 启用FastInfoset(快速信息集)webservice的性能实在是不敢恭维.曾经因为webservice吞吐量上不去,对webservice进行了一些性能方面的优化,采用了Fast ...
- IIS webService 并发 性能
IIS webService 并发 性能 做的WebService,客户压力测试500并发(随机步长60s-90s),响应速度不理想. 1,优化程序,压缩执行时间2,提高IIS“最大工作进程数 ...
- 提高ASP.net性能的十种方法
提高ASP.net性能的十种方法 2014-10-24 空城66 摘自 博客园 阅 67 转 1 转藏到我的图书馆 微信分享: 今天无意中看了一篇关于提高ASP.NET性能的文章,个人 ...
- 25条提高iOS App性能的建议和技巧
这篇文章来自iOS Tutorial Team 成员 Marcelo Fabri, 他是 Movile 的一个iOS开发者. Check out his personal website or fol ...
- 提高 DHTML 页面性能
联盟电脑摘要:本文说明了某些DHTML功能对性能的重大影响,并提供了一些提高DHTML页面性能的技巧. 目录 简介 成批处理DHTML更改 使用innerText 使用DOM添加单个元素 扩展SELE ...
- 25条提高iOS app性能的方法和技巧
以下这些技巧分为三个不同那个的级别---基础,中级,高级. 基础 这些技巧你要总是想着实现在你开发的App中. 1. 用ARC去管理内存(Use ARC to Manage Memory) 2.适当的 ...
- 用 Function.apply() 的参数数组化来提高 JavaScript程序性能
我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...
- 如何提高jQuery的性能
缓存变量DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // ...
- 一个用于每一天JavaScript示例-使用缓存计算(memoization)为了提高应用程序性能
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
随机推荐
- ROCKETMQ——2主2从集群部署
1.压缩包准备两台服务器镜像操作cd /optmkdir softcd soft将两个压缩包复制到 soft目录unzip apache-maven-3.2.2-bin.zipunzip rocket ...
- mysql学习(2)-Navicat Premium 12 链接MySQL8.0.11数据库报2059错误
Navicat Premium 12 链接MySQL8.0.11数据库报2059错误 1,问题现象 安装完MySQL8.0.11和Navicat Premium12后,我们会用Navicat去测试连接 ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- linux内核分析--操作系统是如何工作的?
一个简单的时间片轮转多道程序 操作系统的"两把剑":中断上下文(保存现场和恢复现场)和进程上下文的切换 源代码的分析 *使用的源代码为视频中所使用的精简内核的源代码 首先分析myp ...
- 团队作业4——WBS练习
一.作业要求 对团队项目进行任务分解 要求所有人共同参与 队长列出需求 成员进行估计 队长领导大家达成共识 形成团队报告,发至团队博客 注意:分解的粒度最小不应超过6小时(即一个人在6小时以内能够完 ...
- 微信小程序input组件抖动及textarea组件光标错位解决方案
问题一: 使用微信小程序input组件时,在移动端唤起focus或blur事件时,因光标占位导致内容出现叠影及抖动现象. 解决方案: 用<textarea>组件代替了<input/& ...
- Linux命令(十二) 分割文件 split 合并文件 join
一.分割文件 split 命令介绍 当处理文件时,有时需要将文件做分割处理,split 命令用于分割文件,可以分割文本文件,按指定的行数分割,每个分割后的文件都包含相同的行数.split 可以分割非文 ...
- C++中关键字explicit的作用
C++中, 一个参数的构造函数(或者除了第一个参数外其余参数都有默认值的多参构造函数), 承担了两个角色. 1 是个构造器 ,2 是个默认且隐含的类型转换操作符. 所以, 有时候在我们写下如 AAA ...
- Redis的核心Hystrix在Spring mvc的使用
核心Hystrix,Hystrix对于接口调用具有很好的保护,能在多服务依赖的分布式系统中,有效的提供应用的可用性,并且对失败应用进行熔断和恢复检查,让应用在复杂的环境中也能各种稳. http://t ...
- Chrome Ajax 跨域设置
一.前言 web 开发中 Ajax 是十分常见的技术,但是在前后端使用接口对接的调试过程中不可避免会碰到跨域问题.今天我给大家介绍一个十分简单有效的方法. 跨域经典错误 二.Chrome 跨域设置 首 ...