秒表总时间问题
Stopwatch total time issue
我有一个项目,我必须向服务器显示查询时间。
正在使用多个秒表:
1) For webservice response(TechDoctimer)
2) For db response (dbTimer)
3) For total time of function performance (timer) this timer is "the biggest one", all the other timers are nested inside its performance.
因此我必须得到 2 个数字(来自 db 和 techdoc 秒表),它们的总和将小于 timer
,但我得到这样的结果:
Server total response time: 1.351 (seconds)
TechDoc total response time: 1.215 (seconds)
Database prices total response time: 0.67 (seconds)
如您所见,TechDoc+Database > 服务器总响应时间。
我不明白这怎么会发生,如果 Visual Studio 不自己做线程的话。
这是我的函数代码:
public static FullModel GetDetailList(string article)
{
Stopwatch timer = new Stopwatch();
timer.Start();
Stopwatch dbTimer = new Stopwatch();
Stopwatch TechDoctimer = new Stopwatch();
var db = new TecAllianceEntities();
FullModel model = new FullModel();
model.details = new List<DetailModel>();
List<string> listOfNo = new List<string>();
int articleId;
TechDoctimer.Start();
List<int> listOfIdsFound = RequestDetailIdByArticle(article);
TechDoctimer.Stop();
foreach (var item in listOfIdsFound)
{
DetailModel detail = new DetailModel();
detail.oeNumberList = new List<OENumberModel>();
detail.documents = new List<DocumentModel>();
detail.attributeList = new List<AttributeModel>();
articleId = item;
TechDoctimer.Start();
string resultFromRequestDetailById = TecAllianceResponce.RequestDetailByIds(articleId);
TechDoctimer.Stop();
var result = TecAllianceResponce.GetSorted(resultFromRequestDetailById);
for (int i = 0; i < result.Count; i++)
{
switch (result[i])
{
case "articleId":
detail.articleId = Int32.Parse(result[i + 1]);
break;
case "articleName":
detail.articleName = result[i + 1];
break;
case "articleNo":
detail.articleNo = result[i + 1];
listOfNo.Add(result[i + 1]);
break;
case "articleStateName":
detail.articleStateName = result[i + 1];
break;
case "brandName":
if (result[i - 2] == "articleStateName")
detail.brandName = result[i + 1];
break;
case "packingUnit":
detail.packingUnit = Int32.Parse(result[i + 1]);
break;
case "quantityPerPackingUnit":
detail.quantityPerPackingUnit = Int32.Parse(result[i + 1]);
break;
case "docId":
detail.hasDocuments = result[i + 1] != "0" ? true : false;
DocumentModel newDoc = new DocumentModel()
{
docId = Int32.Parse(result[i + 1]),
docFileName = result[i - 1],
docTypeName = result[i + 5]
};
newDoc.docURL = "http://webservicepilot.tecdoc.net/pegasus-3-0/documents/367/" + newDoc.docId + "/" + 0;
detail.documents.Add(newDoc);
break;
case "oeNumber":
detail.hasDocuments = result[i + 1] != "0" ? true : false;
OENumberModel newOE = new OENumberModel()
{
brandName = result[i - 1],
oeNumber = result[i + 1]
};
detail.oeNumberList.Add(newOE);
break;
case "attrName":
AttributeModel newAttr = new AttributeModel()
{
attrName = result[i + 1]
};
for (int j = i; j <= i + 12; j++)
{
if (result[j] == "attrValue") newAttr.attrValue = result[j + 1];
if (result[j] == "attrUnit") newAttr.attrUnit = result[j + 1];
}
if (newAttr.attrUnit == null) newAttr.attrUnit = "";
detail.attributeList.Add(newAttr);
break;
}
}
model.details.Add(detail);
}
dbTimer.Start();
if (listOfNo.Any())
{
var queryPrices = db.PRECES.Where(p => listOfNo.Contains(p.RAZOTAJA_KODI)).Select(p => new { p.RAZOTAJA_KODI, p.REALIZ_CENA }).ToList();
dbTimer.Stop();
foreach (var item in model.details)
{
try
{
item.price = queryPrices.Where(q => q.RAZOTAJA_KODI == item.articleNo).Select(q => q.REALIZ_CENA).First();
}
catch
{
item.price = 0;
}
}
}
else
{
dbTimer.Stop();
}
timer.Stop();
TimeSpan timeTaken = timer.Elapsed;
TimeSpan TechDocUsageTime = TechDoctimer.Elapsed;
TimeSpan dbRequestTime = dbTimer.Elapsed;
model.dbTimeTaken = dbRequestTime.Seconds + "." + dbRequestTime.Milliseconds;
model.TimeTaken = timeTaken.Seconds + "." + timeTaken.Milliseconds;
model.TimeTechDoc = TechDocUsageTime.Seconds + "." + TechDocUsageTime.Milliseconds;
return model;
}
}
看到您将结果保存到字符串中,为什么不直接使用 TimeSpan.ToString() 输出?
我有一个项目,我必须向服务器显示查询时间。 正在使用多个秒表:
1) For webservice response(TechDoctimer)
2) For db response (dbTimer)
3) For total time of function performance (timer) this timer is "the biggest one", all the other timers are nested inside its performance.
因此我必须得到 2 个数字(来自 db 和 techdoc 秒表),它们的总和将小于 timer
,但我得到这样的结果:
Server total response time: 1.351 (seconds)
TechDoc total response time: 1.215 (seconds)
Database prices total response time: 0.67 (seconds)
如您所见,TechDoc+Database > 服务器总响应时间。 我不明白这怎么会发生,如果 Visual Studio 不自己做线程的话。
这是我的函数代码:
public static FullModel GetDetailList(string article)
{
Stopwatch timer = new Stopwatch();
timer.Start();
Stopwatch dbTimer = new Stopwatch();
Stopwatch TechDoctimer = new Stopwatch();
var db = new TecAllianceEntities();
FullModel model = new FullModel();
model.details = new List<DetailModel>();
List<string> listOfNo = new List<string>();
int articleId;
TechDoctimer.Start();
List<int> listOfIdsFound = RequestDetailIdByArticle(article);
TechDoctimer.Stop();
foreach (var item in listOfIdsFound)
{
DetailModel detail = new DetailModel();
detail.oeNumberList = new List<OENumberModel>();
detail.documents = new List<DocumentModel>();
detail.attributeList = new List<AttributeModel>();
articleId = item;
TechDoctimer.Start();
string resultFromRequestDetailById = TecAllianceResponce.RequestDetailByIds(articleId);
TechDoctimer.Stop();
var result = TecAllianceResponce.GetSorted(resultFromRequestDetailById);
for (int i = 0; i < result.Count; i++)
{
switch (result[i])
{
case "articleId":
detail.articleId = Int32.Parse(result[i + 1]);
break;
case "articleName":
detail.articleName = result[i + 1];
break;
case "articleNo":
detail.articleNo = result[i + 1];
listOfNo.Add(result[i + 1]);
break;
case "articleStateName":
detail.articleStateName = result[i + 1];
break;
case "brandName":
if (result[i - 2] == "articleStateName")
detail.brandName = result[i + 1];
break;
case "packingUnit":
detail.packingUnit = Int32.Parse(result[i + 1]);
break;
case "quantityPerPackingUnit":
detail.quantityPerPackingUnit = Int32.Parse(result[i + 1]);
break;
case "docId":
detail.hasDocuments = result[i + 1] != "0" ? true : false;
DocumentModel newDoc = new DocumentModel()
{
docId = Int32.Parse(result[i + 1]),
docFileName = result[i - 1],
docTypeName = result[i + 5]
};
newDoc.docURL = "http://webservicepilot.tecdoc.net/pegasus-3-0/documents/367/" + newDoc.docId + "/" + 0;
detail.documents.Add(newDoc);
break;
case "oeNumber":
detail.hasDocuments = result[i + 1] != "0" ? true : false;
OENumberModel newOE = new OENumberModel()
{
brandName = result[i - 1],
oeNumber = result[i + 1]
};
detail.oeNumberList.Add(newOE);
break;
case "attrName":
AttributeModel newAttr = new AttributeModel()
{
attrName = result[i + 1]
};
for (int j = i; j <= i + 12; j++)
{
if (result[j] == "attrValue") newAttr.attrValue = result[j + 1];
if (result[j] == "attrUnit") newAttr.attrUnit = result[j + 1];
}
if (newAttr.attrUnit == null) newAttr.attrUnit = "";
detail.attributeList.Add(newAttr);
break;
}
}
model.details.Add(detail);
}
dbTimer.Start();
if (listOfNo.Any())
{
var queryPrices = db.PRECES.Where(p => listOfNo.Contains(p.RAZOTAJA_KODI)).Select(p => new { p.RAZOTAJA_KODI, p.REALIZ_CENA }).ToList();
dbTimer.Stop();
foreach (var item in model.details)
{
try
{
item.price = queryPrices.Where(q => q.RAZOTAJA_KODI == item.articleNo).Select(q => q.REALIZ_CENA).First();
}
catch
{
item.price = 0;
}
}
}
else
{
dbTimer.Stop();
}
timer.Stop();
TimeSpan timeTaken = timer.Elapsed;
TimeSpan TechDocUsageTime = TechDoctimer.Elapsed;
TimeSpan dbRequestTime = dbTimer.Elapsed;
model.dbTimeTaken = dbRequestTime.Seconds + "." + dbRequestTime.Milliseconds;
model.TimeTaken = timeTaken.Seconds + "." + timeTaken.Milliseconds;
model.TimeTechDoc = TechDocUsageTime.Seconds + "." + TechDocUsageTime.Milliseconds;
return model;
}
}
看到您将结果保存到字符串中,为什么不直接使用 TimeSpan.ToString() 输出?