C# 代码在 IIS 上运行速度快,但在 Mono 上运行速度慢 - 如何改进?
我有一个 ASP.NET 应用程序,它在我的 Windows 开发计算机上运行良好。不过,服务器是运行 Mono 的 Linux,一旦上传相同的代码,其运行速度就会比在 Windows 机器上慢 4 到 5 倍(例如,一项任务需要 25 秒,而一项任务需要 5 秒)。
这种性能是 Mono 的已知问题吗?我能做些什么吗?代码主要是文本处理、字符串替换、正则表达式等,如果这有什么区别的话。我已经在本地使用 VS 分析和调试了我的代码,但我不知道是否可以使用 Mono 在服务器上进行远程调试,或者我下一步需要做什么才能真正修复它。
I have an ASP.NET application that is working well on my Windows development machine. The server is Linux running Mono though, and once uploaded the same code is running 4 or 5 times slower there than it does on the Windows box (taking 25 seconds vs 5 seconds for one task for instance).
Is this performance a known problem with Mono ? And is there anything I can do about it ? The code is mostly text processing, string replaces, regexes and the like, if that makes any difference. I've profiled and debugged my code using VS locally, but I don't know if it's possible to do remote debugging on the server with Mono, or what I need to do next to fix it really.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正则表达式是 Mono 的一个特别薄弱的领域。 Mono 的 Regex 类始终使用解释型代码,而 .Net 可以将其转换为编译型 IL,从而实现更快的执行速度。
大多数其他形式的文本处理(例如替换)应该大致相似。
Regexes are a particularly weak area for Mono. Mono's Regex class always uses interpreted code, while .Net can turn it into compiled IL, resulting in much faster execution.
Most other forms of text processing like replaces should be roughly similar.
安装 Mono,最好是在与您的服务器类似的 Linux 系统上。 在 Mono 上分析您的代码并查看瓶颈所在。
我有一个在 Linux 服务器上运行的 Mono 应用程序,它遵循 Apache 日志文件。我在 Windows 上开发了它,在 Linux 上测试它时,我发现它在 Mono 2.4 上比 .NET 3.5 慢 8-10 倍。它的大部分时间都花在 Regex.Match 和字符串函数上。只需在 4 次调用 string.EndsWith() 中指定 StringComparison.Ordinal,我就能将 Mono 中程序的整体速度提高一倍。如果您想要序数字符串比较,这可能会提高您的速度。
即使使用 StringComparison.Ordinal,string.StartsWith() 仍然很慢。通过编写我自己的 string.StartsWith() 版本,我的整体程序速度提高了 25%。
因此,如果您的应用程序需要执行序数比较,请尝试指定 StringComparison.Ordinal 或编写您自己的字符串函数。
Install Mono, preferably on a Linux system similar to your server. Profile your code on Mono and see where the bottlenecks are.
I have a Mono app running on a Linux server that follows Apache log files. I developed it on Windows and when testing it on Linux, I found it to be something like 8-10 times slower on Mono 2.4 vs. .NET 3.5. Most of its time is spent in Regex.Match and string functions. I was able to double the overall speed of the program in Mono just by specifying StringComparison.Ordinal in 4 calls to string.EndsWith(). If ordinal string comparisons are what you want, that might give you a speed boost.
Even with StringComparison.Ordinal, string.StartsWith() was still slow. I got a 25% increase in overall program speed by writing my own version of string.StartsWith().
So if ordinal comparisons are what your app needs to do, try specifying StringComparison.Ordinal or writing your own string functions.
您使用 mod_mono 还是 mod_proxy 来实现此目的?虽然 Mono 的功能有限,但使用 mod_mono 所获得的总体延迟也比 mod_proxy 少。
请参阅Mono ASP.NET 常见问题解答中的“mod_mono 和 mod_proxy”部分
Are you using mod_mono or mod_proxy for this? While there are limitations to what you can get out of Mono, you're also going to get less general delay using mod_mono than mod_proxy.
Please see the section "mod_mono and mod_proxy" at Mono ASP.NET FAQ
您正在使用 StringBuilder 对象,还是正在执行字符串串联。如果您正在进行大量字符串工作,您会遇到一些性能错误。
但我认为 leppie 很清楚,微软花了很多钱测试 ASP.net 并将其集成到 IIS 中,因此它非常非常快。如果您不想为 MS 盒子付费,那么您将不得不面对这样一个事实:Mono 是开源的,而 IIS 是经过测试的商业产品。
这里就是一个很好的例子,微软实际上使用与 IIS 服务器配合使用的内核模式库修改了 Windows 本身。正确使用缓存的正确架构的应用程序可以在此代码中获得非常好的性能提升:HTTP.sys
Are you using StringBuilder objects, or are you doing string catenation. If you are doing lots of string work you will hit some performance bugs.
But I think leppie has it on the head, Microsoft spent a lot of money testing and integrating ASP.net into IIS so that it was really, really fast. If you don't want to pay for the MS box, then you are going to have to deal with the fact that Mono is open source and IIS is a tested commercial product.
A good example is here, where Microsoft actually modified Windows itself with a kernel mode library that works with the IIS server. Correctly architected apps using the cache properly can get very good performance increases in this code: HTTP.sys
根据这项学术研究,由于某些原因,某些 Linux 发行版中的 Mono 性能可能会受到阻碍Linux 系统“更快地访问硬盘缓存/交换空间”[与 Windows 相比]。
由于 Linux 发行版内核调度配置各不相同,因此了解 Linux 服务器上运行的操作系统发行版和版本以及应用程序可用的内存和 CPU 周期将非常有用。
According to this academic study, Mono performance can be hampered in some Linux distributions due to some Linux systems' "much quicker propensity to go to the hard drive cache/swap space" [when compared to Windows].
As Linux distribution kernel scheduling configurations vary, it would be useful to know what OS distribution and version you're running on your Linux server, and how much memory and CPU cycles are available to your application.