为什么在Azure上运行的代码如此之慢?

时间:2014-01-21 09:01:23

标签: azure hosting cloud-hosting

我有一个在Azure 共享网站模式下运行的网络应用。一种简单的方法,我将项目添加到列表并对列表进行排序,当列表大小约为300项时,我的计算机上 0.3s ,部署后 10s 天蓝色的机器)。

有人知道为什么Azure太慢了吗?

我做错了吗?我使用默认模式,但用SHARED模式替换了FREE模式,因为我认为这会有所帮助,但似乎没有。

更新

public ActionResult GetPosts(String selectedStreams, int implicitSelectedVisualiserId, int userId)
    {
        DateTime begin = DateTime.UtcNow;

        List<SearchQuery> selectedSearchQueries = searchQueryRepository.GetSearchQueriesOfStreamsIds(selectedStreams == String.Empty ? new List<int>() : selectedStreams.Split(',').Select(n => int.Parse(n)).ToList());
        var implicitSelectedVisualiser = VisualiserModel.ToVisualiserModel(visualiserRepository.GetVisualiser(implicitSelectedVisualiserId));

        var twitterSearchQueryOfImplicitSelectedVisualiser = searchQueryRepository.GetSearchQuery(implicitSelectedVisualiser.Stream.Name, Service.Twitter, userId);
        var instagramSearchQueryOfImplicitSelectedVisualiser = searchQueryRepository.GetSearchQuery(implicitSelectedVisualiser.Stream.Name, Service.Instagram, userId);
        var facebookSearchQueryOfImplicitSelectedVisualiser = searchQueryRepository.GetSearchQuery(implicitSelectedVisualiser.Stream.Name, Service.Facebook, userId);
        var manualSearchQueryOfImplicitSelectedVisualiser = searchQueryRepository.GetSearchQuery(implicitSelectedVisualiser.Stream.Name, Service.Manual, userId);

        List<SearchResultModel> approvedSearchResults = new List<SearchResultModel>();

        if (twitterSearchQueryOfImplicitSelectedVisualiser != null || instagramSearchQueryOfImplicitSelectedVisualiser != null || facebookSearchQueryOfImplicitSelectedVisualiser != null
            || manualSearchQueryOfImplicitSelectedVisualiser != null)
        {
            // Define search text to be displayed during slideshow;
            SearchModel searchModel = new SearchModel();

            // Set slideshow settings from implicit selected visualiser.
            ViewBag.CurrentVisualiser = implicitSelectedVisualiser;

            // Load search results from selected visualisers.
            foreach (SearchQuery searchQuery in selectedSearchQueries)
            {
                approvedSearchResults.AddRange(
                    SearchResultModel.ToSearchResultModel(
                        searchResultRepository.GetSearchResults
                            (searchQuery.Id,
                            implicitSelectedVisualiser.Language)));
                // Add defined query too.
                searchModel.SearchValue += " " + searchQuery.Query;
            }

            // Add defined query for implicit selected visualiser.
            if (twitterSearchQueryOfImplicitSelectedVisualiser != null)
                searchModel.SearchValue += " " + twitterSearchQueryOfImplicitSelectedVisualiser.Query;
            if (instagramSearchQueryOfImplicitSelectedVisualiser != null)
                searchModel.SearchValue += " " + instagramSearchQueryOfImplicitSelectedVisualiser.Query;
            if (facebookSearchQueryOfImplicitSelectedVisualiser != null)
                searchModel.SearchValue += " " + facebookSearchQueryOfImplicitSelectedVisualiser.Query;

            ViewBag.Search = searchModel;

            // Also add search results from implicit selected visualiser
            if (twitterSearchQueryOfImplicitSelectedVisualiser != null)
                approvedSearchResults.AddRange(SearchResultModel.ToSearchResultModel(searchResultRepository.GetSearchResults(twitterSearchQueryOfImplicitSelectedVisualiser.Id, implicitSelectedVisualiser.Language)));
            if (instagramSearchQueryOfImplicitSelectedVisualiser != null)
                approvedSearchResults.AddRange(SearchResultModel.ToSearchResultModel(searchResultRepository.GetSearchResults(instagramSearchQueryOfImplicitSelectedVisualiser.Id, implicitSelectedVisualiser.Language)));
            if (facebookSearchQueryOfImplicitSelectedVisualiser != null)
                approvedSearchResults.AddRange(SearchResultModel.ToSearchResultModel(searchResultRepository.GetSearchResults(facebookSearchQueryOfImplicitSelectedVisualiser.Id, implicitSelectedVisualiser.Language)));
            if (manualSearchQueryOfImplicitSelectedVisualiser != null)
                approvedSearchResults.AddRange(SearchResultModel.ToSearchResultModel(searchResultRepository.GetSearchResults(manualSearchQueryOfImplicitSelectedVisualiser.Id, implicitSelectedVisualiser.Language)));

            // if user selected to show only posts from specific number of last days.
            var approvedSearchResultsFilteredByDays = new List<SearchResultModel>();
            if (implicitSelectedVisualiser.ShowPostsFromLastXDays != 0)
            {
                foreach (SearchResultModel searchResult in approvedSearchResults)
                {
                    var postCreatedTimeWithDays = searchResult.PostCreatedTime.AddDays(implicitSelectedVisualiser.ShowPostsFromLastXDays + 1);
                    if (postCreatedTimeWithDays >= DateTime.Now)
                        approvedSearchResultsFilteredByDays.Add(searchResult);
                }
            }
            else
            {
                approvedSearchResultsFilteredByDays = approvedSearchResults;
            }

            // Order search results (posts to be displayed by created datetime).
            var approvedSearchResultsOrdered = new List<SearchResultModel>();
            if (implicitSelectedVisualiser.PostsSortOrder == PostsSortOrder.CREATED_DATE_ASC)
            {
                approvedSearchResultsOrdered = approvedSearchResultsFilteredByDays.OrderBy(s => s.PostCreatedTime).ToList(); ;
            }
            else if (implicitSelectedVisualiser.PostsSortOrder == PostsSortOrder.CREATED_DATE_DESC)
            {
                approvedSearchResultsOrdered = approvedSearchResultsFilteredByDays.OrderByDescending(s => s.PostCreatedTime).ToList(); ;
            }
            else if (implicitSelectedVisualiser.PostsSortOrder == PostsSortOrder.RANDOM)
            {
                var rnd = new Random();
                approvedSearchResultsOrdered = approvedSearchResultsFilteredByDays.OrderBy(x => rnd.Next()).ToList();
            }

            // Load background images;
            var visualiserImages = visualiserImageRepository.GetImages(implicitSelectedVisualiser.Id);

            //foreach (SearchResultModel searchResultModel in approvedSearchResultsOrdered)
            //{
            //    searchResultModel.BackgroundImagePath = TwitterUtils.GetRandomImageBackgroundForDisplay(visualiserImages);
            //}

            ViewBag.BackgroundImagePath = TwitterUtils.GetRandomImageBackgroundForDisplay(visualiserImages);

            approvedSearchResults = approvedSearchResultsOrdered;
        }

        DateTime end = DateTime.UtcNow;

        Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception(String.Format("User {0}: Preparing {1} posts for visualiser took {2} seconds", MySession.Current.LoggedInUserName, approvedSearchResults.Count(), (end - begin).TotalMilliseconds / 1000)));

        return PartialView("_DisplayPostsNew", approvedSearchResults);
    }

