cofoundry-cms / cofoundry

Cofoundry is an extensible and flexible .NET Core CMS & application framework focusing on code first development
https://www.cofoundry.org
MIT License
835 stars 146 forks source link

Late server response. #531

Closed sourabhCofoundry closed 1 year ago

sourabhCofoundry commented 1 year ago

Hello, @HeyJoel HeyJoel I have faced a issue regarding a late server response. I have 102 blog posts with details. I am showing 6-6 posts at once but the server response is taking a long time to fetch data. This is code using Cofoundry.Core; using Cofoundry.Domain; using Cofoundry.Web; using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;

namespace CMS { public class PostListViewComponent : ViewComponent { [Obsolete] private readonly ICustomEntityRepository _customEntityRepository; [Obsolete] private readonly IImageAssetRepository _imageAssetRepository; private readonly IPageResponseDataCache _pageRenderDataCache; private readonly IVisualEditorStateService _visualEditorStateService;

    [Obsolete]
    public PostListViewComponent(
         ICustomEntityRepository customEntityRepository,
         IImageAssetRepository imageAssetRepository,
         IPageResponseDataCache pageRenderDataCache,
         IVisualEditorStateService visualEditorStateService
         )
    {
        _customEntityRepository = customEntityRepository;
        _imageAssetRepository = imageAssetRepository;
        _pageRenderDataCache = pageRenderDataCache;
        _visualEditorStateService = visualEditorStateService;
    }

    [Obsolete]
    public async Task<IViewComponentResult> InvokeAsync()
    {
        var webQuery = new SearchBlogPostsQuery();
        var url = HttpContext.Request.GetEncodedUrl();
        string url_value = url.Substring(url.LastIndexOf("=") + 1);
        var query = new SearchCustomEntityRenderSummariesQuery();
        query.CustomEntityDefinitionCode = BlogPostCustomEntityDefinition.DefinitionCode;
        query.PageNumber = webQuery.PageNumber;
        query.PageSize = 6;
        if (Convert.ToInt32(TempData["pageId"]) == 0)
        {
            if (url_value == url)
            {
                query.PageNumber = 1;
            }
            else if (TempData["pageId"] == null)
            {
                query.PageNumber = Convert.ToInt32(url_value);
            }
        }
        else
        {
            query.PageNumber = Convert.ToInt32(url_value);
        }
        var state = await _visualEditorStateService.GetCurrentAsync();
        query.PublishStatus = state.GetAmbientEntityPublishStatusQuery();
        var entities = await _customEntityRepository.SearchCustomEntityRenderSummariesAsync(query);

        var viewModel = await MapBlogPostsAsync(entities);
        return View(viewModel);
    }

    [Obsolete]
    private async Task<object> MapBlogPostsAsync(PagedQueryResult<CustomEntityRenderSummary> customEntityResult)
    {
        if (HttpContext.Request.Cookies["CategoryName"] != null || HttpContext.Request.Cookies["CategoryId"] != null)
        {
            HttpContext.Response.Cookies.Delete("CategoryId");
            HttpContext.Response.Cookies.Delete("CategoryName");
        }
        var blogPosts = new List<BlogPostSummary>(customEntityResult.Items.Count());
        var imageAssetIds = customEntityResult
            .Items
            .Select(i => (DataModel)i.Model)
            .Select(m => m.ThumbnailImageAssetId)
            .Distinct();
        var images = await _imageAssetRepository.GetImageAssetRenderDetailsByIdRangeAsync(imageAssetIds);
        foreach (var customEntity in customEntityResult.Items)
        {
            var model = (DataModel)customEntity.Model;
            var blogPost = new BlogPostSummary();
            blogPost.Title = customEntity.Title;
            blogPost.ShortDescription = model.Description;
            blogPost.ThumbnailImageAsset = images.GetOrDefault(model.ThumbnailImageAssetId);
            blogPost.FullPath = customEntity.Urls.FirstOrDefault();
            DateTime tempDate = customEntity.Date;
            blogPost.PostDate = tempDate.ToString("MMMM dd, yyyy");
            blogPost.CategoryId = model.CategoryIds.SingleOrDefault();

            blogPosts.Add(Post);
        }
        return customEntityResult.ChangeType(blogPosts);
    }
}

}

HeyJoel commented 1 year ago

As far as I can see there's nothing in particular in the code you pasted that looks wrong from a performance point of view, it's basically two queries, one is pages to 6 items, and the other (images) should be cached in subsequent fetches (assuming latest version, default config etc).

Having said that, DateTime tempDate = customEntity.Date; is weird because CustomEntityRenderSummary doesn't have a date property, so it looks like you may have modified Cofoundry in some way, which I wouldn't be able to help you with. Also you say "6-6" posts at once? If you are running this code multiple times in every request than yes that might start to add up.

Aside from that, there could be plenty of issues outside of this code that I won't know about, e.g. is your db in the same location as your app? Is your db up to spec? What is the load? Have you isolated this code as the bottleneck/is anything else running in the request pipeline? In another issue you mentioned TTI, but TTI isn't just about the initial server response time etc etc