Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/PerfView.Tests/StackViewer/StackWindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -859,5 +859,39 @@ public override string GetFrameName(StackSourceFrameIndex frameIndex, bool verbo
return frameIndex.ToString();
}
}

[WpfFact]
[WorkItem(2308, "https://github.com/Microsoft/perfview/issues/2308")]
public void TestExportFlameGraphWithInvalidCanvasSize()
{
// Create a canvas with zero size (simulating an unrendered or collapsed canvas)
var canvas = new Canvas();
canvas.Width = 0;
canvas.Height = 0;
canvas.Measure(new Size(0, 0));
canvas.Arrange(new Rect(0, 0, 0, 0));

var tempFile = Path.GetTempFileName();
try
{
// Attempt to export should throw InvalidOperationException with a meaningful message
var exception = Assert.Throws<InvalidOperationException>(() =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var exception = Assert.Throws<InvalidOperationException>(() =>
var exception = Assert.Throws<ArgumentOutOfRangeException>(() =>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0699b4a

{
FlameGraph.Export(canvas, tempFile);
});

// Verify the exception message is helpful
Assert.Contains("canvas has an invalid size", exception.Message);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assert.Contains("canvas has an invalid size", exception.Message);
Assert.Contains("Canvas has an invalid size", exception.Message);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 9f9e872

Assert.Contains("width=0", exception.Message);
Assert.Contains("height=0", exception.Message);
}
finally
{
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}
}
}
13 changes: 12 additions & 1 deletion src/PerfView/StackViewer/FlameGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ public static IEnumerable<FlameBox> Calculate(CallTree callTree, double maxWidth
public static void Export(Canvas flameGraphCanvas, string filePath)
{
var rectangle = new Rect(flameGraphCanvas.RenderSize);
var renderTargetBitmap = new RenderTargetBitmap((int)rectangle.Right, (int)rectangle.Bottom, 96d, 96d, PixelFormats.Default);
int width = (int)rectangle.Right;
int height = (int)rectangle.Bottom;

// Validate that the canvas has a valid size before attempting to export
if (width <= 0 || height <= 0)
{
throw new InvalidOperationException(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new InvalidOperationException(
throw new ArgumentOutOfRangeException(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0699b4a

$"Cannot export flame graph: the canvas has an invalid size (width={width}, height={height}). " +
"Please ensure the flame graph is visible and has been rendered before attempting to export.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text here is too long to appear in the status bar. Please use this text: "Please ensure the flame graph is visible and has been rendered before attempting to export."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0699b4a - shortened the error message to just the essential guidance

}

var renderTargetBitmap = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
renderTargetBitmap.Render(flameGraphCanvas);

var pngEncoder = new PngBitmapEncoder();
Expand Down
14 changes: 11 additions & 3 deletions src/PerfView/StackViewer/StackWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2764,10 +2764,18 @@ private void DoSaveFlameGraph(object sender, RoutedEventArgs e)
var result = saveDialog.ShowDialog();
if (result == true)
{
if (FlameGraphCanvas.IsEmpty || m_RedrawFlameGraphWhenItBecomesVisible)
RedrawFlameGraph();
try
{
if (FlameGraphCanvas.IsEmpty || m_RedrawFlameGraphWhenItBecomesVisible)
RedrawFlameGraph();

FlameGraph.Export(FlameGraphCanvas, saveDialog.FileName);
FlameGraph.Export(FlameGraphCanvas, saveDialog.FileName);
StatusBar.Log($"Saved flame graph to {saveDialog.FileName}");
}
catch (InvalidOperationException ex)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
catch (InvalidOperationException ex)
catch (ArgumentOutOfRangeException ex)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0699b4a

{
StatusBar.LogError($"Failed to save flame graph: {ex.Message}");
}
}
}

Expand Down