简体   繁体   English

ManualResetEventSlim 信号是否会降低性能?

[英]Does ManualResetEventSlim signaling degrades performance?

I am using ManualResetEventSlim to have signaling mechanism in my application and It works great if requests/sec are 100. As I increase request/sec, it gets worse.我正在使用 ManualResetEventSlim 在我的应用程序中使用信号机制,如果请求/秒为 100,它会很好。随着我增加请求/秒,它会变得更糟。

Example:例子:

100 Requests/sec -> 90% transaction done in 250 ms and Throughput (Success request/sec) is 134. 100 个请求/秒 -> 90% 的事务在 250 毫秒内完成,吞吐量(成功请求/秒)为 134。

150 Requests/sec -> 90% transaction done in 34067 ms and Throughput (Success request/sec) is 2.2. 150 请求/秒 -> 90% 的事务在 34067 毫秒内完成,吞吐量(成功请求/秒)为 2.2。

I use ConcurrentDictionary as give below:我使用 ConcurrentDictionary 如下所示:

// <key, (responseString,ManualResetEventSlim) >
private static ConcurrentDictionary<string, (string, ManualResetEventSlim)> EventsDict = new ConcurrentDictionary<string, (string, ManualResetEventSlim)>();

Below given process describes need for ManualResetEventSlim ( Api Solution 1 and Api Solution 2 are completely:下面给出的过程描述了对 ManualResetEventSlim 的需求( Api 解决方案 1Api 解决方案 2完全是:

  1. Api Solution 1 (REST Api) received a request, it added an element (null, ManualResetEventSlim) in ConcurrentDictionary against a key and called thirdparty service (SOAP) using async/await. Api 解决方案 1(REST Api)收到一个请求,它在 ConcurrentDictionary 中针对一个添加一个元素(null,ManualResetEventSlim),并使用 async/await 调用第三方服务(SOAP)。 Thirdparty soap api returned acknowledgement response but actual response is pending.第三方 soap api 返回确认响应,但实际响应待定。 After getting acknowledgement response, it goes to ManualResetEventSlim.wait得到确认响应后,转到 ManualResetEventSlim.wait

  2. Once thirdparty processed the request, it calls Api Solution 2 (SOAP) using exposed method and sends actual response.一旦第三方处理了请求,它就会使用公开的方法调用Api 解决方案 2 (SOAP)并发送实际响应。 Api solution 2 sends response to Api Solution 1 (REST Api) by making http request and then inserts data to database for auditlog. Api 解决方案 2 通过发出 http 请求向Api 解决方案 1 (REST Api)发送响应,然后将数据插入数据库以进行审计日志。

  3. Api Solution 1 will get key from response string and update response string in ConcurrentDictionary and set signal. Api 解决方案 1将从响应字符串中获取密钥并更新 ConcurrentDictionary 中的响应字符串并设置信号。

  4. Api Solution 1 disposes ManualResetEventSlim object before returning response to client. Api 解决方案 1在向客户端返回响应之前处理 ManualResetEventSlim object。

I think, you should be able to get rid of the blocking code by replacing (string, ManualResetEventSlim) with TaskCompletionSource<string> :我认为,您应该能够通过将(string, ManualResetEventSlim)替换为TaskCompletionSource<string>来摆脱阻塞代码:

In Solution 1, you would do something along this:在解决方案 1 中,您将执行以下操作:

TaskCompletionSource<string> tcs = new TaskCompletionSource<string>()
EventsDict.AddOrUpdate( key, tcs );
await KickOffSolution2ThirdParty( /*...*/ );
string result = await tcs.Task; // <-- now not blocking any thread anymore

And the counterpart:和对方:

void CallbackFromSolution2( string key, string result )
{
     if( EventsDict.TryRemove(key, out TaskCompletionSource<string> tcs )
     {
         tcs.SetResult(result);
     }
}

This is of course only a coarse outline of the idea.这当然只是这个想法的粗略轮廓。 But hopefully enough to make my line of thought understandable.但希望足以让我的思路可以理解。 I cannot test this right now, so any improvements/corrections welcome.我现在无法对此进行测试,因此欢迎任何改进/更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM