秒表总时间问题

时间:2016-09-16 06:56:55

标签: c# asp.net-mvc timer stopwatch

我有一个项目,我必须向服务器显示查询时间。 正在使用多个秒表:

  

1)对于webservice响应(TechDoctimer)

     

2)对于db响应(dbTimer)

     

3)对于功能性能的总时间(定时器),此定时器是#34;最大的定时器",所有其他定时器都嵌套在其性能中。

因此,我必须获得2个数字(来自db和techdoc秒表),其总和将小于timer,但我得到这样的结果:

  

服务器总响应时间:1.351(秒)

     

TechDoc总响应时间:1.215(秒)

     

数据库价格总响应时间:0.67(秒)

正如您所见,TechDoc +数据库> 服务器总响应时间。 我不明白如果只有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;
    }
}

1 个答案:

答案 0 :(得分:1)

当您将结果保存到字符串时,为什么不直接使用TimeSpan.ToString()输出?