Performance tweaks.

This commit is contained in:
Steven Hildreth 2018-12-05 10:04:21 -06:00
parent c97281b05b
commit 49cfe0dc8c
6 changed files with 124 additions and 69 deletions

View file

@ -1,6 +1,7 @@
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Roadie.Library; using Roadie.Library;
using Roadie.Library.Caching; using Roadie.Library.Caching;
using Roadie.Library.Configuration; using Roadie.Library.Configuration;
@ -50,22 +51,36 @@ namespace Roadie.Api.Services
public async Task<OperationResult<Artist>> ById(User roadieUser, Guid id, IEnumerable<string> includes) public async Task<OperationResult<Artist>> ById(User roadieUser, Guid id, IEnumerable<string> includes)
{ {
var timings = new Dictionary<string, long>();
var tsw = new Stopwatch();
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
sw.Start(); sw.Start();
var cacheKey = string.Format("urn:artist_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes)); var cacheKey = string.Format("urn:artist_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes));
var result = await this.CacheManager.GetAsync<OperationResult<Artist>>(cacheKey, async () => var result = await this.CacheManager.GetAsync<OperationResult<Artist>>(cacheKey, async () =>
{ {
return await this.ArtistByIdAction(id, includes); tsw.Restart();
var rr = await this.ArtistByIdAction(id, includes);
tsw.Stop();
timings.Add("ArtistByIdAction", tsw.ElapsedMilliseconds);
return rr;
}, data.Artist.CacheRegionUrn(id)); }, data.Artist.CacheRegionUrn(id));
if (result?.Data != null && roadieUser != null) if (result?.Data != null && roadieUser != null)
{ {
tsw.Restart();
var artist = this.GetArtist(id); var artist = this.GetArtist(id);
tsw.Stop();
timings.Add("GetArtist", tsw.ElapsedMilliseconds);
tsw.Restart();
var userBookmarkResult = await this.BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Artist); var userBookmarkResult = await this.BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Artist);
if(userBookmarkResult.IsSuccess) if (userBookmarkResult.IsSuccess)
{ {
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Value == artist.RoadieId.ToString()) != null; result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Value == artist.RoadieId.ToString()) != null;
} }
tsw.Stop();
timings.Add("userBookmarkResult", tsw.ElapsedMilliseconds);
tsw.Restart();
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == roadieUser.Id); var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == roadieUser.Id);
if (userArtist != null) if (userArtist != null)
{ {
@ -76,8 +91,12 @@ namespace Roadie.Api.Services
Rating = userArtist.Rating Rating = userArtist.Rating
}; };
} }
tsw.Stop();
timings.Add("userArtist", tsw.ElapsedMilliseconds);
} }
sw.Stop(); sw.Stop();
timings.Add("operation", sw.ElapsedMilliseconds);
this.Logger.LogDebug("ById Timings: id [{0}], includes [{1}], timings [{3}]", id, includes, JsonConvert.SerializeObject(timings));
return new OperationResult<Artist>(result.Messages) return new OperationResult<Artist>(result.Messages)
{ {
Data = result?.Data, Data = result?.Data,
@ -90,19 +109,32 @@ namespace Roadie.Api.Services
private async Task<OperationResult<Artist>> ArtistByIdAction(Guid id, IEnumerable<string> includes) private async Task<OperationResult<Artist>> ArtistByIdAction(Guid id, IEnumerable<string> includes)
{ {
var timings = new Dictionary<string, long>();
var tsw = new Stopwatch();
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
sw.Start(); sw.Start();
tsw.Restart();
var artist = this.GetArtist(id); var artist = this.GetArtist(id);
tsw.Stop();
timings.Add("getArtist", tsw.ElapsedMilliseconds);
if (artist == null) if (artist == null)
{ {
return new OperationResult<Artist>(true, string.Format("Artist Not Found [{0}]", id)); return new OperationResult<Artist>(true, string.Format("Artist Not Found [{0}]", id));
} }
tsw.Restart();
var result = artist.Adapt<Artist>(); var result = artist.Adapt<Artist>();
tsw.Stop();
timings.Add("adaptArtist", tsw.ElapsedMilliseconds);
result.Thumbnail = base.MakeArtistThumbnailImage(id); result.Thumbnail = base.MakeArtistThumbnailImage(id);
result.MediumThumbnail = base.MakeThumbnailImage(id, "artist", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height); result.MediumThumbnail = base.MakeThumbnailImage(id, "artist", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
tsw.Restart();
result.Genres = artist.Genres.Select(x => new DataToken { Text = x.Genre.Name, Value = x.Genre.RoadieId.ToString() }); result.Genres = artist.Genres.Select(x => new DataToken { Text = x.Genre.Name, Value = x.Genre.RoadieId.ToString() });
tsw.Stop();
timings.Add("genres", tsw.ElapsedMilliseconds);
if (includes != null && includes.Any()) if (includes != null && includes.Any())
{ {
if (includes.Contains("releases")) if (includes.Contains("releases"))
@ -147,9 +179,10 @@ namespace Roadie.Api.Services
} }
result.Releases = dtoReleases; result.Releases = dtoReleases;
} }
if (includes.Contains("stats")) if (includes.Contains("stats"))
{ {
tsw.Restart();
var artistTracks = (from r in this.DbContext.Releases var artistTracks = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId
@ -178,13 +211,19 @@ namespace Roadie.Api.Services
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
select ut.PlayedCount).Sum() ?? 0 select ut.PlayedCount).Sum() ?? 0
}; };
tsw.Stop();
timings.Add("stats", tsw.ElapsedMilliseconds);
} }
if (includes.Contains("images")) if (includes.Contains("images"))
{ {
tsw.Restart();
result.Images = this.DbContext.Images.Where(x => x.ArtistId == artist.Id).Select(x => MakeFullsizeImage(x.RoadieId, x.Caption)).ToArray(); result.Images = this.DbContext.Images.Where(x => x.ArtistId == artist.Id).Select(x => MakeFullsizeImage(x.RoadieId, x.Caption)).ToArray();
tsw.Stop();
timings.Add("images", tsw.ElapsedMilliseconds);
} }
if (includes.Contains("associatedartists")) if (includes.Contains("associatedartists"))
{ {
tsw.Restart();
var associatedWithArtists = (from aa in this.DbContext.ArtistAssociations var associatedWithArtists = (from aa in this.DbContext.ArtistAssociations
join a in this.DbContext.Artists on aa.AssociatedArtistId equals a.Id join a in this.DbContext.Artists on aa.AssociatedArtistId equals a.Id
where aa.ArtistId == artist.Id where aa.ArtistId == artist.Id
@ -232,10 +271,13 @@ namespace Roadie.Api.Services
}).ToArray(); }).ToArray();
result.AssociatedArtists = associatedArtists.Union(associatedWithArtists).OrderBy(x => x.SortName); result.AssociatedArtists = associatedArtists.Union(associatedWithArtists).OrderBy(x => x.SortName);
tsw.Stop();
timings.Add("associatedartists", tsw.ElapsedMilliseconds);
} }
if (includes.Contains("collections")) if (includes.Contains("collections"))
{ {
tsw.Restart();
var collectionPagedRequest = new PagedRequest var collectionPagedRequest = new PagedRequest
{ {
Limit = 100 Limit = 100
@ -246,9 +288,12 @@ namespace Roadie.Api.Services
{ {
result.CollectionsWithArtistReleases = r.Rows.ToArray(); result.CollectionsWithArtistReleases = r.Rows.ToArray();
} }
tsw.Stop();
timings.Add("collections", tsw.ElapsedMilliseconds);
} }
if (includes.Contains("playlists")) if (includes.Contains("playlists"))
{ {
tsw.Restart();
var pg = new PagedRequest var pg = new PagedRequest
{ {
FilterToArtistId = artist.RoadieId FilterToArtistId = artist.RoadieId
@ -258,9 +303,12 @@ namespace Roadie.Api.Services
{ {
result.PlaylistsWithArtistReleases = r.Rows.ToArray(); result.PlaylistsWithArtistReleases = r.Rows.ToArray();
} }
tsw.Stop();
timings.Add("playlists", tsw.ElapsedMilliseconds);
} }
if (includes.Contains("contributions")) if (includes.Contains("contributions"))
{ {
tsw.Restart();
result.ArtistContributionReleases = (from t in this.DbContext.Tracks result.ArtistContributionReleases = (from t in this.DbContext.Tracks
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join r in this.DbContext.Releases.Include(x => x.Artist) on rm.ReleaseId equals r.Id join r in this.DbContext.Releases.Include(x => x.Artist) on rm.ReleaseId equals r.Id
@ -271,6 +319,7 @@ namespace Roadie.Api.Services
.Select(rr => rr.First()) .Select(rr => rr.First())
.Select(r => new ReleaseList .Select(r => new ReleaseList
{ {
Id = r.RoadieId,
Release = new DataToken Release = new DataToken
{ {
Text = r.Title, Text = r.Title,
@ -295,9 +344,12 @@ namespace Roadie.Api.Services
Thumbnail = MakeReleaseThumbnailImage(r.RoadieId) Thumbnail = MakeReleaseThumbnailImage(r.RoadieId)
}).ToArray().OrderBy(x => x.Release.Text).ToArray(); }).ToArray().OrderBy(x => x.Release.Text).ToArray();
result.ArtistContributionReleases = result.ArtistContributionReleases.Any() ? result.ArtistContributionReleases : null; result.ArtistContributionReleases = result.ArtistContributionReleases.Any() ? result.ArtistContributionReleases : null;
tsw.Stop();
timings.Add("contributions", tsw.ElapsedMilliseconds);
} }
if (includes.Contains("labels")) if (includes.Contains("labels"))
{ {
tsw.Restart();
result.ArtistLabels = (from l in this.DbContext.Labels result.ArtistLabels = (from l in this.DbContext.Labels
join rl in this.DbContext.ReleaseLabels on l.Id equals rl.LabelId join rl in this.DbContext.ReleaseLabels on l.Id equals rl.LabelId
join r in this.DbContext.Releases on rl.ReleaseId equals r.Id join r in this.DbContext.Releases on rl.ReleaseId equals r.Id
@ -320,9 +372,14 @@ namespace Roadie.Api.Services
Thumbnail = MakeLabelThumbnailImage(l.RoadieId) Thumbnail = MakeLabelThumbnailImage(l.RoadieId)
}).ToArray().GroupBy(x => x.Label.Value).Select(x => x.First()).OrderBy(x => x.SortName).ThenBy(x => x.Label.Text).ToArray(); }).ToArray().GroupBy(x => x.Label.Value).Select(x => x.First()).OrderBy(x => x.SortName).ThenBy(x => x.Label.Text).ToArray();
result.ArtistLabels = result.ArtistLabels.Any() ? result.ArtistLabels : null; result.ArtistLabels = result.ArtistLabels.Any() ? result.ArtistLabels : null;
tsw.Stop();
timings.Add("labels", tsw.ElapsedMilliseconds);
} }
} }
sw.Stop(); sw.Stop();
timings.Add("operation", sw.ElapsedMilliseconds);
this.Logger.LogDebug("ArtistByIdAction Timings: id [{0}], includes [{1}], timings [{3}]", id, includes, JsonConvert.SerializeObject(timings));
return new OperationResult<Artist> return new OperationResult<Artist>
{ {
Data = result, Data = result,

View file

@ -61,90 +61,77 @@ namespace Roadie.Api.Services
var rowCount = result.Count(); var rowCount = result.Count();
BookmarkList[] rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray(); BookmarkList[] rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
var datas = (from b in rows
join a in this.DbContext.Artists on b.BookmarkTargetId equals a.Id into aa
from a in aa.DefaultIfEmpty()
join r in this.DbContext.Releases on b.BookmarkTargetId equals r.Id into rr
from r in rr.DefaultIfEmpty()
join t in this.DbContext.Tracks on b.BookmarkTargetId equals t.Id into tt
from t in tt.DefaultIfEmpty()
join p in this.DbContext.Playlists on b.BookmarkTargetId equals p.Id into pp
from p in pp.DefaultIfEmpty()
join c in this.DbContext.Collections on b.BookmarkTargetId equals c.Id into cc
from c in cc.DefaultIfEmpty()
join l in this.DbContext.Labels on b.BookmarkTargetId equals l.Id into ll
from l in ll.DefaultIfEmpty()
select new
{ b, a, r, t, p, c, l });
foreach (var row in rows) foreach (var row in rows)
{ {
var d = datas.FirstOrDefault(x => x.b.DatabaseId == row.DatabaseId);
switch (row.Type) switch (row.Type)
{ {
case BookmarkType.Artist: case BookmarkType.Artist:
var artist = this.DbContext.Artists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken row.Bookmark = new DataToken
{ {
Text = d.a.Name, Text = artist.Name,
Value = d.a.RoadieId.ToString() Value = artist.RoadieId.ToString()
}; };
row.Thumbnail = this.MakeArtistThumbnailImage(d.a.RoadieId); row.Thumbnail = this.MakeArtistThumbnailImage(artist.RoadieId);
row.SortName = d.a.SortName ?? d.a.Name; row.SortName = artist.SortName ?? artist.Name;
break; break;
case BookmarkType.Release: case BookmarkType.Release:
var release = this.DbContext.Releases.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken row.Bookmark = new DataToken
{ {
Text = d.r.Title, Text = release.Title,
Value = d.r.RoadieId.ToString() Value = release.RoadieId.ToString()
}; };
row.Thumbnail = this.MakeReleaseThumbnailImage(d.r.RoadieId); row.Thumbnail = this.MakeReleaseThumbnailImage(release.RoadieId);
row.SortName = d.r.Title; row.SortName = release.Title;
break; break;
case BookmarkType.Track: case BookmarkType.Track:
var track = this.DbContext.Tracks.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken row.Bookmark = new DataToken
{ {
Text = d.t.Title, Text = track.Title,
Value = d.t.RoadieId.ToString() Value = track.RoadieId.ToString()
}; };
row.Thumbnail = this.MakeTrackThumbnailImage(d.t.RoadieId); row.Thumbnail = this.MakeTrackThumbnailImage(track.RoadieId);
row.SortName = d.t.Title; row.SortName = track.Title;
break; break;
case BookmarkType.Playlist: case BookmarkType.Playlist:
var playlist = this.DbContext.Playlists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken row.Bookmark = new DataToken
{ {
Text = d.p.Name, Text = playlist.Name,
Value = d.p.RoadieId.ToString() Value = playlist.RoadieId.ToString()
}; };
row.Thumbnail = this.MakePlaylistThumbnailImage(d.p.RoadieId); row.Thumbnail = this.MakePlaylistThumbnailImage(playlist.RoadieId);
row.SortName = d.p.Name; row.SortName = playlist.Name;
break; break;
case BookmarkType.Collection: case BookmarkType.Collection:
var collection = this.DbContext.Collections.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken row.Bookmark = new DataToken
{ {
Text = d.c.Name, Text = collection.Name,
Value = d.c.RoadieId.ToString() Value = collection.RoadieId.ToString()
}; };
row.Thumbnail = this.MakeCollectionThumbnailImage(d.c.RoadieId); row.Thumbnail = this.MakeCollectionThumbnailImage(collection.RoadieId);
row.SortName = d.c.SortName ?? d.c.Name; row.SortName = collection.SortName ?? collection.Name;
break; break;
case BookmarkType.Label: case BookmarkType.Label:
var label = this.DbContext.Labels.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken row.Bookmark = new DataToken
{ {
Text = d.l.Name, Text = label.Name,
Value = d.l.RoadieId.ToString() Value = label.RoadieId.ToString()
}; };
row.Thumbnail = this.MakeLabelThumbnailImage(d.l.RoadieId); row.Thumbnail = this.MakeLabelThumbnailImage(label.RoadieId);
row.SortName = d.l.SortName ?? d.l.Name; row.SortName = label.SortName ?? label.Name;
break; break;
} }
}; };
sw.Stop();
sw.Stop(); sw.Stop();
return new Library.Models.Pagination.PagedResult<BookmarkList> return new Library.Models.Pagination.PagedResult<BookmarkList>
{ {

View file

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Roadie.Library.Caching; using Roadie.Library.Caching;
using Roadie.Library.Configuration; using Roadie.Library.Configuration;
using Roadie.Library.Encoding; using Roadie.Library.Encoding;
@ -34,13 +35,33 @@ namespace Roadie.Api.Services
{ {
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
IQueryable<data.Collection> collections = null;
if (!string.IsNullOrEmpty(request.Sort)) if(artistId.HasValue)
{ {
request.Sort = request.Sort.Replace("createdDate", "createdDateTime"); var sql = @"select DISTINCT c.*
request.Sort = request.Sort.Replace("lastUpdated", "lastUpdatedDateTime"); from `collectionrelease` cr
join `collection` c on c.id = cr.collectionId
join `release` r on r.id = cr.releaseId
join `artist` a on r.artistId = a.id
where a.roadieId = {0}";
collections = this.DbContext.Collections.FromSql(sql, artistId);
} }
var result = (from c in this.DbContext.Collections else if(releaseId.HasValue)
{
var sql = @"select DISTINCT c.*
from `collectionrelease` cr
join `collection` c on c.id = cr.collectionId
join `release` r on r.id = cr.releaseId
where r.roadieId = {0}";
collections = this.DbContext.Collections.FromSql(sql, releaseId);
}
else
{
collections = this.DbContext.Collections;
}
var result = (from c in collections
where (request.FilterValue.Length == 0 || (request.FilterValue.Length > 0 && c.Name.Contains(request.Filter))) where (request.FilterValue.Length == 0 || (request.FilterValue.Length > 0 && c.Name.Contains(request.Filter)))
select new CollectionList select new CollectionList
{ {
@ -60,19 +81,6 @@ namespace Roadie.Api.Services
LastUpdated = c.LastUpdated, LastUpdated = c.LastUpdated,
Thumbnail = MakeCollectionThumbnailImage(c.RoadieId) Thumbnail = MakeCollectionThumbnailImage(c.RoadieId)
}); });
if (artistId.HasValue || releaseId.HasValue)
{
result = (from re in result
join cr in this.DbContext.CollectionReleases on re.DatabaseId equals cr.CollectionId into crs
from cr in crs.DefaultIfEmpty()
join r in this.DbContext.Releases on cr.ReleaseId equals r.Id into rs
from r in rs.DefaultIfEmpty()
join a in this.DbContext.Artists on r.ArtistId equals a.Id into aas
from a in aas.DefaultIfEmpty()
where (releaseId == null || r.RoadieId == releaseId)
where (artistId == null || a.RoadieId == artistId)
select re).GroupBy(x => x.DatabaseId).Select(x => x.First());
}
var sortBy = string.IsNullOrEmpty(request.Sort) ? request.OrderValue(new Dictionary<string, string> { { "Collection.Text", "ASC" } }) : request.OrderValue(null); var sortBy = string.IsNullOrEmpty(request.Sort) ? request.OrderValue(new Dictionary<string, string> { { "Collection.Text", "ASC" } }) : request.OrderValue(null);
var rowCount = result.Count(); var rowCount = result.Count();
var rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray(); var rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();

View file

@ -9,10 +9,11 @@
"Serilog": { "Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.RollingFileAlternate" ], "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.RollingFileAlternate" ],
"MinimumLevel": { "MinimumLevel": {
"Default": "Information", "Default": "Verbose",
"Override": { "Override": {
"Microsoft": "Information", "System": "Warning",
"System": "Warning" "Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
} }
}, },
"WriteTo": [ "WriteTo": [

View file

@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Internal;
using Roadie.Library.Identity; using Roadie.Library.Identity;
using Roadie.Library.Models.Releases;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;

View file

@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Roadie.Library.Enums; using Roadie.Library.Enums;
using Roadie.Library.Identity; using Roadie.Library.Identity;
using Roadie.Library.Models.Releases;
using System; using System;
namespace Roadie.Library.Data namespace Roadie.Library.Data