Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 90243c9

Browse files
authored
Merge pull request #2131 from github/feature/timer-logger-extensions
Add timer extension methods for ILogger
2 parents c871d0f + 4eca5bb commit 90243c9

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/GitHub.App/ViewModels/Dialog/Clone/RepositorySelectViewModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public async Task Activate()
108108

109109
try
110110
{
111-
var results = await service.ReadViewerRepositories(connection.HostAddress).ConfigureAwait(true);
111+
var results = await log.TimeAsync(nameof(service.ReadViewerRepositories),
112+
() => service.ReadViewerRepositories(connection.HostAddress));
112113

113114
var yourRepositories = results.Repositories
114115
.Where(r => r.Owner == results.Owner)
@@ -121,6 +122,7 @@ public async Task Activate()
121122
.OrderBy(x => x.Key)
122123
.SelectMany(x => x.Value.Select(y => new RepositoryItemViewModel(y, x.Key)));
123124
Items = yourRepositories.Concat(collaboratorRepositories).Concat(orgRepositories).ToList();
125+
log.Information("Read {Total} viewer repositories", Items.Count);
124126
ItemsView = CollectionViewSource.GetDefaultView(Items);
125127
ItemsView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(RepositoryItemViewModel.Group)));
126128
ItemsView.Filter = FilterItem;

src/GitHub.InlineReviews/Services/PullRequestSessionService.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public IReadOnlyList<InlineAnnotationModel> BuildAnnotations(
9797
relativePath = relativePath.Replace("\\", "/");
9898

9999
return pullRequest.CheckSuites
100-
?.SelectMany(checkSuite => checkSuite.CheckRuns.Select(checkRun => new { checkSuite, checkRun}))
100+
?.SelectMany(checkSuite => checkSuite.CheckRuns.Select(checkRun => new { checkSuite, checkRun }))
101101
.SelectMany(arg =>
102102
arg.checkRun.Annotations
103103
.Where(annotation => annotation.Path == relativePath)
@@ -361,17 +361,21 @@ public virtual async Task<PullRequestDetailModel> ReadPullRequestDetail(HostAddr
361361
var result = await connection.Run(readPullRequest, vars);
362362

363363
var apiClient = await apiClientFactory.Create(address);
364-
var files = await apiClient.GetPullRequestFiles(owner, name, number).ToList();
365-
var lastCommitModel = await GetPullRequestLastCommitAdapter(address, owner, name, number);
366364

367-
result.Statuses = (IReadOnlyList<StatusModel>) lastCommitModel.Statuses ?? Array.Empty<StatusModel>();
365+
var files = await log.TimeAsync(nameof(apiClient.GetPullRequestFiles),
366+
async () => await apiClient.GetPullRequestFiles(owner, name, number).ToList());
367+
368+
var lastCommitModel = await log.TimeAsync(nameof(GetPullRequestLastCommitAdapter),
369+
() => GetPullRequestLastCommitAdapter(address, owner, name, number));
370+
371+
result.Statuses = (IReadOnlyList<StatusModel>)lastCommitModel.Statuses ?? Array.Empty<StatusModel>();
368372

369373
if (lastCommitModel.CheckSuites == null)
370374
{
371375
result.CheckSuites = Array.Empty<CheckSuiteModel>();
372376
}
373377
else
374-
{
378+
{
375379
result.CheckSuites = lastCommitModel.CheckSuites;
376380
foreach (var checkSuite in result.CheckSuites)
377381
{
@@ -838,8 +842,8 @@ async Task<LastCommitAdapter> GetPullRequestLastCommitAdapter(HostAddress addres
838842
commit => new LastCommitAdapter
839843
{
840844
Statuses = commit.Commit.Status == null ? null : commit.Commit.Status
841-
.Select(context => context == null
842-
? null
845+
.Select(context => context == null
846+
? null
843847
: context.Contexts
844848
.Select(statusContext => new StatusModel
845849
{
@@ -871,7 +875,7 @@ async Task<LastCommitAdapter> GetPullRequestLastCommitAdapter(HostAddress addres
871875
static void BuildPullRequestThreads(PullRequestDetailModel model)
872876
{
873877
var commentsByReplyId = new Dictionary<string, List<CommentAdapter>>();
874-
878+
875879
// Get all comments that are not replies.
876880
foreach (CommentAdapter comment in model.Reviews.SelectMany(x => x.Comments))
877881
{
@@ -961,5 +965,5 @@ class LastCommitAdapter
961965

962966
public string HeadSha { get; set; }
963967
}
964-
}
968+
}
965969
}

src/GitHub.Logging/Logging/ILoggerExtensions.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Threading.Tasks;
14
using Serilog;
25

36
namespace GitHub.Logging
@@ -25,5 +28,37 @@ public static void Assert(this ILogger logger, bool condition, string messageTem
2528
#pragma warning restore Serilog004
2629
}
2730
}
31+
32+
public static void Time(this ILogger logger, string name, Action method)
33+
{
34+
var startTime = DateTime.Now;
35+
method();
36+
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime));
37+
}
38+
39+
public static T Time<T>(this ILogger logger, string name, Func<T> method)
40+
{
41+
var startTime = DateTime.Now;
42+
var value = method();
43+
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime));
44+
return value;
45+
}
46+
47+
public static async Task TimeAsync(this ILogger logger, string name, Func<Task> methodAsync)
48+
{
49+
var startTime = DateTime.Now;
50+
await methodAsync().ConfigureAwait(false);
51+
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime));
52+
}
53+
54+
public static async Task<T> TimeAsync<T>(this ILogger logger, string name, Func<Task<T>> methodAsync)
55+
{
56+
var startTime = DateTime.Now;
57+
var value = await methodAsync().ConfigureAwait(false);
58+
logger.Verbose("{Name} took {Seconds} seconds", name, FormatSeconds(DateTime.Now - startTime));
59+
return value;
60+
}
61+
62+
static string FormatSeconds(TimeSpan timeSpan) => timeSpan.TotalSeconds.ToString("0.##", CultureInfo.InvariantCulture);
2863
}
29-
}
64+
}

0 commit comments

Comments
 (0)