接上一篇 通過一個範例形象地理解C# async await非同步
我在 .NET與巨量資料 中吐槽前同事在雙層迴圈體中(肯定是單執行緒了)頻繁請求es,導致介面的總耗時很長。這不能怪前同事,確實難寫,會使程式碼複雜度增加。
評論區有人說他的理解是使用非同步增加了系統吞吐能力,這個理解是正確的,但對於單個介面的單次請求而言,它是單執行緒的,耗時反而可能比同步還慢。如何縮短單個介面的單次請求的時間呢(要求:儘量不增加程式碼複雜度)?請看下文。
先直接測試,看結果,下面再放程式碼
截圖說明:單次請求耗時約0.5秒,共10次請求,耗時約 0.5秒×10=5秒
截圖說明:單次請求耗時約0.5秒,共10次請求,耗時約 0.5秒
截圖說明:單次請求耗時約0.5秒,共10次請求,並行數是5,耗時約 0.5秒×10÷5=1秒
伺服器端
伺服器端和使用者端是兩個獨立的工程,測試時在一起跑,但其實可以分開部署,部署到不同的機器上
伺服器端是一個web api介面,用.NET 6、VS2022開發,程式碼如下:
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
[HttpGet]
[Route("[action]")]
public async Task<Dictionary<int, int>> Get(int i)
{
var result = new Dictionary<int, int>();
await Task.Delay(500); //模擬耗時操作
if (i == 0)
{
result.Add(0, 5);
result.Add(1, 4);
result.Add(2, 3);
result.Add(3, 2);
result.Add(4, 1);
}
else if (i == 1)
{
result.Add(0, 10);
result.Add(1, 9);
result.Add(2, 8);
result.Add(3, 7);
result.Add(4, 6);
}
return result;
}
}
使用者端
大家看使用者端程式碼時,不需要關心伺服器端怎麼寫
使用者端是一個Winform工程,用.NET 6、VS2022開發,程式碼如下:
public partial class Form1 : Form
{
private readonly string _url = "http://localhost:5028/Test/Get";
public Form1()
{
InitializeComponent();
}
private async void Form1_Load(object sender, EventArgs e)
{
//預熱
HttpClient httpClient = HttpClientFactory.GetClient();
await (await httpClient.GetAsync(_url)).Content.ReadAsStringAsync();
}
//非並行非同步(順序執行的非同步)
private async void button3_Click(object sender, EventArgs e)
{
await Task.Run(async () =>
{
Log($"==== 非並行非同步 開始,執行緒ID={Thread.CurrentThread.ManagedThreadId} ========================");
Stopwatch sw = Stopwatch.StartNew();
HttpClient httpClient = HttpClientFactory.GetClient();
var tasks = new Dictionary<string, Task<string>>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2; i++)
{
int sum = 0;
for (int j = 0; j < 5; j++)
{
Dictionary<int, int> dict = await RequestAsync(_url, i);
if (dict.ContainsKey(j))
{
int num = dict[j];
sum += num;
sb.Append($"{num}, ");
}
}
Log($"輸出:sum={sum}");
}
Log($"輸出:{sb}");
sw.Stop();
Log($"==== 結束,執行緒ID={Thread.CurrentThread.ManagedThreadId},耗時:{sw.Elapsed.TotalSeconds:0.000}秒 ========================");
});
}
// 並行非同步
private async void button4_Click(object sender, EventArgs e)
{
await Task.Run(async () =>
{
Log($"==== 並行非同步 開始,執行緒ID={Thread.CurrentThread.ManagedThreadId} ========================");
Stopwatch sw = Stopwatch.StartNew();
HttpClient httpClient = HttpClientFactory.GetClient();
var tasks = new Dictionary<string, Task<Dictionary<int, int>>>();
StringBuilder sb = new StringBuilder();
//雙層迴圈寫第一遍
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
var task = RequestAsync(_url, i);
tasks.Add($"{i}_{j}", task);
}
}
//雙層迴圈寫第二遍
for (int i = 0; i < 2; i++)
{
int sum = 0;
for (int j = 0; j < 5; j++)
{
Dictionary<int, int> dict = await tasks[$"{i}_{j}"];
if (dict.ContainsKey(j))
{
int num = dict[j];
sum += num;
sb.Append($"{num}, ");
}
}
Log($"輸出:sum={sum}");
}
Log($"輸出:{sb}");
sw.Stop();
Log($"==== 結束,執行緒ID={Thread.CurrentThread.ManagedThreadId},耗時:{sw.Elapsed.TotalSeconds:0.000}秒 ========================");
});
}
// 並行非同步(控制並行數量)
private async void button5_Click(object sender, EventArgs e)
{
await Task.Run(async () =>
{
Log($"==== 並行非同步(控制並行數量) 開始,執行緒ID={Thread.CurrentThread.ManagedThreadId} ===================");
Stopwatch sw = Stopwatch.StartNew();
HttpClient httpClient = HttpClientFactory.GetClient();
var tasks = new Dictionary<string, Task<Dictionary<int, int>>>();
Semaphore sem = new Semaphore(5, 5);
StringBuilder sb = new StringBuilder();
//雙層迴圈寫第一遍
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
var task = RequestAsync(_url, i, sem);
tasks.Add($"{i}_{j}", task);
}
}
//雙層迴圈寫第二遍
for (int i = 0; i < 2; i++)
{
int sum = 0;
for (int j = 0; j < 5; j++)
{
Dictionary<int, int> dict = await tasks[$"{i}_{j}"];
if (dict.ContainsKey(j))
{
int num = dict[j];
sum += num;
sb.Append($"{num}, ");
}
}
Log($"輸出:sum={sum}");
}
sem.Dispose(); //別忘了釋放
Log($"輸出:{sb}");
sw.Stop();
Log($"==== 結束,執行緒ID={Thread.CurrentThread.ManagedThreadId},耗時:{sw.Elapsed.TotalSeconds:0.000}秒 ========================");
});
}
private async Task<Dictionary<int, int>> RequestAsync(string url, int i)
{
Stopwatch sw = Stopwatch.StartNew();
HttpClient httpClient = HttpClientFactory.GetClient();
var result = await (await httpClient.GetAsync($"{url}?i={i}")).Content.ReadAsStringAsync();
sw.Stop();
Log($"執行緒ID={Thread.CurrentThread.ManagedThreadId},請求耗時:{sw.Elapsed.TotalSeconds:0.000}秒");
return JsonSerializer.Deserialize<Dictionary<int, int>>(result);
}
private async Task<Dictionary<int, int>> RequestAsync(string url, int i, Semaphore semaphore)
{
semaphore.WaitOne();
try
{
Stopwatch sw = Stopwatch.StartNew();
HttpClient httpClient = HttpClientFactory.GetClient();
var result = await (await httpClient.GetAsync($"{url}?i={i}")).Content.ReadAsStringAsync();
sw.Stop();
Log($"執行緒ID={Thread.CurrentThread.ManagedThreadId},請求耗時:{sw.Elapsed.TotalSeconds:0.000}秒");
return JsonSerializer.Deserialize<Dictionary<int, int>>(result);
}
catch (Exception ex)
{
Log($"錯誤:{ex}");
throw;
}
finally
{
semaphore.Release();
}
}
#region Log
private void Log(string msg)
{
msg = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} {msg}\r\n";
if (this.InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
txtLog.AppendText(msg);
}));
}
else
{
txtLog.AppendText(msg);
}
}
#endregion
private void button6_Click(object sender, EventArgs e)
{
txtLog.Text = string.Empty;
}
}
請思考程式碼中的註釋"雙層迴圈寫第一遍""雙層迴圈寫第二遍",這個寫法儘量不增加程式碼複雜度,試想一下,如果你用Task.Run且不說佔用執行緒,就問你怎麼寫能簡單?
有人說,我會,這樣寫不就行了:
Dictionary<int, int>[] result = await Task.WhenAll(tasks.Values);
那請問,你接下來怎麼寫?我相信你肯定會寫,但問題是,程式碼的邏輯結構變了,程式碼複雜度增加了!
所以"雙層迴圈寫第一遍""雙層迴圈寫第二遍"是什麼意思?你即能方便合併,又能方便拆分,程式碼邏輯結構沒變,只是複製了一份。
RequestAsync的複雜度並沒有因為Semaphore的引入變得更復雜,增加的程式碼可以接受。
[HttpPost]
[Route("[action]")]
public async Task<List<NightActivitiesResultItem>> Get([FromBody] NightActivitiesPostData data)
{
return await ServiceFactory.Get<NightActivitiesService>().Get(data.startDate, data.endDate, data.startTime, data.endTime, data.threshold, data.peopleClusters);
}
public async Task<List<NightActivitiesResultItem>> Get(string strStartDate, string strEndDate, string strStartTime, string strEndTime, decimal threshold, List<PeopleCluster> peopleClusterList)
{
List<NightActivitiesResultItem> result = new List<NightActivitiesResultItem>();
DateTime startDate = DateTime.ParseExact(strStartDate, "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime endDate = DateTime.ParseExact(strEndDate, "yyyyMMdd", CultureInfo.InvariantCulture);
string[][] strTimes;
if (string.Compare(strStartTime, strEndTime) > 0)
{
strTimes = new string[2][] { new string[2], new string[2] };
strTimes[0][0] = strStartTime;
strTimes[0][1] = "235959";
strTimes[1][0] = "000000";
strTimes[1][1] = strEndTime;
}
else
{
strTimes = new string[1][] { new string[2] };
strTimes[0][0] = strStartTime;
strTimes[0][1] = strEndTime;
}
foreach (PeopleCluster peopleCluster in peopleClusterList)
{
for (DateTime day = startDate; day <= endDate; day = day.AddDays(1))
{
string strDate = day.ToString("yyyyMMdd");
int sum = 0;
foreach (string[] timeArr in strTimes)
{
List<PeopleFeatureAgg> list = await ServiceFactory.Get<PeopleFeatureQueryService>().QueryAgg(strDate + timeArr[0], strDate + timeArr[1], peopleCluster.ClusterIds);
Dictionary<string, int> agg = list.ToLookup(a => a.ClusterId).ToDictionary(a => a.Key, a => a.First().Count);
foreach (string clusterId in peopleCluster.ClusterIds)
{
if (agg.TryGetValue(clusterId, out int count))
{
sum += count;
}
}
}
if (sum >= threshold) //大於或等於閾值
{
NightActivitiesResultItem item = new NightActivitiesResultItem();
item.peopleCluster = peopleCluster;
item.date = strDate;
item.count = sum;
foreach (string[] timeArr in strTimes)
{
PeopleFeatureQueryResult featureList = await ServiceFactory.Get<PeopleFeatureQueryService>().Query(strDate + timeArr[0], strDate + timeArr[1], peopleCluster.ClusterIds, 10000);
item.list.AddRange(featureList.list);
}
item.dataType = "xxx";
result.Add(item);
}
}
}
var clusters = result.ConvertAll<PeopleCluster>(a => a.peopleCluster);
await ServiceFactory.Get<PersonScoreService>().Set(OpeType.Xxx, peopleClusterList, clusters, startDate.ToString("yyyyMMddHHmmss"), endDate.ToString("yyyyMMddHHmmss"));
return result;
}
上述介面程式碼,它有三層迴圈,在第三層迴圈體中await,第一層迴圈的數量會達到1000甚至10000,第二層迴圈的數量會達到30(一個月30天),甚至90(三個月),第三層迴圈的數量很少。
那麼總請求次數會達到3萬甚至90萬,如果不使用並行非同步請求,那耗時將會很長。
請問:在儘量不增加程式碼複雜度的前提下,怎麼優化,縮短該服務介面的執行時間?
我知道肯定有人要說我了,你傻啊,請求3萬次?你可以改寫,只請求一次,或者按天來,每天的資料只請求一次,那最多也才90次。然後在記憶體中計算,這不就快了?
確實是這樣的,確實不應該請求3萬次。但問題沒這麼簡單:
windows的cmd命令:
netstat -ano | findstr 5028
通過工作管理員檢視,非並行非同步,執行緒數很少,請求開始後只增加了一兩個執行緒。並行非同步執行緒數較多。並行非同步並控制並行量的活,執行緒數少很多。
semaphore.WaitOne()阻塞執行緒一直阻塞到semaphore.Release(),使用了Semaphore的介面,被請求一次,阻塞一個執行緒,不過問題不是很大。
.NET只有一個CLR執行緒池和一個非同步執行緒池(完成埠執行緒池),當執行緒池中執行緒數量不夠用時,.NET每秒才增加1到2個執行緒,執行緒增加的速度非常緩慢。結合非同步,考慮一下這是為什麼?
我認為(不一定對):
注意是AsyncParallel分支
https://gitee.com/s0611163/AsyncAwaitDemo2/tree/AsyncParallel/
我努力嘗試,不一定能解決我想解決的問題,倒是可以幫助大家理解非同步、並行、並行。
上述我寫的實際介面,耗時長沒有問題,還有很多服務介面,它們通過定時任務在凌晨錯開時間跑,結果儲存在資料庫中供前端查詢。這是離線分析。
前同事寫的介面是實時的,所以他覺得es慢了,如果只請求一次呢,可能es的查詢語句也不好寫,所以用ClickHouse,利用SQL靈活性,只查詢一次,然後在記憶體中計算。