如何在混合匹配调用的测试方法的同时对代码运行数小时的测试
本文关键字:代码 运行 测试 小时 测试方法 混合 调用 | 更新日期: 2023-09-27 18:34:55
我需要对某些代码运行 2 小时的测试。我正在使用四种异步任务方法,而不是每次调用所有方法,我想一次调用两个,然后下一次调用一个,然后下次调用所有四个,依此类推,依此类推。
调用哪些并不重要,我只需要每次都混合调用,而不是每次都调用所有方法。
我需要知道如何设置计时器,以便在 2 小时后它将停止,并有一种混合方法调用的方法。
我正在寻找的代码将进入static void Main(string[] args){}
基本上我需要某种类型的循环。
class Program
{
static void Main(string[] args)
{
}
static async Task Test1()
{
int[] elevIds = new int[] { 4440, 4441, 4442 };
IElevation[] elevs = new IElevation[3];
elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));
using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
{
using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
}
}
static async Task Test2()
{
int[] elevIds = new int[] { 4430, 4431, 4434, 4435 };
IElevation[] elevs = new IElevation[4];
elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));
using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
{
using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
}
}
static async Task Test3()
{
int[] elevIds = new int[] { 4427 };
IElevation[] elevs = new IElevation[1];
elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));
using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
{
using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
}
}
static async Task Test4()
{
int[] elevIds = new int[] { 2282, 4309 };
IElevation[] elevs = new IElevation[2];
elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));
using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
{
using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
}
}
}
}
您可以创建与其签名匹配的delegate
,然后将其添加到该委托的列表中。然后使用 Random
确定该委托列表中有多少方法以及所述方法的索引,然后执行它们,并在继续下一个随机确定的组之前等待所有这些方法。
这是我最终实现的内容,它运行得很好。
private async void Form1_Load(object sender, EventArgs e)
{
await TaskProcessor();
}
async Task TaskProcessor()
{
DateTime end = DateTime.Now.AddHours(4);
while (end > DateTime.Now)
{
for (byte i = 0; i < 3; i++)
{
switch (i)
{
case 0:
await Test1();
break;
case 1:
await Task.WhenAll(Test1(), Test2());
break;
case 2:
await Task.WhenAll(Test1(), Test2(), Test3());
break;
case 3:
await Task.WhenAll(Test1(), Test2(), Test3());
break;
}
}
}
}
static async Task Test1()
{
int[] elevIds = new int[] { 4440, 4441, 4442 };
IElevation[] elevs = new IElevation[3];
elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));
using (var pdf = await AlumCloudPlans.Manager.RollUpProjectImageAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
{
//using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
}
}
static async Task Test2()
{
int[] elevIds = new int[] { 4430, 4431 };
IElevation[] elevs = new IElevation[2];
elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));
using (var pdf = await AlumCloudPlans.Manager.RollUpProjectImageAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
{
// using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
}
}
static async Task Test3()
{
int[] elevIds = new int[] { 4427 };
IElevation[] elevs = new IElevation[1];
elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));
using (var pdf = await AlumCloudPlans.Manager.RollUpProjectImageAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
{
// using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
}
}