怎样提高WebService的性能

服务器端WebService程序:

C#代码  

    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. }
时间: 2024-08-26 09:55:20

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

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

这篇文章来自iOS Tutorial Team 成员 Marcelo Fabri, 他是 Movile 的一个iOS开发者. Check out his personal website or follow him on Twitter.原文地址      当我们开发iOS应用时,好的性能对我们的App来说是很重要的.你的用户也希望如此,但是如果你的app表现的反应迟钝或者很慢就会让你得到不好的评论. 然而,由于IOS设备的限制有时很难工作得很正确.我们开发时有很多需要我们记住这些容易忘记的决定

25条提高iOS App性能的技巧和诀窍

 这篇文章来自iOS Tutorial Team 成员 Marcelo Fabri, 他是 Movile 的一个iOS开发者. Check out his personal website or follow him on Twitter.原文地址      当我们开发iOS应用时,好的性能对我们的App来说是很重要的.你的用户也希望如此,但是如果你的app表现的反应迟钝或者很慢也会伤害到你的审核. 然而,由于IOS设备的限制有时很难工作得很正确.我们开发时有很多需要我们记住这些容易忘记的决定对

使用TraceView调试并提高Android应用性能

TraceView是android的一个可视化的调试工具.借助它,你可以具体了解你的代码在运行时的性能表现.它能帮你更好了解到代码运行过程的效率,进而改善代码,提高你应用的体验. 在用TraceView工具之前,你需要先生成TraceView日志文件,文件包含了应用的跟踪的相关信息,然后再用TraceView工具对文件进行分析. 生成Trace日志的两种方法 代码方式 在代码中使用Debug类来跟生成日志文件,调用startMethodTracing()方法开始跟踪,调用stopMethodTr

如何提高jQuery的性能

缓存变量DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // 建议 $element = $('#element'); h = $element.height(); $element.css('height',h-20); 避免全局变量jQuery与javascript一样,一般来说,最好确保你的变量在函数作用域内. // 糟糕 $element = $('#

哪些方法能提高香港服务器性能

哪些方法能提高香港服务器性能 1.为硬盘存储部分增加冗余硬盘和阵列控制卡,提供数据冗余,并且大幅度增加系统的IO性能. 2.增加冗余的CPU,使用SMP(对称性多处理器)技术提高系统性能,并且增加了中心处理的冗余. 3.增加冗余网卡,提高网络的IO性能,在某块网卡出现故障时,服务器不会与网络中断连接.4.增加冗余电源模块,提高服务器的供电能力,一旦某个电源模块出现问题时,系统不会因电源中断而导致宕机.5.增加内存,满足操作系统及不断增加的优化和应用程序的需求,提高香港服务器性能.另外,需要对于香

如何提高windows的性能

默认windows启用了很多的效果,我们可能平时没有注意到,比如什么淡入淡出效果之类的,其实在我看来,这些效果不仅难看,而且影响了windows的性能,下面我就来说说怎么通过关闭这些效果来提高windows的性能.首先打开资源管理器, 在“这台电脑”上右击,选择“属性”, 然后选择“高级系统设置”,然后选择下面图片中的 可以看见windows默认启用了很多的效果,我们修改后如下 之所以保留这两个是有其道理的,“平滑屏幕字体边缘”是为了一些应用程序贴边隐藏后,然后鼠标划过,这些应用程序可以闪现,如

提高磁盘访问性能 - NtfsDisableLastAccessUpdate

这个技巧可以提高磁盘访问性能,不过仅适用于NTFS文件系统. 我们知道,当在磁盘管理应用程序中列出目录结构时──效果类似“资源管理器”.“文件管理 器”(Windows NT  3.xx/4.0下的称呼).DOS下的DIR命令,通常每次目录被显示或访问后,系统都会更新最后访问的日期/时间标记,我们可以阻止这种无谓的消耗, 打开“注册表编辑器”,找到[HKEY_LOCAL  _MACHINE\System\CurrentControlSet\Control\FileSystem],在右侧窗格中找到

提高 Web 站点性能的最佳实践

本文内容 提高 Web 站点性能的最佳实践 最大限度减少 HTTP 请求 使用内容分发网络(CDN) 添加 Expires 或 Cache – Control 头 Gzip 组件 CSS 放在页面顶部 JavaScript 放在页面底部 避免 CSS 表达式 使用外部 JavaScript 和 CSS 减少 DNS 查询 精简 JavaScript 和 CSS 避免重定向 删除重复的脚本 配置 ETags 使得 Ajax 可缓存 尽早强制地发送缓冲给客户端 用 GET 发送 Ajax 请求 延迟

提高 web 应用性能之 CSS 性能调优

CSS 性能调优 CSS 代码的分析与渲染都是由浏览器来完成的,所以,了解浏览器的 CSS 工作机制对我们的优化有至关重要的作用.这篇文章我们主要从如下几个方面入手来介绍一下 CSS 的性能优化: Style 标签的相关调优 特殊的 CSS 样式使用方式 CSS 缩写 CSS 的声明 CSS 选择器 把 Stylesheets 放在 HTML 页面头部: 浏 览器在所有的 stylesheets 加载完成之后,才会开始渲染整个页面,@import 就相当于是把 <link> 标签放在页面的底部