开发者

varying response times on jquery ajax request

I am making an ajax call to asp.net mvc controller that post data and returns json.

This works correctly however i'm receiving the following response times. They are not the same every time but this give a ball park indication of whats going on.

  • 1st time controller is requested = 2 secs
  • 2nd time controller is requested = 4 secs
  • 3rd time controller is requested = 10 secs
  • 4th time controller is requested = 30 secs
  • 5th time controller is requested = 60 secs

  • I currently not using the mvc 3 async feature or threading.

  • I'm using nhibernate

Controller

    [Authorize]
    [HttpPost]
    public JsonResult Index([DefaultValue(0)]int id, UserSession userSession)
    {
        try
        {
            var productId = int.Parse(Request.QueryString["productId"]);
            var company = _companyRepository.Get(userSession.CompanyId);
            var previousProductTotal = decimal.Parse(Request.Form[string.Format("collectivetotalprice-{0}", productId)]);
            var auditOutput = new StringBuilder();
            var campaign = _campaignRepository.Get(id);
            campaign.SubTotal = (campaign.SubTotal - previousProductTotal);
            var product = _productRepository.Get(productId);
            var productCampaigns =
                _productCampaignRepository.FindByCompanyIdAndCampaignIdAndProductId(company.Id,
                                                                                    campaign.Id, productId);



            var totalQuantity = 0;
            foreach (var campaignRetailer in campaign.CampaignRetailers)
            {
                var retailerName = campaignRetailer.Retailer.Name.Replace(" ", "").ToLower();
                var inputRetailerOrder = string.Format("input-retailerorder-{0}", campaignRetailer.Retailer.Id);
                var orderId = int.Parse(Request.Form[inputRetailerOrder]);
                var order = _orderRepository.Get(orderId);

                foreach (var productCampaign in productCampaigns)
                {
                    var inputName = string.Format("input-{0}-{1}", retailerName, productCampaign.Product.Id);
                    var input = Request.Form[inputName];
                    var quantity = StringHelper.IntParse(input);
                    if (quantity > 0) totalQuantity += quantity;
                    int previousQuantity = 0;
                    var orderItem = _orderItemRepository.GetByCompanyIdAndCampaignIdAndRetailerId(company.Id, campaign.Id, campaignRetailer.Retailer.Id, productCampaign.Product.Id);
                    if (orderItem == null)
                    {
                        orderItem = OrderHelper.CreateCampaignOrderItem(order, productCampaign.Product, company, 
                            campaign, campaignRetailer.Retailer, quantity);

                        order.AddOrderItem(orderItem);
                        _orderRepository.Update(order);
                    }
                    else
                    {
                        previousQuantity = orderItem.Quantity;
                        orderItem.Quantity = quantity;
                        _orderItemRepository.Update(orderItem);
                    }

                    AuditHelper.UpdatedOrderItems(orderItem.Quantity, previousQuantity, orderItem.Product.Name,
                                                  retailerName, auditOutput);

                }
            }

            var priceBreak = PriceBreakHelper.Choose(totalQuantity, product);
            var response = new CampaignOrderResponse();
            var productTotal = priceBreak.Price*totalQuantity;
            campaign.SubTotal += productTotal;
            _campaignRepository.Update(campaign);

            var auditMessage = (!string.IsNullOrEmpty(auditOutput.ToString()))
                                   ? string.Format("<table class='pc-100'>{0}</table>", auditOutput.ToString())
                                   : auditOutput.ToString();
            var userProfile = _userProfileRepository.Get(userSession.UserName);
            UpdateAudit(company, campaign, userProfile, auditMessage);

            response.CollectiveQuantity = totalQuantity.ToString();
            response.CollectivePrice = StringHelper.Currency(priceBreak.Price);
            response.CollectivePriceFormatted = priceBreak.Price.ToString();
            response.CollectiveTotalPrice = productTotal.ToString();
            response.CollectiveTotalPriceFormatted = StringHelper.Currency(productTotal);
            response.Production = StringHelper.Currency(campaign.SubTotal);
            response.TotalProduction = StringHelper.Currency(campaign.TotalProduction);
            response.Delivery = StringHelper.Currency(campaign.Delivery);
            response.Variant = StringHelper.Currency(campaign.Variant);
            response.Status = "Updated";
            return this.Json(response, JsonRequestBehavior.AllowGet);

        }
        catch (Exception ex)
        {
            var response = new CampaignOrderResponse();
            response.Status = "UpdateProduct: " + ex.Message;
            return this.Json(response, JsonRequestBehavior.AllowGet); ;
        }
    }

$.ajax({ type: "POST", url: quickResponseUrl, data: $("form").serialize(), success: function (data) { if (data.Status == "Updated") { $(currentButton).html(complete); var collectquantity = priceElement + " #collectivequantity-" + productId; var collectiveprice = priceElement + " #collectiveprice-" + productId; var collectivepriceformatted = priceElement + " #collectivepriceformatted-" + productId; var collectivetotalpriceformatted = priceElement + " #collectivetotalpriceformatted-" + productId; var collectivetotalprice = priceElement + " #collectivetotalprice-" + productId; //alert(collectivetotalprice); $(collectquantity).html(data.CollectiveQuantity); $(collectiveprice).html(data.CollectivePrice); $(collectivepriceformatted).html(data.CollectivePriceFormatted); $(collectivetotalpriceformatted).html(data.CollectiveTotalPriceFormatted); $(collectivetotalprice).val(data.CollectiveTotalPrice);

$("#campaign-totalproduction").html(data.TotalProduction); $("#campaign-delivery").html(data.Delivery); $("#campaign-variant").html(data.Variant); $("#campaign-production").html(data.Production); UpdateRetailers(); console.log(new Date() + ": First Request :" + data.Status); } else { $(".wrapper-matrixStatus").html("<span class='error campaign-status'>Error with first submission " + data.Status + "</span>").show(); $(currentButton).html(complete);开发者_如何学运维 } }, error: function (data) { $(".wrapper-matrixStatus").html("<span class='error campaign-status'>Error with first submission " + data.Status + "</span>").show(); $(currentButton).html(complete); } });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