Skip to content

Commit

Permalink
Add BuildJobResult.PerformanceDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Oct 12, 2020
1 parent c287cc8 commit 107cfef
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Imageflow/Fluent/BuildJobResult.cs
Expand Up @@ -15,6 +15,11 @@ public class BuildJobResult
/// A collection of the encoded images produced by the job
/// </summary>
public IReadOnlyCollection<BuildEncodeResult> EncodeResults { get; private set; }

/// <summary>
/// Details about the runtime performance of the job
/// </summary>
public PerformanceDetails PerformanceDetails { get; private set; }

/// <summary>
/// The first encoded image produced by the job (with the lowest io_id)
Expand Down Expand Up @@ -50,9 +55,11 @@ internal static BuildJobResult From(IJsonResponseProvider response, Dictionary<i
}).OrderBy(er => er.IoId).ToList();

var dict = encodeResults.ToDictionary(r => r.IoId);


var perfDetails = new PerformanceDetails((v.data.job_result ?? v.data.build_result).performance);

// There may be fewer reported outputs than registered ones - encoding is conditional on input, I think
return new BuildJobResult {EncodeResults = encodeResults, _results = dict};
return new BuildJobResult {EncodeResults = encodeResults, _results = dict, PerformanceDetails = perfDetails};
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/Imageflow/Fluent/PerformanceDetails.cs
@@ -0,0 +1,42 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Imageflow.Fluent
{
public class PerformanceDetails
{
internal PerformanceDetails(dynamic perf)
{
foreach (var f in perf.frames)
{
frames.Add(new PerformanceDetailsFrame(f));
}
}
private List<PerformanceDetailsFrame> frames = new List<PerformanceDetailsFrame>();
public ICollection<PerformanceDetailsFrame> Frames => frames;

public string GetFirstFrameSummary()
{
var sb = new StringBuilder();
if (Frames.Count > 1)
{
sb.Append($"First of {Frames.Count} frames: ");
}else if (Frames.Count == 0)
{
sb.Append("No frames found");
}

foreach (var n in Frames.First().Nodes)
{
sb.Append(n.Name);
sb.Append("(");
sb.Append((n.WallMicroseconds / 1000.0).ToString("0.####"));
sb.Append("ms) ");
}

return sb.ToString();
}
}
}
23 changes: 23 additions & 0 deletions src/Imageflow/Fluent/PerformanceDetailsFrame.cs
@@ -0,0 +1,23 @@
using System.Collections.Generic;

namespace Imageflow.Fluent
{
public class PerformanceDetailsFrame
{
internal PerformanceDetailsFrame(dynamic frame)
{
foreach (var n in frame.nodes)
{
nodes.Add(new PerformanceDetailsNode()
{
Name = n.name,
WallMicroseconds = n.wall_microseconds
});
}
}

private List<PerformanceDetailsNode> nodes = new List<PerformanceDetailsNode>();

public ICollection<PerformanceDetailsNode> Nodes => nodes;
}
}
9 changes: 9 additions & 0 deletions src/Imageflow/Fluent/PerformanceDetailsNode.cs
@@ -0,0 +1,9 @@
namespace Imageflow.Fluent
{
public struct PerformanceDetailsNode
{

public string Name { get; internal set; }
public long WallMicroseconds { get; internal set; }
}
}
1 change: 1 addition & 0 deletions tests/Imageflow.Test/TestApi.cs
Expand Up @@ -220,6 +220,7 @@ public async Task TestBuildCommandString()
"width=3&height=2&mode=stretch&scale=both&format=webp")
.Finish().InProcessAsync();

Assert.NotEmpty(r.PerformanceDetails.GetFirstFrameSummary());
Assert.Equal(3, r.First.Width);
Assert.Equal("webp", r.First.PreferredExtension);
Assert.True(r.First.TryGetBytes().HasValue);
Expand Down

0 comments on commit 107cfef

Please sign in to comment.