-
Notifications
You must be signed in to change notification settings - Fork 764
Expand file tree
/
Copy pathSpeedScopeStackSourceWriter.cs
More file actions
112 lines (91 loc) · 4.8 KB
/
SpeedScopeStackSourceWriter.cs
File metadata and controls
112 lines (91 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using static Microsoft.Diagnostics.Tracing.Stacks.StackSourceWriterHelper;
namespace Microsoft.Diagnostics.Tracing.Stacks.Formats
{
public static class SpeedScopeStackSourceWriter
{
/// <summary>
/// exports provided StackSource to a https://www.speedscope.app/ format
/// schema: https://www.speedscope.app/file-format-schema.json
/// </summary>
public static void WriteStackViewAsJson(StackSource source, string filePath)
{
if (File.Exists(filePath))
File.Delete(filePath);
using (var writeStream = File.CreateText(filePath))
Export(source, writeStream, Path.GetFileNameWithoutExtension(filePath));
}
#region private
private static void Export(StackSource source, TextWriter writer, string name)
{
var samplesPerThread = GetSortedSamplesPerThread(source);
var exportedFrameNameToExportedFrameId = new Dictionary<string, int>();
var exportedFrameIdToFrameTuple = new Dictionary<int, FrameInfo>();
var profileEventsPerThread = new Dictionary<string, IReadOnlyList<ProfileEvent>>(samplesPerThread.Count);
foreach (var pair in samplesPerThread)
{
var sortedProfileEvents = GetProfileEvents(source, pair.Value, exportedFrameNameToExportedFrameId, exportedFrameIdToFrameTuple);
Debug.Assert(Validate(sortedProfileEvents), "The output should be always valid");
profileEventsPerThread.Add(pair.Key.Name, sortedProfileEvents);
};
var orderedFrameNames = exportedFrameNameToExportedFrameId.OrderBy(pair => pair.Value).Select(pair => pair.Key).ToArray();
WriteToFile(profileEventsPerThread, orderedFrameNames, writer, name);
}
/// <summary>
/// writes pre-calculated data to SpeedScope format
/// </summary>
private static void WriteToFile(IReadOnlyDictionary<string, IReadOnlyList<ProfileEvent>> sortedProfileEventsPerThread,
IReadOnlyList<string> orderedFrameNames, TextWriter writer, string name)
{
Dictionary<string, string> escapedNames = new Dictionary<string, string>();
writer.Write("{");
writer.Write($"\"exporter\": \"{GetExporterInfo()}\", ");
writer.Write($"\"name\": \"{GetEscaped(name, escapedNames)}\", ");
writer.Write("\"activeProfileIndex\": 0, ");
writer.Write("\"$schema\": \"https://www.speedscope.app/file-format-schema.json\", ");
writer.Write("\"shared\": { \"frames\": [ ");
for (int i = 0; i < orderedFrameNames.Count; i++)
{
writer.Write($"{{ \"name\": \"{GetEscaped(orderedFrameNames[i], escapedNames)}\" }}");
if (i != orderedFrameNames.Count - 1)
writer.Write(", ");
}
writer.Write("] }, ");
writer.Write("\"profiles\": [ ");
bool isFirst = true;
foreach (var perThread in sortedProfileEventsPerThread.OrderBy(pair => pair.Value.First().RelativeTime))
{
if (!isFirst)
writer.Write(", ");
else
isFirst = false;
var sortedProfileEvents = perThread.Value;
writer.Write("{ ");
writer.Write("\"type\": \"evented\", ");
writer.Write($"\"name\": \"{GetEscaped(perThread.Key, escapedNames)}\", ");
writer.Write("\"unit\": \"milliseconds\", ");
writer.Write($"\"startValue\": {sortedProfileEvents.First().RelativeTime.ToString("R", CultureInfo.InvariantCulture)}, ");
writer.Write($"\"endValue\": {sortedProfileEvents.Last().RelativeTime.ToString("R", CultureInfo.InvariantCulture)}, ");
writer.Write("\"events\": [ ");
for (int i = 0; i < sortedProfileEvents.Count; i++)
{
var frameEvent = sortedProfileEvents[i];
writer.Write($"{{ \"type\": \"{(frameEvent.Type == ProfileEventType.Open ? "O" : "C")}\", ");
writer.Write($"\"frame\": {frameEvent.FrameId}, ");
// "R" is crucial here!!! we can't lose precision because it can affect the sort order!!!!
writer.Write($"\"at\": {frameEvent.RelativeTime.ToString("R", CultureInfo.InvariantCulture)} }}");
if (i != sortedProfileEvents.Count - 1)
writer.Write(", ");
}
writer.Write("]");
writer.Write("}");
}
writer.Write("] }");
}
#endregion private
}
}