WeiXinMPSDK icon indicating copy to clipboard operation
WeiXinMPSDK copied to clipboard

性能问题

Open gwhzh21 opened this issue 3 years ago • 4 comments

此版块专为反馈 bug 及提交需求服务,不负责解答开发问题,请勿发表开发问题, 如果您需要这方面的帮助,请移步问答社区:https://weixin.senparc.com/QA

问题描述

[HttpPost] public async Task<ActionResult> Callback(Senparc.Weixin.MP.Entities.Request.PostModel postModel) { string nowappid = Request.Path.HasValue ? Request.Path.Value.Substring(15) : "";// postModel.AppId; postModel.Token = topnapp.component_token; postModel.EncodingAESKey = topnapp.component_EncodingAESKey;// component_EncodingAESKey;//根据自己后台的设置保持一致 postModel.AppId = topnapp.component_appid;

                var messageHandler = new CustomMessageHandler(Request.GetRequestMemoryStream(), postModel,
                  _bus,_logger,  openset == null ? "0000" : openset.shopid,10);

取消1或者2注释之后。线程一直在增,但是,整个api系统卡住。因为处理不了。 //messageHandler.Execute(); //await messageHandler.ExecuteAsync(CancellationToken.None); //执行 return Content("success"); }

public class CustomMessageHandler : MessageHandler<DefaultMpMessageContext> { protected string shopid = ""; private readonly IBus _bus; private readonly ILogger _logger;

    public CustomMessageHandler(Stream inputStream, PostModel postModel, IBus bus, ILogger mlogger, string mshopid = "0000", int maxRecordCount = 3, bool onlyAllowEncryptMessage = false)
        : base(inputStream, postModel, maxRecordCount, onlyAllowEncryptMessage)
    {
        this._bus = bus;
        this._logger = mlogger;
        this.shopid = mshopid;
    }

    public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
    {
        return null;
    }

public override async Task OnExecutingAsync(CancellationToken cancellationToken) { try { var result = this.RequestDocument.ToString(); _logger.LogDebug($"处理中:{result}"); } catch (Exception ex) { _logger.LogError(ex.ToString()); } } } 推测是线程饥饿的问题。

重现问题步骤(如果可以)
  1. ...
  2. ...
微信官方文档 URL
微信官方文档快照(直接复制关键内容到下方)
发现问题的模块
  • [6.8.101 ] Senparc.Weixin 版本:
  • [ 16.11.101] Senparc.Weixin.MP 版本:
  • [ ] Senparc.Weixin.MP.MVC 版本:
  • [4.8.101 ] Senparc.Weixin.Open 版本: ~- [ ] Senparc.Weixin.QY 版本:~
  • [ ] Senparc.Weixin.Work 版本:
  • [ ] Senparc.Weixin.WxOpen 版本:
  • [ ] Senparc.Weixin.Cache.Redis 版本:
  • [ ] Senparc.Weixin.Cache.Memcached 版本:
  • [ ] 其他模块:
模块对应的 .net 版本
  • [ ] .net 3.5
  • [ ] .net 4.0
  • [ ] .net 4.5
  • [ ] .net standard 2.0
  • [ ] .net core 1.x
  • [ ] .net core 2.x
  • [x] .net core 3.x
开发环境
  • [ ] Visual Studio 2015
  • [ ] Visual Studio 2017
  • [x] Visual Studio 2019
  • [ ] Visual Studio Code
  • [ ] 其他:
缓存环境
  • [x] 服务器内存缓存(默认)
  • [ ] Redis 版本:
  • [ ] Memcached 版本:
  • [ ] 其他:
系统环境
  • [x] Windows,版本:
  • [ ] Linux,版本:
  • [ ] Mac,版本:
  • [ ] 其他:
联系方式

Email:

(也可将问题地址及联系方式发送到 [email protected]

发布问题后,请保持对 issue 的关注,有时会有需要进一步沟通的信息,很长时间内没有得到答复的 issue 将被关闭。

gwhzh21 avatar Dec 18 '20 11:12 gwhzh21

@gwhzh21 代码看的比较迷,是否能PR一个重现问题的单元测试过来?

JeffreySu avatar Dec 21 '20 14:12 JeffreySu

我大约思考下原因。因为是默认模式,并没有指定线程数量,在某些情况下,遇到了,线程的创建跟不上请求数量。目前把api的最小线程增加到了100。继续观察。

gwhzh21 avatar Dec 22 '20 00:12 gwhzh21

这个代码,基本上是实例代码。并没有其它东西。

gwhzh21 avatar Dec 22 '20 00:12 gwhzh21

@gwhzh21 您可以试一下这里的压力测试,也可以改源码把并发做得更大:https://sdk.weixin.senparc.com/SimulateTool

JeffreySu avatar Jan 01 '21 10:01 JeffreySu