Skip to content

Commit

Permalink
更新异步缓存之后,添加并发单元测试 ,用于发现并发延迟的问题 #1693 #1693 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySu committed Apr 28, 2019
1 parent 68a3ca1 commit 71ce80c
Showing 1 changed file with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
using System.Threading.Tasks;
using Senparc.Weixin.MP.Test.CommonAPIs;
using System.Threading;
using System.Collections.Generic;
using Senparc.CO2NET.Extensions;
using Senparc.CO2NET.Cache;
using Senparc.CO2NET.Cache.Redis;

namespace Senparc.Weixin.MP.Test.AdvancedAPIs
{
Expand All @@ -17,13 +21,14 @@ public void TestMethod1()
Console.WriteLine("1. Start");

bool finished = false;
Thread thread = new Thread(()=> {
Thread thread = new Thread(() =>
{
Task.Factory.StartNew(async () =>
{
var d2 = SystemTime.Now;
var tagJsonResult = await Senparc.Weixin.MP.AdvancedAPIs.UserTagApi.GetAsync(base._appId);
Console.WriteLine("3. tagJsonResult 1:" + string.Join(",", tagJsonResult.tags.Select(z => z.name)));
Console.WriteLine("4. 用时:" + (SystemTime.Now-d2).TotalMilliseconds+" ms");
Console.WriteLine("4. 用时:" + (SystemTime.Now - d2).TotalMilliseconds + " ms");
return tagJsonResult;
}).ContinueWith(async task =>
Expand All @@ -45,7 +50,70 @@ public void TestMethod1()
Thread.Sleep(5);
}

Console.WriteLine("6. End:"+(SystemTime.Now -d1).TotalMilliseconds+" ms");
Console.WriteLine("6. End:" + (SystemTime.Now - d1).TotalMilliseconds + " ms");

}

/// <summary>
/// 并发测试
/// </summary>
[TestMethod]
public void ConcurrentTesting()
{
var threadCount = 30;//同时运行线程数
var finishedCount = 0;
var threads = new List<Thread>();

var apiTotalTime = 0D;//接口总耗时

//指定缓存
Senparc.Weixin.Cache.Redis.Register.RegisterDomainCache();//注册领域缓存
CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisObjectCacheStrategy.Instance);

var cacheStrategy = CacheStrategyFactory.GetObjectCacheStrategyInstance();
Console.WriteLine($"== 开始,缓存:{cacheStrategy.GetType()} ==");

for (int i = 0; i < threadCount; i++)
{
var index = i;
var thread = new Thread(async () =>
{
try
{
var dt0 = SystemTime.Now;
Console.WriteLine($"{SystemTime.Now.ToString("HH:mm:ss.ffffff")}\t{Thread.CurrentThread.Name} START");
var result = await MP.AdvancedAPIs.QrCodeApi.CreateAsync(base._appId, 300, 100000 * 10 + index, QrCode_ActionName.QR_SCENE);
var apiRunTime = (SystemTime.Now - dt0).TotalMilliseconds;
Console.WriteLine($"{SystemTime.Now.ToString("HH:mm:ss.ffffff")}\t{Thread.CurrentThread.Name} RESULT - 耗时 {apiRunTime:###,###}ms - {result.ticket} - {result.url}");
apiTotalTime += apiRunTime;
}
catch (Exception ex)
{
Console.WriteLine($"{SystemTime.Now.ToString("HH:mm:ss.ffffff")}\t{ex.ToString()}");
}
finally
{
finishedCount++;
}
});
thread.Name = "T" + index.ToString("00");

threads.Add(thread);
}

var dt1 = SystemTime.Now;
foreach (var thread in threads)
{
thread.Start();//尽量在相近的时间开始
}

while (finishedCount < threadCount)
{
//等待线程结束(为测试避免线程锁死,这里不使用信号灯)
}

var costTime = (SystemTime.Now - dt1).TotalMilliseconds;
Console.WriteLine($"== 运行结束,总耗时 {costTime:###,###}ms,平均 {apiTotalTime / threadCount:###,###}ms ==");

}
}
Expand Down

0 comments on commit 71ce80c

Please sign in to comment.