怎样提高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 ...
随机推荐
- GitHub 新手教程 四,Git GUI 新手教程(1),OpenSSH Public Key
1,从开始菜单 启动 Git GUI,或者运行: D:\soft\Git\cmd\git-gui.exe(D:\soft\Git 为您的 GitHub 安装文件夹) 2,获取 SSH 密钥: 3,点击 ...
- 20135220谈愈敏Blog3_构造一个简单的Linux系统MenuOS
构造一个简单的Linux系统MenuOS 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1 ...
- Daily Scrum 10.20
今天进行了团队第一次scrum meeting,在这次会议中,我们针对NABC模型以及开发前期的工作进行了探讨. 第一次会议 主要内容如下: 为了大家接下来几周的开发效率,需要共同商量团队的一些规则 ...
- Beta阶段冲刺-5
一. 每日会议 1. 照片 2. 昨日完成工作 3. 今日完成工作 4. 工作中遇到的困难 杨晨露:现在我过的某种意义上挺滋润的,没啥事了都.......咳,困难就是前端每天都在想砸电脑,我要怎么阻止 ...
- 使用redis防止抢购商品超卖
前言: redis不仅仅是单纯的缓存,它还有一些特殊的功能,在一些特殊场景上很好用. 本篇博文用来测试下使用redis来防止抢购商品超卖问题. 内容: 使用redis的list进行测试 思路是设置一个 ...
- 【luogu3768】简单的数学题 欧拉函数(欧拉反演)+杜教筛
题目描述 给出 $n$ 和 $p$ ,求 $(\sum\limits_{i=1}^n\sum\limits_{j=1}^nij\gcd(i,j))\mod p$ . $n\le 10^{10}$ . ...
- Maven项目打包,Jar包不更新的问题
问题: 我的maven项目A要打成Jar包A,依赖了另外一个项目B生成的Jar包B.更改了项目B的代码,然后继续打包项目A,生成的Jar包A中并没有我修改了的代码. 原因: Jar包B在开始时被Ins ...
- oracle 创建表空间 与创建用户与分配用户权限
创建一个表空间名为ABC create tablespace "ABC" //貌似要大写 datafile 'D:\oracle\TBSPACES\ABC.dbf' / ...
- 【刷题】HDU 5869 Different GCD Subarray Query
Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...
- 【题解】 [ZJOI2008] 泡泡堂(贪心/二分图/动态规划)
懒得复制,戳我戳我 Solution: 就是有一个贪心策略:(以下假设使\(A\)队分数更高) \(First:\)比较两个分值的最小值,如果\(A\)最小分比\(B\)最小分大就直接比较两个最小的, ...