2 个答案:

答案 0 :(得分:3)

实际上这并不奇怪。 Windows Azure中使用的服务器目前主要是1.6 GHz计算机。您使用的大型机器使用的核心越多,但它们的速度都相同。这可能是比您使用的开发机器慢得多的CPU。

在Windows Azure网站上,当您转到共享模式时,您仍然处于多租户环境中,因此可以在这里看到一些嘈杂的邻居。免费和共享之间的区别在于,自付款以来,许多免费配额都被删除了。当您转移到Standard时,您将被分配一个专用于您的网站的虚拟机(最多100个),因此这是最好的情况,因为您是唯一一个在此时使用资源的人。

前一段时间MSDN论坛上有一个帖子:http://social.msdn.microsoft.com/Forums/windowsazure/en-US/0d0a3a88-eac4-4b9e-8b10-4a547cbf653b/performance-of-azure-servers-slow-cpus?forum=windowsazuredevelopment

他们已经开始为虚拟机和云服务等提供不同的硬件配置和更多内存,但我不确定CPU是否已经更改。很难找到在WindowsAzure.com上声明的CPU,但是在网站的定价计算器上,当您将滑块移动到标准时,它会引用1.6Ghz机器。

答案 1 :(得分:2)

其实我发现了这个问题。

在本地,我在我的数据库中测试了几百条记录,而在Azure DB中,我在该表中有超过70 000条记录影响了算法的性能......

我在上面的代码中犯了一个错误:我已经完成了特定日期后从DB中过滤了记录。通过直接在Linq中过滤,我在Azure中也将性能从10s提高到0.3s。