发布于 2024-10-29 07:35:06 字数 1748 浏览 1 评论 0原文

我的应用程序遇到严重问题。我们正在开发 GUI 应用程序 + 服务器,可用于两种不同的目的。

  1. GUI 应用程序调用与嵌入式服务器 GUI 应用程序在同一进程中运行的嵌入式服务器,
  2. 并通过 REST 分离进程与分离的独立服务器应用程序进行通信。

我们使用的是 spring.net,因此两种解决方案之间存在细微差别。服务器只是一个上下文,因此解决方案#1 将其直接实例化为新的 spring.net 上下文,解决方案#2 有两个 exe 文件:GUI.exe + 独立服务器 exe。正如我已经说过的,两个应用程序流程几乎相同。

有什么问题吗?独立服务器比解决方案 #1 慢三倍。这意味着分离的独立服务器应用程序比嵌入式应用程序慢三倍。

我用过DotTrace,10分钟就找到了原因。服务器使用 NHibernate,它经常通过反射获取/设置属性。

在第一个解决方案中,当 GUI 应用程序托管在嵌入式服务器中时,反射速度非常快。但是当它在单独的独立服务器上执行时,反射任务非常慢。

以下是慢速解决方案的堆栈跟踪:


- 5,874 ms  System.RuntimeMethodHandle.PerformSecurityCheck(Object, IRuntimeMethodInfo, RuntimeType, UInt32)
  - 4,642 ms  System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, PermissionSet)
  - 36ms System.Security.CodeAccessSecurityEngine.CheckSetHelper(CompressedStack, PermissionSet, PermissionSet, PermissionSet, RuntimeMethodHandleInternal, RuntimeAssembly, SecurityAction)
  - 1ms System.Reflection.RuntimeMethodInfo.get_Value

快速解决方案:


- 5 ms  •  10,740 calls  •  System.RuntimeMethodHandle.PerformSecurityCheck(Object, IRuntimeMethodInfo, RuntimeType, UInt32)
  -  1 ms  •  10,740 calls  •  System.Reflection.RuntimeMethodInfo.get_Value

如您所见,慢速解决方案的杀手是对 System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper 的额外调用。独立服务器应该像 GUI 一样自动运行,完全受信任。

您是否有任何解决方案如何关闭此检查或如何设置独立服务器应用程序?当我比较两个 app.configs 时,我无法发现所描述问题的任何差异。

编辑:

我们终于调查了原因,解决方案是唯一明确的。

独立服务器使用ContextRegistry.GetContext()实例化spring.net的上下文,但嵌入式服务器使用标准new XmlApplicationContext(new[] {"..."})。这个简单的差异导致了如此显着的性能影响。

看起来 spring 的 app.config 上下文处理程序做了“任何错误的事情”,但我们还没有时间调查真正的目的。

I've got serious problem with our application. We are developing GUI application + server which can be used in two different purposes.

  1. GUI application invoking embedded server which runs in the same process as embedded server
  2. GUI application is communicated with separated standalone server application via REST - separated process.

We are using spring.net so there are small differences between both solutions. Server is just one context so the solution #1 instantiates it directly as new spring.net context and solution #2 has two exe files: GUI.exe + standalone server exe. As I already said, both application flows are almost the same.

Whats the issue? Standalone server is three times slower than solution #1. It means separated standalone server application is three times slower than embedded one.

I've used DotTrace and find the find the reason in 10 minutes. Server uses NHibernate which get/set properties via reflection very often.

In the first solution when GUI application hosted in embedded server, reflection is very quick. But when it's on separate standalone server perform, reflections tasks are very slow.

Here are stack traces for slow solution:


- 5,874 ms  System.RuntimeMethodHandle.PerformSecurityCheck(Object, IRuntimeMethodInfo, RuntimeType, UInt32)
  - 4,642 ms  System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, PermissionSet)
  - 36ms System.Security.CodeAccessSecurityEngine.CheckSetHelper(CompressedStack, PermissionSet, PermissionSet, PermissionSet, RuntimeMethodHandleInternal, RuntimeAssembly, SecurityAction)
  - 1ms System.Reflection.RuntimeMethodInfo.get_Value

Fast solution:


- 5 ms  •  10,740 calls  •  System.RuntimeMethodHandle.PerformSecurityCheck(Object, IRuntimeMethodInfo, RuntimeType, UInt32)
  -  1 ms  •  10,740 calls  •  System.Reflection.RuntimeMethodInfo.get_Value

As you can see, the slow solution killer is the aditional call to System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper. Standalone server should automatically run as full trusted as well as GUI does.

Do you have any solutions how to switch this check off or how to set up the standalone server application? When I compare both app.configs, i'm not able to find any difference regarding described issue.

EDIT:

We have finally investigated the reason and the solution was the only clear.

Standalone server instantiates spring.net's context with using ContextRegistry.GetContext() but embedded one uses standard new XmlApplicationContext(new[] {"..."}). This simple difference results into so significant performance hit.

It seems that spring's app.config context handler do "any wrong stuff" but we have not time to investigate the real purpose yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪之触 2024-11-05 07:35:06

嵌入式和独立服务器是如何创建的?它们是生成的代码还是您编写的代码?您是否已验证独立服务器是否在完全信任下运行?使用什么框架来处理 REST 请求?我之前以类似的方式使用过 NHibernate(客户端与 Web 服务通信以及使用 NH 的 Web 服务),并且从未见过由于 CAS 导致每个请求有 5-6 秒的延迟。您确定正确缓存了 SessionFactory 吗?

How are the embedded and standalone servers being created? Are they generated code or code that you've written? Have you verified that the standalone server is running under full trust? What framework is being used to handle the REST requests? I've used NHibernate in a similar manner before (client talking to web service and web service using NH) and never seen a 5-6 second delay per request because of CAS. Are you sure that you are caching your SessionFactory properly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文