【问题标题】:Stopwatch total time issue秒表总时间问题
【发布时间】:2016-09-16 15:43:54
【问题描述】:

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

1) 用于webservice响应(TechDoctimer)

2) 用于数据库响应(dbTimer)

3) 对于函数执行的总时间(计时器),此计时器是“最大的”,所有其他计时器都嵌套在其性能中。

因此,我必须得到 2 个数字(来自 db 和 techdoc 秒表),它们的总和会小于 timer,但我得到的是这样的:

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

TechDoc 总响应时间:1.215(秒)

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

如您所见,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;
    }
}

【问题讨论】:

  • 尝试使用'model.dbTimeTaken = dbRequestTime.TotalSeconds + "." + dbRequestTime.Milliseconds'。属性 Second 仅包含当前 Minute 的秒数。因此,如果时间超过一分钟,您将得到错误的结果。
  • 正是我的想法。只需使用 TimeSpan 的 TotalSeconds 属性,而不是结合 Seconds 和 Milliseconds。另请查看文档:msdn.microsoft.com/en-us/library/…
  • 不到 5 秒,通常大约 2-3 秒。
  • 既然您将结果保存到字符串中,为什么不直接使用 TimeSpan.ToString() 输出?
  • 谢谢,它解决了我的问题!你能把它写成答案,以便我标记它。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2021-05-17
    相关资源
    最近更新 更多