-
Notifications
You must be signed in to change notification settings - Fork 322
Modify SqlStreamingXml XmlWriter to internally use a MemoryStream #3974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jimhblythe
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
jimhblythe:issue-1877
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−46
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlStreamingXmlTest/SqlStreamingXmlTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Data; | ||
| using System.Diagnostics; | ||
| using System.Globalization; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
| { | ||
| public static class SqlStreamingXmlTest | ||
| { | ||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
| public static void LinearSingleNode() | ||
| { | ||
| SqlConnection connection = new(DataTestUtility.TCPConnectionString); | ||
| // Use a literal XML column of the specified size. The XML is constructed by replicating a string of 'B' characters to reach the desired size, and wrapping it in XML tags. | ||
| const string commandTextBase = "SELECT Convert(xml, N'<foo>' + REPLICATE(CAST('' AS nvarchar(max)) +N'B', ({0} * 1024 * 1024) - 11) + N'</foo>')"; | ||
|
|
||
| TimeSpan time1 = TimedExecution(commandTextBase, 1); | ||
| TimeSpan time5 = TimedExecution(commandTextBase, 5); | ||
|
|
||
| // Compare linear time for 1MB vs 5MB. We expect the time to be at most 6 times higher for 5MB, which permits additional 20% for any noise in the measurements. | ||
| Assert.True(time5.TotalMilliseconds <= (time1.TotalMilliseconds * 6), $"Execution time did not follow linear scale: 1MB={time1.TotalMilliseconds}ms vs. 5MB={time5.TotalMilliseconds}ms"); | ||
| } | ||
|
|
||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
| public static void LinearMultipleNodes() | ||
| { | ||
| SqlConnection connection = new(DataTestUtility.TCPConnectionString); | ||
| // Use a literal XML column with the specified number of 1MB elements. The XML is constructed by replicating a string of 'B' characters to reach 1MB, then replicating to the desired number of elements. | ||
| const string commandTextBase = "SELECT Convert(xml, REPLICATE(N'<foo>' + REPLICATE(CAST('' AS nvarchar(max)) + N'B', (1024 * 1024) - 11) + N'</foo>', {0}))"; | ||
|
|
||
| TimeSpan time1 = TimedExecution(commandTextBase, 1); | ||
| TimeSpan time5 = TimedExecution(commandTextBase, 5); | ||
|
|
||
| // Compare linear time for 1MB vs 5MB. We expect the time to be at most 6 times higher for 5MB, which permits additional 20% for any noise in the measurements. | ||
| Assert.True(time5.TotalMilliseconds <= (time1.TotalMilliseconds * 6), $"Execution time did not follow linear scale: 1x={time1.TotalMilliseconds}ms vs. 5x={time5.TotalMilliseconds}ms"); | ||
| } | ||
|
|
||
| private static TimeSpan TimedExecution(string commandTextBase, int scale) | ||
| { | ||
| SqlConnection connection = new(DataTestUtility.TCPConnectionString); | ||
| var stopwatch = new Stopwatch(); | ||
|
|
||
| using (SqlCommand command = connection.CreateCommand()) | ||
| { | ||
| connection.Open(); | ||
| command.CommandText = string.Format(CultureInfo.InvariantCulture, commandTextBase, scale); | ||
|
|
||
| SqlDataReader sqlDataReader = command.ExecuteReader(CommandBehavior.SequentialAccess); | ||
| if (sqlDataReader.Read()) | ||
| { | ||
| stopwatch.Start(); | ||
| ReadAllChars(sqlDataReader, scale); | ||
| stopwatch.Stop(); | ||
| } | ||
| connection.Close(); | ||
| } | ||
|
|
||
| return stopwatch.Elapsed; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Replicate the reading approach used with issue #1877 | ||
| /// </summary> | ||
| private static void ReadAllChars(SqlDataReader sqlDataReader, int expectedMB) | ||
| { | ||
| var expectedSize = expectedMB * 1024 * 1024; | ||
| var text = new char[expectedSize]; | ||
| var buffer = new char[1]; | ||
|
|
||
| long position = 0; | ||
| long numCharsRead; | ||
| do | ||
| { | ||
| numCharsRead = sqlDataReader.GetChars(0, position, buffer, 0, 1); | ||
| if (numCharsRead > 0) | ||
| { | ||
| text[position] = buffer[0]; | ||
| position += numCharsRead; | ||
| } | ||
| } | ||
| while (numCharsRead > 0); | ||
|
|
||
| Assert.Equal(expectedSize, position); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's written to the MemoryStream is bytes, the number of bytes and chars can be quite different depending on encoding. It might be clearer to just refer to byte counts in the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree for a MemoryStream, but within method GetChars() the stream is passed into XmlWriter.Create() with specific Encoding = new UTF8Encoding(false) to prevent the BOM, as well as meet the XML expectation.
The only use for writing is within WriteXmlElement() consistent with only XML characters being written. Changing the comment for this specific method might dilute that only chars are written. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ultimately, the comment should assist future maintenance so I'm good with modifying it to achieve that goal while also being more inline with MemoryStream semantics.
Something felt unexpected about the prior StringWriter using the default UTF-8 encoding, and why I included "Note: UTF8Encoding(false)" within the PR comment. Perhaps there is a different, existing issue with SqlClient support of other encodings. I've seen references to using FOR XML with non-XML columns causing concerns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's hard to tell what the right thing to do is. I'll go with your intuition as you currently know the code better.
do the new tests you've added work on the old implementation as well? What sort of code coverage do we have?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will modify the comment on Mon. and also comment regarding the above "UTF8Encoding(false) addition in s_writerSettings" as the PR note is not as good as within the actual code.
TDD-wise, I was using a locally modified version SqlBenchmarker testing extremes - up to 500MB single node & up to 500 x 1MB nodes. Both ran longer than I cared to wait on prior SqlClient code and completed under 3 minutes on new. Minimal time difference between single and multiple nodes, but noticeable memory usage difference through Task Manager.
I'm not sure on code coverage tests. I will review them before pushing comment changes. I did not run the PerformanceTests, so I will have to get my environment configured for them.