服务器端WebService程序:

    1. using System.Runtime.Serialization.Formatters.Binary;
    2. using System.IO;
    3. using System.IO.Compression;
    4. using System.Data.SqlClient;
    5. ………
    6. public class Service1 : System.Web.Services.WebService
    7. {
    8. [WebMethod(Description = "直接返回 DataSet 对象。")]
    9. public DataSet GetNorthwindDataSet()
    10. {
    11. string sql = "SELECT * FROM XT_TEXT";
    12. SqlConnection conn = new SqlConnection("Server=60.28.25.58;DataBase=s168593;user id=s168593;password=h0y+FeC*;");
    13. conn.Open();
    14. SqlDataAdapter dataadapter = new SqlDataAdapter(sql, conn);
    15. DataSet ds = new DataSet();
    16. dataadapter.Fill(ds, "XT_TEXT");
    17. conn.Close();
    18. return ds;
    19. }
    20. [WebMethod(Description = "返回 DataSet 对象用 Binary 序列化后的字节数组。")]
    21. public byte[] GetDataSetBytes()
    22. {
    23. DataSet dataSet = GetNorthwindDataSet();
    24. BinaryFormatter ser = new BinaryFormatter();
    25. MemoryStream ms = new MemoryStream();
    26. ser.Serialize(ms, dataSet);
    27. byte[] buffer = ms.ToArray();
    28. return buffer;
    29. }
    30. [WebMethod(Description = "返回 DataSetSurrogate 对象用 Binary 序列化后的字节数组。")]
    31. public byte[] GetDataSetSurrogateBytes()
    32. {
    33. DataSet dataSet = GetNorthwindDataSet();
    34. DataSetSurrogate dss = new DataSetSurrogate(dataSet);
    35. BinaryFormatter ser = new BinaryFormatter();
    36. MemoryStream ms = new MemoryStream();
    37. ser.Serialize(ms, dss);
    38. byte[] buffer = ms.ToArray();
    39. return buffer;
    40. }
    41. [WebMethod(Description = "返回 DataSetSurrogate 对象用 Binary 序列化并 Zip 压缩后的字节数组。")]
    42. public byte[] GetDataSetSurrogateZipBytes()
    43. {
    44. DataSet dataSet = GetNorthwindDataSet();
    45. DataSetSurrogate dss = new DataSetSurrogate(dataSet);
    46. BinaryFormatter ser = new BinaryFormatter();
    47. MemoryStream ms = new MemoryStream();
    48. ser.Serialize(ms, dss);
    49. byte[] buffer = ms.ToArray();
    50. byte[] zipBuffer = Compress(buffer);
    51. return zipBuffer;
    52. }
    53. public byte[] Compress(byte[] data)
    54. {
    55. try
    56. {
    57. MemoryStream ms = new MemoryStream();
    58. Stream zipStream = null;
    59. zipStream = new GZipStream(ms, CompressionMode.Compress, true);
    60. zipStream.Write(data, 0, data.Length);
    61. zipStream.Close();
    62. ms.Position = 0;
    63. byte[] compressed_data = new byte[ms.Length];
    64. ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
    65. return compressed_data;
    66. }
    67. catch
    68. {
    69. return null;
    70. }
    71. }
    72. }
    73. 客户端WebService程序
    74. [code ="C#"]
    75. private void button1_Click(object sender, EventArgs e)
    76. {
    77. com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
    78. DateTime dtBegin = DateTime.Now;
    79. DataSet dataSet = ds.GetNorthwindDataSet();
    80. this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
    81. binddata(dataSet);
    82. }
    83. private void button2_Click(object sender, EventArgs e)
    84. {
    85. com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
    86. DateTime dtBegin = DateTime.Now;
    87. byte[] buffer = ds.GetDataSetBytes();
    88. BinaryFormatter ser = new BinaryFormatter();
    89. DataSet dataSet = ser.Deserialize(new MemoryStream(buffer)) as DataSet;
    90. this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + "  " + buffer.Length;
    91. binddata(dataSet);
    92. }
    93. private void button3_Click(object sender, EventArgs e)
    94. {
    95. com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
    96. DateTime dtBegin = DateTime.Now;
    97. byte[] buffer = ds.GetDataSetSurrogateBytes();
    98. BinaryFormatter ser = new BinaryFormatter();
    99. DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
    100. DataSet dataSet = dss.ConvertToDataSet();
    101. this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + "  " + buffer.Length;
    102. binddata(dataSet);
    103. }
    104. private void button4_Click(object sender, EventArgs e)
    105. {
    106. com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1();
    107. DateTime dtBegin = DateTime.Now;
    108. byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();
    109. byte[] buffer = UnZipClass.Decompress(zipBuffer);
    110. BinaryFormatter ser = new BinaryFormatter();
    111. DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
    112. DataSet dataSet = dss.ConvertToDataSet();
    113. this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + "  " + zipBuffer.Length;
    114. binddata(dataSet);
    115. }
    116. private void binddata(DataSet dataSet)
    117. {
    118. this.dataGridView1.DataSource = dataSet.Tables[0];
    119. this.label5.Text = "共计:" + dataSet.Tables[0].Rows.Count + "条记录";
    120. }
    121. 客户端UnZipClass程序
    122. [code ="C#"]
    123. public static class UnZipClass
    124. {
    125. public static byte[] Decompress(byte[] data)
    126. {
    127. try
    128. {
    129. MemoryStream ms = new MemoryStream(data);
    130. Stream zipStream = null;
    131. zipStream = new GZipStream(ms, CompressionMode.Decompress);
    132. byte[] dc_data = null;
    133. dc_data = ExtractBytesFromStream(zipStream, data.Length);
    134. return dc_data;
    135. }
    136. catch
    137. {
    138. return null;
    139. }
    140. }
    141. public static byte[] ExtractBytesFromStream(Stream zipStream, int dataBlock)
    142. {
    143. byte[] data = null;
    144. int totalBytesRead = 0;
    145. try
    146. {
    147. while (true)
    148. {
    149. Array.Resize(ref data, totalBytesRead + dataBlock + 1);
    150. int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
    151. if (bytesRead == 0)
    152. {
    153. break;
    154. }
    155. totalBytesRead += bytesRead;
    156. }
    157. Array.Resize(ref data, totalBytesRead);
    158. return data;
    159. }
    160. catch
    161. {
    162. return null;
    163. }
    164. }
    165. }

怎样提高WebService的性能的更多相关文章

  1. cxf怎样提高webservice性能,及访问速度调优

    性能: 1. 启用FastInfoset(快速信息集)webservice的性能实在是不敢恭维.曾经因为webservice吞吐量上不去,对webservice进行了一些性能方面的优化,采用了Fast ...

  2. IIS webService 并发 性能

    IIS webService 并发 性能     做的WebService,客户压力测试500并发(随机步长60s-90s),响应速度不理想. 1,优化程序,压缩执行时间2,提高IIS“最大工作进程数 ...

  3. 提高ASP.net性能的十种方法

    提高ASP.net性能的十种方法 2014-10-24  空城66  摘自 博客园  阅 67  转 1 转藏到我的图书馆   微信分享:   今天无意中看了一篇关于提高ASP.NET性能的文章,个人 ...

  4. 25条提高iOS App性能的建议和技巧

    这篇文章来自iOS Tutorial Team 成员 Marcelo Fabri, 他是 Movile 的一个iOS开发者. Check out his personal website or fol ...

  5. 提高 DHTML 页面性能

    联盟电脑摘要:本文说明了某些DHTML功能对性能的重大影响,并提供了一些提高DHTML页面性能的技巧. 目录 简介 成批处理DHTML更改 使用innerText 使用DOM添加单个元素 扩展SELE ...

  6. 25条提高iOS app性能的方法和技巧

    以下这些技巧分为三个不同那个的级别---基础,中级,高级. 基础 这些技巧你要总是想着实现在你开发的App中. 1. 用ARC去管理内存(Use ARC to Manage Memory) 2.适当的 ...

  7. 用 Function.apply() 的参数数组化来提高 JavaScript程序性能

    我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...

  8. 如何提高jQuery的性能

    缓存变量DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // ...

  9. 一个用于每一天JavaScript示例-使用缓存计算(memoization)为了提高应用程序性能

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. 机器学习中几种优化算法的比较(SGD、Momentum、RMSProp、Adam)

    有关各种优化算法的详细算法流程和公式可以参考[这篇blog],讲解比较清晰,这里说一下自己对他们之间关系的理解. BGD 与 SGD 首先,最简单的 BGD 以整个训练集的梯度和作为更新方向,缺点是速 ...

  2. CSAPP lab2 二进制拆弹 binary bombs phase_6

    给出对应于7个阶段的7篇博客 phase_1  https://www.cnblogs.com/wkfvawl/p/10632044.htmlphase_2  https://www.cnblogs. ...

  3. 为什么java的main方法必须是静态的

    今天看类型信息时发现一个问题,不能再main方法中打印this关键字的信息,这时想起了之前的知识,不能再静态方法中调用this.理由很简单,this表示“这个对象”,也就是声明一个类的对象,然而静态方 ...

  4. WC----命令行实现对文件信息的统计

    需求分析: 程序处理用户需求的模式为: wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数与程序交互,需实现的功能如下: 1.基本功能 支持 -c ...

  5. final版本发布评价II

    其实我对技术上的问题了解不多,所以有些评语可能说的不对或者压根就没啥用.可直接忽略.请见谅. 1新蜂的俄罗斯方块,UI设计虽然给出了背景和颜色,但是感觉色彩对比也不好,模块之间也不协调.没有更多的说服 ...

  6. jQuery~DOM基础操作

    操作DOM 1.什么是DOM:document object model文档对象模型 2.树形结构 3.什么是节点(node):DOM结构中最小单位,元素.文本.属性...创建节点 var $div ...

  7. 平时在PHP编码时有没有注意到这些问题

    编出一手好代码,这个是需要你在平时开发中日积月累的,平时如果你有注意到以下的那些代码的编码,那么祝贺你,你在技能提升这方面已经垫下了一些基础,编写出一手好代码,说白了就是你特么注意到性能这块的问题,代 ...

  8. 爆打团队 四则运算 beta视频

    爆打团队 四则运算 beta视频链接 http://v.youku.com/v_show/id_XMTU1MjAzNDI0NA==.html?from=s1.8-1-1.2

  9. 原理分析dubbo分布式应用中使用zipkin做链路追踪

    zipkin是什么 Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开 ...

  10. Lisp之根源 --- 保罗格雷厄姆

    Lisp之根源 --- 保罗格雷厄姆 来源 http://daiyuwen.freeshell.org/gb/rol/roots_of_lisp.html 约翰麦卡锡于1960年发表了一篇非凡的论文, ...