From a8b857b636cde3df6b958063610b44806b2712a8 Mon Sep 17 00:00:00 2001 From: Tanmay Seth Date: Mon, 9 Nov 2026 09:58:09 +0530 Subject: [PATCH] Fix security report findings; add one-click procedure tuning with a before/after benchmark Security (from the 8 Nov scan): - Pin patched MessagePack 2.5.303 (compile-time only, ExcludeAssets=runtime) and System.Security.Cryptography.Pkcs 10.0.13; refresh both packages.lock.json files. - Protect the saved connection string with DPAPI entropy (DS112266); values written by earlier versions are re-protected on first load. - Reword / suppress the loopback address notes (DS162092). - security.yml: retry the Syft and Grype downloads once after a pause. The installer scan failed on a GitHub release download returning HTTP 502, before any scan ran. Feature: - "Tune procedures" button: pick several stored procedures (with plan-cache cost stats and a "Slowest 5" shortcut), Claude writes _Optimized copies and calls the new create_benchmark_plan MCP tool. - BenchmarkPlan generates a before/after test plan for a query window: elapsed, CPU and logical reads per run; mean, SD, speed-up, Welch t and 95% CI; a result-equality check; runs rolled back by default; cleanup block. Optimized copies must be named *_Optimized. Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/security.yml | 56 +- README.md | 31 +- download-function/DownloadFunction.csproj | 2 + download-function/packages.lock.json | 11 +- .../Database/BenchmarkPlan.cs | 841 ++++++++++++++++++ .../Database/ProcedureTuneRequest.cs | 48 + .../Database/SqlObjectName.cs | 159 ++++ .../Database/StoredProcedureCatalog.cs | 125 +++ src/SsmsChatExtension/Mcp/McpHttpServer.cs | 7 +- src/SsmsChatExtension/Mcp/SsmsMcpTools.cs | 166 +++- .../Services/Claude/ClaudeCodeChatService.cs | 11 +- .../Services/Claude/ClaudeSettings.cs | 43 +- .../SsmsChatExtension.csproj | 15 + src/SsmsChatExtension/UI/ChatControl.xaml | 2 + src/SsmsChatExtension/UI/ChatViewModel.cs | 91 +- .../UI/ProcedureTuneDialog.xaml | 103 +++ .../UI/ProcedureTuneDialog.xaml.cs | 252 ++++++ src/SsmsChatExtension/packages.lock.json | 33 +- 18 files changed, 1957 insertions(+), 39 deletions(-) create mode 100644 src/SsmsChatExtension/Database/BenchmarkPlan.cs create mode 100644 src/SsmsChatExtension/Database/ProcedureTuneRequest.cs create mode 100644 src/SsmsChatExtension/Database/SqlObjectName.cs create mode 100644 src/SsmsChatExtension/Database/StoredProcedureCatalog.cs create mode 100644 src/SsmsChatExtension/UI/ProcedureTuneDialog.xaml create mode 100644 src/SsmsChatExtension/UI/ProcedureTuneDialog.xaml.cs diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index f706efe..586f490 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -244,9 +244,10 @@ jobs: name: sarif-dependencies path: sarif - # Fails on critical only: the high ones today (MessagePack, System.Text.Json, Microsoft.IO.Redist) come in - # through Microsoft.VisualStudio.SDK, which is compile-time only (ExcludeAssets="runtime"); SSMS supplies - # those assemblies and the VSIX ships none of them. Direct dependencies are held to moderate by NuGet audit. + # Fails on critical only: transitive advisories can come in through Microsoft.VisualStudio.SDK, which is + # compile-time only (ExcludeAssets="runtime"); SSMS supplies those assemblies and the VSIX ships none of them. + # Direct dependencies are held to moderate by NuGet audit, so the MessagePack and Pkcs advisories were fixed by + # pinning patched direct references in the two projects. - name: Check findings shell: pwsh run: .github/scripts/Assert-Sarif.ps1 -Path sarif -FailOn critical -Title 'Dependencies (OSV-Scanner, Trivy)' @@ -312,7 +313,26 @@ jobs: for vsix in build/*.vsix; do unzip -q "$vsix" -d "build/unpacked/$(basename "$vsix" .vsix)"; done find build -type f -exec ls -l {} + + # Syft and Grype are downloaded from GitHub releases when each action starts, and that download now and then + # answers 502 (the 8 Nov scan failed that way before scanning anything, and the report showed "Failed" for the + # installer). Each tool step therefore gets one retry after a pause; a second failure still fails the job. - name: SBOM (SPDX) + id: sbom-spdx + continue-on-error: true + uses: anchore/sbom-action@66cbf4bc1f1c0d2edc94016e65bc221b6bb0ad6c # v0.24.3 + with: + path: build + format: spdx-json + output-file: sbom.spdx.json + artifact-name: sbom.spdx.json + upload-release-assets: false + + - name: Wait before retrying SBOM (SPDX) + if: steps.sbom-spdx.outcome == 'failure' + run: sleep 60 + + - name: SBOM (SPDX), retry + if: steps.sbom-spdx.outcome == 'failure' uses: anchore/sbom-action@66cbf4bc1f1c0d2edc94016e65bc221b6bb0ad6c # v0.24.3 with: path: build @@ -322,6 +342,22 @@ jobs: upload-release-assets: false - name: SBOM (CycloneDX) + id: sbom-cdx + continue-on-error: true + uses: anchore/sbom-action@66cbf4bc1f1c0d2edc94016e65bc221b6bb0ad6c # v0.24.3 + with: + path: build + format: cyclonedx-json + output-file: sbom.cdx.json + artifact-name: sbom.cdx.json + upload-release-assets: false + + - name: Wait before retrying SBOM (CycloneDX) + if: steps.sbom-cdx.outcome == 'failure' + run: sleep 60 + + - name: SBOM (CycloneDX), retry + if: steps.sbom-cdx.outcome == 'failure' uses: anchore/sbom-action@66cbf4bc1f1c0d2edc94016e65bc221b6bb0ad6c # v0.24.3 with: path: build @@ -332,6 +368,20 @@ jobs: - name: Grype id: grype + continue-on-error: true + uses: anchore/scan-action@27805bf3b4e84b4a5c980df22ed233c00390a439 # v7.4.2 + with: + sbom: sbom.spdx.json + fail-build: false + output-format: sarif + output-file: sarif/grype.sarif + + - name: Wait before retrying Grype + if: steps.grype.outcome == 'failure' + run: sleep 60 + + - name: Grype, retry + if: steps.grype.outcome == 'failure' uses: anchore/scan-action@27805bf3b4e84b4a5c980df22ed233c00390a439 # v7.4.2 with: sbom: sbom.spdx.json diff --git a/README.md b/README.md index 1084770..8b14baf 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,32 @@ so treat it as a strong default rather than a security boundary.) reason (review them - they are guesses) and the views / procedures / functions that read other databases. - Large selections are trimmed to 200 tables (tables with relationships first), and above 40 tables only key columns are shown. Claude can do the same with `show_database_diagram` (one or more databases). +- **One-click procedure tuning with a before / after benchmark**: the **Tune procedures** (lightning bolt) button + lists the stored procedures of the active connection's database, with executions, average time and logical reads + from the plan cache (`sys.dm_exec_procedure_stats`, when your login may read it). Tick as many as you like (up to + 10 per run; **Slowest 5** picks the ones that used the most time), choose the number of timed runs and click + **Tune and benchmark**. Claude reads each procedure and its dependencies, gathers evidence with read-only + queries, writes an optimized copy named `_Optimized` that returns the same results (the original is never + changed) and calls `create_benchmark_plan`, which opens a **test plan in a new query window**. The plan is a + script you read and run with F5; nothing runs until you do: + - Every procedure and its copy run 2 warm-up + N timed times (no supporting change: alternating run by run, so + drift of the machine hits both equally; with a supporting change such as an index: all *before* runs, then + the change is applied, then all *after* runs). Each run records elapsed time, CPU time and logical reads + from `sys.dm_exec_requests`. + - **Result grid**: mean before and after, *time saved %* = (before - after) / before x 100, *speed-up* = + before / after, the same for logical reads and CPU, Welch's *t* = (meanb - meana) / + sqrt(sdb^2 / nb + sda^2 / na), the 95% confidence interval of the time saved + (difference +/- 1.96 x SE) and a verdict: *FASTER* / *SLOWER* needs |t| >= 2 **and** a change of at least 5%; + anything smaller is *NO MEANINGFUL DIFFERENCE*. More grids show mean / median / SD / min / max, an overall + total, errors and every raw run. + - **Result check**: both versions run once more into table variables and their rows are compared (same rows, + same order). A copy that returns different rows is flagged *DO NOT USE - the results differ*, however fast it is. + - Safety: every run is wrapped in `BEGIN TRAN ... ROLLBACK` by default; optimized copies must be named + `*_Optimized`, so a plan can never overwrite another procedure; a cleanup block at the end of the script + drops the copies and undoes supporting changes. Switch on *Query Options > Results > Grid > Discard results + after execution* before running, and use a test copy for procedures that change data (rollback does not undo + mail, files, linked servers or sequence values). Needs SQL Server 2016 SP1 or later. + - Claude cannot run procedures itself, so it never quotes timings: the numbers come from your run of the plan. - **Runs SQL for you**: `execute_sql` runs any script (INSERT / UPDATE / DELETE, CREATE / ALTER, EXEC, `GO` batches), commits it and gives Claude the results; `execute_query_window` presses F5 on the active query window so you see the results grid in SSMS. What happens depends on **Settings > Let Claude run SQL that @@ -104,7 +130,8 @@ SSMS Claude Chat window ──stdin──▶ claude -p --output-format stream-js every SSMS session. - Database tools use the connection of the active query window, created by SSMS itself (Windows, SQL and Microsoft Entra logins all work). If that is not possible, set a **connection string override** in Settings - (stored DPAPI-encrypted for your Windows user). + (stored DPAPI-encrypted for your Windows user, with an extension-specific entropy value so other programs running as + you cannot decrypt it by calling DPAPI blindly; a value saved by an earlier version is re-encrypted on first load). - `run_query` is read-only by design: statements that write, `EXEC`, transactions, `USE`, `OPENROWSET` and similar are rejected before they run, and every query also runs inside a transaction that is always rolled back, with a 30 second timeout and a row limit. @@ -247,7 +274,7 @@ ChatServiceProvider.Current = new EchoChatService(); | `src/SsmsChatExtension/Services/` | `IChatService`, the echo stub, the message models and the chat history store | | `src/SsmsChatExtension/Services/Claude/` | Claude Code CLI wrapper (sign-in, status), the chat service, settings | | `src/SsmsChatExtension/Mcp/` | Loopback MCP server and the `ssms` tools Claude calls | -| `src/SsmsChatExtension/Database/` | Active query window connection (via SSMS internals), the read-only query runner and the `GO`-aware executor | +| `src/SsmsChatExtension/Database/` | Active query window connection (via SSMS internals), the read-only query runner and the `GO`-aware executor; the procedure list (`StoredProcedureCatalog`), the before / after benchmark script generator (`BenchmarkPlan`) and the safe object-name parser it uses (`SqlObjectName`) | | `src/SsmsChatExtension/Diagrams/` | Mermaid diagram pages opened by `show_diagram` | | `src/SsmsChatExtension/Editor/SsmsEditor.cs` | Reads / writes / opens / executes query windows through DTE | | `src/SsmsChatExtension/Editor/SsmsObjectExplorer.cs` | Refreshes Object Explorer and the IntelliSense cache after schema changes | diff --git a/download-function/DownloadFunction.csproj b/download-function/DownloadFunction.csproj index bd59e08..b7a254b 100644 --- a/download-function/DownloadFunction.csproj +++ b/download-function/DownloadFunction.csproj @@ -27,6 +27,8 @@ + + diff --git a/download-function/packages.lock.json b/download-function/packages.lock.json index 7251d34..8692f15 100644 --- a/download-function/packages.lock.json +++ b/download-function/packages.lock.json @@ -73,6 +73,12 @@ "System.Threading.RateLimiting": "9.0.18" } }, + "System.Security.Cryptography.Pkcs": { + "type": "Direct", + "requested": "[10.0.13, )", + "resolved": "10.0.13", + "contentHash": "a+EovgC1fBnwdlQ6cn3j4JFBfKqiFaFvoAhjisOoSvazLAcIznaBzPQP0oYnvccmeXfqUH1KebVZOxSeH6rMaw==" + }, "Apache.Arrow": { "type": "Transitive", "resolved": "22.1.0", @@ -632,11 +638,6 @@ "resolved": "10.0.9", "contentHash": "YweovHiLUoEdbr2Xv8eRqCSkTu1vMms4a6xBHaQiMSFVy32PL+hXv6Vhol2NWOmLHjaVgTyhJ3R52U6JxS6w3Q==" }, - "System.Security.Cryptography.Pkcs": { - "type": "Transitive", - "resolved": "9.0.18", - "contentHash": "NKMKhUdLGNZryY9GXXTF5dXyP6S37EYXhiA2JYvwshJbd4DRq07IOS9it8Opi9atadvv5rlNkD76Mio+5Oo2aQ==" - }, "System.Security.Cryptography.ProtectedData": { "type": "Transitive", "resolved": "9.0.18", diff --git a/src/SsmsChatExtension/Database/BenchmarkPlan.cs b/src/SsmsChatExtension/Database/BenchmarkPlan.cs new file mode 100644 index 0000000..21b63d9 --- /dev/null +++ b/src/SsmsChatExtension/Database/BenchmarkPlan.cs @@ -0,0 +1,841 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace SsmsChatExtension.Database +{ + /// One stored procedure to benchmark: the original, its optimized copy and how to call both. + internal sealed class BenchmarkProcedure + { + /// The original procedure, as schema.name. + public string Name { get; set; } + + /// Name of the optimized copy. Default: the original name plus _Optimized. + public string OptimizedName { get; set; } + + /// CREATE [OR ALTER] PROCEDURE statement of the optimized copy (one batch, no GO). + public string OptimizedDefinition { get; set; } + + /// The argument list of the EXEC, for example @CustomerId = 42, @From = '2025-01-01'. + public string Arguments { get; set; } + + /// What was changed and why; printed in the script header. + public string Changes { get; set; } + + /// True when the procedure writes data (the plan then warns if runs are not rolled back). + public bool ModifiesData { get; set; } + + /// True when a procedure with the optimized name already exists; running the plan replaces it. + public bool OptimizedExists { get; set; } + } + + /// A change that both procedures depend on, such as an index, applied between the BEFORE and AFTER runs. + internal sealed class BenchmarkChange + { + public string Description { get; set; } + + public string ApplySql { get; set; } + + public string UndoSql { get; set; } + } + + internal sealed class BenchmarkRequest + { + public string Title { get; set; } + + public string Database { get; set; } + + public int Runs { get; set; } = BenchmarkPlan.DefaultRuns; + + public int WarmupRuns { get; set; } = BenchmarkPlan.DefaultWarmupRuns; + + public bool RollbackEachRun { get; set; } = true; + + public IList Procedures { get; set; } = new List(); + + public IList SupportingChanges { get; set; } = new List(); + } + + /// + /// Builds the before / after benchmark script that is opened in a query window. The measuring and the statistics are + /// fixed T-SQL (so the numbers mean the same thing every time); only the procedures, the arguments and the optimized + /// definitions come from the request. Nothing here runs against the database. + /// + internal static class BenchmarkPlan + { + public const int DefaultRuns = 10; + public const int DefaultWarmupRuns = 2; + public const int MinRuns = 3; + public const int MaxRuns = 200; + public const int MaxWarmupRuns = 10; + public const int MaxProcedures = 10; + public const string OptimizedSuffix = "_Optimized"; + + private static readonly Regex GoLine = new Regex(@"^[ \t]*GO(?:[ \t]+\d+)?[ \t]*(?:--[^\r\n]*)?\r?$", + RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly Regex CreateHeader = new Regex(@"\G(?CREATE)(?:\s+OR\s+ALTER)?\s+PROC(?:EDURE)?\s+", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + + private static readonly Regex ForbiddenArguments = new Regex(@"--|/\*|\*/|;", RegexOptions.Compiled); + + /// Checks the request and returns the script. Throws ArgumentException with a message that says what to fix. + public static string Build(BenchmarkRequest request, DateTime generatedAt) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + if (string.IsNullOrWhiteSpace(request.Database)) throw new ArgumentException("The database name is required."); + if (request.Procedures == null || request.Procedures.Count == 0) throw new ArgumentException("Give at least one procedure to benchmark."); + if (request.Procedures.Count > MaxProcedures) throw new ArgumentException("At most " + MaxProcedures + " procedures per plan; split them over several plans."); + if (request.Runs < MinRuns || request.Runs > MaxRuns) throw new ArgumentException("runs must be between " + MinRuns + " and " + MaxRuns + "."); + if (request.WarmupRuns < 0 || request.WarmupRuns > MaxWarmupRuns) throw new ArgumentException("warmup_runs must be between 0 and " + MaxWarmupRuns + "."); + + var cases = request.Procedures.Select((p, i) => Prepare(p, i + 1)).ToList(); + var duplicates = cases.GroupBy(c => c.Original.ToString(), StringComparer.OrdinalIgnoreCase).FirstOrDefault(g => g.Count() > 1); + if (duplicates != null) throw new ArgumentException(duplicates.Key + " is listed twice."); + + var changes = (request.SupportingChanges ?? new List()) + .Where(c => !string.IsNullOrWhiteSpace(c?.ApplySql)) + .ToList(); + + var database = request.Database.Trim().Trim('[', ']'); + var script = new StringBuilder(); + script.Append(Header(request, database, cases, changes, generatedAt)); + script.AppendLine("USE " + SqlObjectName.QuoteName(database) + ";"); + script.AppendLine("GO"); + script.AppendLine(); + script.Append(Setup(request, database, cases)); + script.AppendLine("GO"); + script.AppendLine(); + script.Append(RunnerProcedure); + script.AppendLine("GO"); + script.AppendLine(); + script.Append(VerifyProcedure); + script.AppendLine("GO"); + script.AppendLine(); + if (changes.Count == 0) + { + // Nothing has to happen between the two measurements, so the versions alternate run by run and drift cancels out. + script.Append(Apply(cases, changes)); + script.Append(Phase("MEASURE - the original and its optimized copy, alternating run by run", "EXEC #BenchRun @mode = 'Interleaved';")); + } + else + { + // A supporting change (an index) must sit between the two measurements, so they run one after the other. + script.Append(Phase("BEFORE - the original procedures", "EXEC #BenchRun @mode = 'Before';")); + script.AppendLine("GO"); + script.AppendLine(); + script.Append(Apply(cases, changes)); + script.Append(Phase("AFTER - the optimized copies", "EXEC #BenchRun @mode = 'After';")); + } + script.AppendLine("GO"); + script.AppendLine(); + script.Append(Phase("CHECK - do both versions return the same rows?", "EXEC #BenchVerify;")); + script.AppendLine("GO"); + script.AppendLine(); + script.Append(Report); + script.AppendLine("GO"); + script.AppendLine(); + script.Append(Cleanup(cases, changes)); + + // One line ending throughout: the templates below use the source file's, AppendLine uses the machine's, and SSMS asks to normalise a mix on save. + return script.ToString().Replace("\r\n", "\n").Replace("\n", Environment.NewLine); + } + + // ---- validation ---- + + private sealed class Case + { + public int Id; + public SqlObjectName Original; + public SqlObjectName Optimized; + public string Definition; + public string Arguments; + public string Changes; + public bool ModifiesData; + public bool OptimizedExists; + } + + private static Case Prepare(BenchmarkProcedure procedure, int id) + { + if (procedure == null) throw new ArgumentException("A procedure entry is empty."); + + var original = SqlObjectName.Parse(procedure.Name); + var optimized = string.IsNullOrWhiteSpace(procedure.OptimizedName) + ? SqlObjectName.FromParts(original.Schema, original.Name + OptimizedSuffix) + : SqlObjectName.Parse(procedure.OptimizedName); + if (original.SameAs(optimized)) + { + throw new ArgumentException(original + ": the optimized copy needs its own name (for example " + original.Name + "_Optimized); the original is never modified."); + } + // The plan runs CREATE OR ALTER on this name, so it must be recognisably a copy and never one of the user's own procedures. + if (!optimized.Name.EndsWith(OptimizedSuffix, StringComparison.OrdinalIgnoreCase)) + { + throw new ArgumentException(original + ": the optimized copy must be named *" + OptimizedSuffix + " (for example " + original.Name + OptimizedSuffix + "), so the plan can never overwrite another procedure."); + } + + var arguments = Regex.Replace(procedure.Arguments ?? string.Empty, @"\s+", " ").Trim(); + if (ForbiddenArguments.IsMatch(arguments)) + { + throw new ArgumentException(original + ": call_arguments may only be the argument list (for example @Id = 42), without comments or semicolons."); + } + + return new Case + { + Id = id, + Original = original, + Optimized = optimized, + Definition = NormalizeDefinition(original, optimized, procedure.OptimizedDefinition), + Arguments = arguments, + Changes = procedure.Changes, + ModifiesData = procedure.ModifiesData, + OptimizedExists = procedure.OptimizedExists + }; + } + + /// + /// Checks that the definition creates exactly the optimized procedure and nothing else, and rewrites its header to + /// CREATE OR ALTER PROCEDURE [schema].[name], so running the plan twice works. + /// + private static string NormalizeDefinition(SqlObjectName original, SqlObjectName optimized, string definition) + { + if (string.IsNullOrWhiteSpace(definition)) + { + throw new ArgumentException(original + ": optimized_definition is required (a CREATE OR ALTER PROCEDURE statement for " + optimized + ")."); + } + if (GoLine.IsMatch(definition)) + { + throw new ArgumentException(original + ": optimized_definition must be one batch; remove the GO lines."); + } + + var start = SkipCommentsAndWhitespace(definition); + var header = CreateHeader.Match(definition, start); + if (!header.Success) + { + throw new ArgumentException(original + ": optimized_definition must start with CREATE OR ALTER PROCEDURE " + optimized + "."); + } + + var position = header.Index + header.Length; + SqlObjectName declared; + string problem; + if (!SqlObjectName.TryRead(definition, ref position, out declared, out problem)) + { + throw new ArgumentException(original + ": " + problem); + } + if (!declared.SameAs(optimized)) + { + throw new ArgumentException(original + ": optimized_definition creates " + declared + " but optimized_name is " + optimized + "."); + } + + return definition.Substring(0, header.Index).TrimEnd() + (header.Index > 0 ? Environment.NewLine : string.Empty) + + "CREATE OR ALTER PROCEDURE " + optimized.Quoted + definition.Substring(position).TrimEnd(); + } + + private static int SkipCommentsAndWhitespace(string text) + { + var i = 0; + while (i < text.Length) + { + if (char.IsWhiteSpace(text[i])) { i++; continue; } + if (text[i] == '-' && i + 1 < text.Length && text[i + 1] == '-') + { + while (i < text.Length && text[i] != '\n') i++; + continue; + } + if (text[i] == '/' && i + 1 < text.Length && text[i + 1] == '*') + { + var end = text.IndexOf("*/", i + 2, StringComparison.Ordinal); + if (end < 0) return text.Length; + i = end + 2; + continue; + } + break; + } + return i; + } + + // ---- script sections ---- + + private static string Header(BenchmarkRequest request, string database, List cases, List changes, DateTime generatedAt) + { + var text = new StringBuilder(); + text.AppendLine("/*=============================================================================="); + text.AppendLine(" " + Comment(string.IsNullOrWhiteSpace(request.Title) ? "Stored procedure benchmark: before and after" : request.Title.Trim())); + text.AppendLine(" Generated by SSMS Claude Chat on " + generatedAt.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture) + " for database " + Comment(SqlObjectName.QuoteName(database))); + text.AppendLine(); + text.AppendLine(" WHAT THIS SCRIPT DOES"); + if (changes.Count == 0) + { + text.AppendLine(" 1. APPLY Creates the optimized copies (_Optimized). The original procedures are NOT modified."); + text.AppendLine(" 2. MEASURE Runs every original procedure and its optimized copy " + request.WarmupRuns + " warm-up + " + request.Runs + " timed times,"); + text.AppendLine(" alternating run by run (original first, then optimized first, ...), so slow drift of the"); + text.AppendLine(" machine hits both versions equally. Every timed run records elapsed time, CPU time"); + text.AppendLine(" and logical reads."); + text.AppendLine(" 3. CHECK Runs every pair once more and compares the rows they return."); + text.AppendLine(" 4. REPORT Result grids: before vs after per procedure, overall total, statistics, errors."); + } + else + { + text.AppendLine(" 1. BEFORE Runs each original procedure " + request.WarmupRuns + " warm-up + " + request.Runs + " timed times. Every timed run records"); + text.AppendLine(" elapsed time, CPU time and logical reads."); + text.AppendLine(" 2. APPLY Creates the optimized copies (_Optimized) and the supporting changes listed"); + text.AppendLine(" below. The original procedures are NOT modified."); + text.AppendLine(" 3. AFTER Runs the optimized copies the same way."); + text.AppendLine(" 4. CHECK Runs every pair once more and compares the rows they return."); + text.AppendLine(" 5. REPORT Result grids: before vs after per procedure, overall total, statistics, errors."); + } + text.AppendLine(); + text.AppendLine(" BEFORE YOU PRESS EXECUTE"); + text.AppendLine(" * This script RUNS the procedures under TEST CASES with the arguments shown. Read them first, and"); + text.AppendLine(" prefer a test copy of the database for procedures that change data."); + text.AppendLine(" * rollback_each_run = 1 (the default, set under TEST CASES AND SETTINGS) wraps every run in BEGIN TRAN ... ROLLBACK, so"); + text.AppendLine(" data changes are undone. Locks and log writes still happen, and effects outside the database"); + text.AppendLine(" (mail, files, linked servers, sequence values) are NOT undone."); + text.AppendLine(" * Switch on Query > Query Options > Results > Grid > \"Discard results after execution\" first,"); + text.AppendLine(" otherwise the grids the procedures return slow the runs down and distort the timings."); + text.AppendLine(" * Needs SQL Server 2016 SP1 or later. Do not run it on a busy production server: the"); + text.AppendLine(" timings are only meaningful when nothing else is competing for the machine."); + text.AppendLine(" * Running it a second time? Run the CLEANUP block at the end first. Otherwise the BEFORE numbers"); + text.AppendLine(" already include the supporting changes of the previous run."); + text.AppendLine(); + text.AppendLine(" HOW TO READ THE REPORT"); + text.AppendLine(" mean = sum of the timed runs / n sd = sample standard deviation"); + text.AppendLine(" Time saved % = (mean_before - mean_after) / mean_before * 100"); + text.AppendLine(" Speed-up (x) = mean_before / mean_after"); + text.AppendLine(" Reads saved % = same formula on logical reads (they barely vary between runs)"); + text.AppendLine(" SE = sqrt( sd_before^2 / n_before + sd_after^2 / n_after )"); + text.AppendLine(" t = (mean_before - mean_after) / SE (Welch's t statistic)"); + text.AppendLine(" 95% CI of the time saved = (mean_before - mean_after) +/- 1.96 * SE"); + text.AppendLine(" |t| >= 2, or a CI that does not contain 0, means the difference is very unlikely to be noise."); + text.AppendLine(" Verdict: FASTER / SLOWER needs |t| >= 2 AND at least a 5% change in mean time; a smaller change is reported"); + text.AppendLine(" as NO MEANINGFUL DIFFERENCE. A procedure whose results differ is always flagged DO NOT USE."); + if (changes.Count > 0) + { + text.AppendLine(" With supporting changes, BEFORE and AFTER run one after the other, so a small systematic bias between the"); + text.AppendLine(" two phases is possible (and the supporting change also affects every other procedure on the same tables)."); + } + text.AppendLine(" Elapsed and CPU time come from the server clock (about 1 ms resolution on many machines). The mean over many"); + text.AppendLine(" runs averages that out; for procedures that finish in under a millisecond raise the number of timed runs"); + text.AppendLine(" (50 or more) and weigh the logical reads, which are exact."); + text.AppendLine(); + text.AppendLine(" TEST CASES"); + foreach (var c in cases) + { + text.AppendLine(" " + c.Id + ". " + Comment(c.Original.Quoted) + " -> " + Comment(c.Optimized.Quoted)); + text.AppendLine(" EXEC " + Comment(c.Original.Quoted) + (c.Arguments.Length > 0 ? " " + Comment(c.Arguments) : string.Empty) + + (c.ModifiesData ? " -- changes data" : string.Empty)); + if (c.OptimizedExists) + { + text.AppendLine(" NOTE: " + Comment(c.Optimized.Quoted) + " already exists; this script replaces it."); + } + if (!string.IsNullOrWhiteSpace(c.Changes)) + { + foreach (var line in Comment(c.Changes).Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)) + { + text.AppendLine(" - " + line.Trim()); + } + } + } + if (changes.Count > 0) + { + text.AppendLine(); + text.AppendLine(" SUPPORTING CHANGES (applied between BEFORE and AFTER; undo statements are in the CLEANUP block at the end)"); + for (var i = 0; i < changes.Count; i++) + { + text.AppendLine(" " + (i + 1) + ". " + Comment(Regex.Replace(changes[i].Description ?? "(no description)", @"\s+", " ").Trim())); + } + } + text.AppendLine("==============================================================================*/"); + text.AppendLine(); + return text.ToString(); + } + + private static string Setup(BenchmarkRequest request, string database, List cases) + { + var rows = cases.Select(c => " (" + c.Id + ", " + + Literal(c.Original.Quoted) + ", " + + Literal(SqlObjectName.QuoteName(database) + "." + c.Original.Quoted) + ", " + + Literal(SqlObjectName.QuoteName(database) + "." + c.Optimized.Quoted) + ", " + + (c.Arguments.Length > 0 ? Literal(c.Arguments) : "NULL") + ", " + + (c.ModifiesData ? "1" : "0") + ")"); + + return SetupTemplate + .Replace("<>", request.Runs.ToString(CultureInfo.InvariantCulture)) + .Replace("<>", request.WarmupRuns.ToString(CultureInfo.InvariantCulture)) + .Replace("<>", request.RollbackEachRun ? "1" : "0") + .Replace("<>", string.Join("," + Environment.NewLine, rows)); + } + + private static string Apply(List cases, List changes) + { + var text = new StringBuilder(); + text.AppendLine("-- ============================================================================="); + text.AppendLine("-- APPLY - creates the optimized copies; the original procedures are not touched"); + text.AppendLine("-- ============================================================================="); + text.AppendLine("PRINT N'APPLY: creating the optimized copies...';"); + text.AppendLine("GO"); + for (var i = 0; i < changes.Count; i++) + { + text.AppendLine(); + text.AppendLine("-- Supporting change " + (i + 1) + ": " + Regex.Replace(Comment(changes[i].Description ?? string.Empty), @"\s+", " ").Trim()); + text.AppendLine(changes[i].ApplySql.Trim()); + text.AppendLine("GO"); + } + foreach (var c in cases) + { + text.AppendLine(); + text.AppendLine("-- Optimized copy of " + c.Original.Quoted); + text.AppendLine(c.Definition); + text.AppendLine("GO"); + } + text.AppendLine(); + return text.ToString(); + } + + private static string Phase(string title, string statement) + { + return "-- =============================================================================" + Environment.NewLine + + "-- " + title + Environment.NewLine + + "-- =============================================================================" + Environment.NewLine + + "PRINT N'" + title.Replace("'", "''") + "...';" + Environment.NewLine + + statement + Environment.NewLine; + } + + private static string Cleanup(List cases, List changes) + { + var text = new StringBuilder(); + text.AppendLine("/*=============================================================================="); + text.AppendLine(" CLEANUP (optional) - remove what this plan created. Select the lines below and run them,"); + text.AppendLine(" or keep the optimized copies and adopt them with CREATE OR ALTER on the original name."); + text.AppendLine(); + foreach (var c in cases) + { + text.AppendLine(" DROP PROCEDURE IF EXISTS " + Comment(c.Optimized.Quoted) + ";"); + } + foreach (var change in changes.Where(c => !string.IsNullOrWhiteSpace(c.UndoSql))) + { + text.AppendLine(); + text.AppendLine(" -- undo: " + Regex.Replace(Comment(change.Description ?? string.Empty), @"\s+", " ").Trim()); + foreach (var line in Comment(change.UndoSql).Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None)) + { + text.AppendLine(" " + line.TrimEnd()); + } + } + text.AppendLine("==============================================================================*/"); + return text.ToString(); + } + + /// Text that goes inside a /* */ comment: a stray comment terminator in it must not end the comment early. + private static string Comment(string text) + { + return (text ?? string.Empty).Replace("*/", "* /").Replace("/*", "/ *"); + } + + private static string Literal(string text) + { + return "N'" + text.Replace("'", "''") + "'"; + } + + // ---- fixed T-SQL ---- + + private const string SetupTemplate = @"-- ============================================================================= +-- TEST CASES AND SETTINGS +-- ============================================================================= +SET NOCOUNT ON; + +IF OBJECT_ID(N'tempdb..#BenchConfig') IS NOT NULL DROP TABLE #BenchConfig; +IF OBJECT_ID(N'tempdb..#BenchCases') IS NOT NULL DROP TABLE #BenchCases; +IF OBJECT_ID(N'tempdb..#BenchRuns') IS NOT NULL DROP TABLE #BenchRuns; +IF OBJECT_ID(N'tempdb..#BenchCheck') IS NOT NULL DROP TABLE #BenchCheck; +IF OBJECT_ID(N'tempdb..#BenchStats') IS NOT NULL DROP TABLE #BenchStats; +IF OBJECT_ID(N'tempdb..#BenchMedian') IS NOT NULL DROP TABLE #BenchMedian; + +CREATE TABLE #BenchConfig +( + runs int NOT NULL, -- timed runs per procedure and variant + warmup_runs int NOT NULL, -- runs before the timed ones (compile the plan, warm the cache); not reported + rollback_each_run bit NOT NULL, -- 1 = wrap every run in BEGIN TRAN ... ROLLBACK + cold_cache bit NOT NULL -- 1 = CHECKPOINT + DBCC DROPCLEANBUFFERS before every timed run (test servers only, needs sysadmin) +); +INSERT #BenchConfig (runs, warmup_runs, rollback_each_run, cold_cache) VALUES (<>, <>, <>, 0); + +CREATE TABLE #BenchCases +( + case_id int NOT NULL PRIMARY KEY, + display_name nvarchar(400) NOT NULL, + original_call_name nvarchar(600) NOT NULL, + optimized_call_name nvarchar(600) NOT NULL, + arguments nvarchar(max) NULL, + modifies_data bit NOT NULL +); +INSERT #BenchCases (case_id, display_name, original_call_name, optimized_call_name, arguments, modifies_data) VALUES +<>; + +CREATE TABLE #BenchRuns +( + case_id int NOT NULL, + variant varchar(6) NOT NULL, -- Before / After + run_no int NOT NULL, + is_warmup bit NOT NULL, + elapsed_ms float NULL, + cpu_ms float NULL, + logical_reads bigint NULL, + physical_reads bigint NULL, + error_message nvarchar(2000) NULL, + PRIMARY KEY (case_id, variant, run_no) +); + +CREATE TABLE #BenchCheck +( + case_id int NOT NULL PRIMARY KEY, + status nvarchar(40) NOT NULL, + rows_before int NULL, + rows_after int NULL, + differing_rows int NULL, + detail nvarchar(1000) NULL +); + +IF EXISTS (SELECT 1 FROM #BenchCases c CROSS JOIN #BenchConfig f WHERE c.modifies_data = 1 AND f.rollback_each_run = 0) + PRINT N'WARNING: a procedure that changes data will run with rollback_each_run = 0, so its changes stay in the database.'; +"; + + private const string RunnerProcedure = @"-- ============================================================================= +-- The measuring procedures (temporary: they live for this session only) +-- ============================================================================= +IF OBJECT_ID(N'tempdb..#BenchOne') IS NOT NULL DROP PROCEDURE #BenchOne; +IF OBJECT_ID(N'tempdb..#BenchRun') IS NOT NULL DROP PROCEDURE #BenchRun; +GO +-- One measured execution of one call: stores a row in #BenchRuns and returns the error, if there was one. +CREATE PROCEDURE #BenchOne @case_id int, @variant varchar(6), @call nvarchar(max), @run int, @warmup int, @rollback bit, @err nvarchar(2000) OUTPUT +AS +BEGIN + SET NOCOUNT ON; + + DECLARE @t0 datetime2(7), @t1 datetime2(7), @cpu0 bigint, @cpu1 bigint, @reads0 bigint, @reads1 bigint, @phys0 bigint, @phys1 bigint; + SET @err = NULL; + + IF @run > @warmup AND EXISTS (SELECT 1 FROM #BenchConfig WHERE cold_cache = 1) + BEGIN + BEGIN TRY + CHECKPOINT; + DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS; + END TRY + BEGIN CATCH + UPDATE #BenchConfig SET cold_cache = 0; + PRINT N'Cold-cache runs need permission to empty the buffer cache (' + ERROR_MESSAGE() + N'); continuing with warm-cache runs.'; + END CATCH; + END; + + IF @rollback = 1 BEGIN TRANSACTION; + + -- cpu_time, logical_reads and reads of this session's running request grow while the batch runs; the difference is the cost of the call + SELECT @cpu0 = cpu_time, @reads0 = logical_reads, @phys0 = reads FROM sys.dm_exec_requests WHERE session_id = @@SPID; + SET @t0 = SYSDATETIME(); + BEGIN TRY + EXEC sys.sp_executesql @call; + END TRY + BEGIN CATCH + SET @err = LEFT(ERROR_MESSAGE(), 2000); + END CATCH; + SET @t1 = SYSDATETIME(); + SELECT @cpu1 = cpu_time, @reads1 = logical_reads, @phys1 = reads FROM sys.dm_exec_requests WHERE session_id = @@SPID; + + IF @rollback = 1 AND @@TRANCOUNT > 0 ROLLBACK TRANSACTION; + + INSERT #BenchRuns (case_id, variant, run_no, is_warmup, elapsed_ms, cpu_ms, logical_reads, physical_reads, error_message) + VALUES (@case_id, @variant, @run, CASE WHEN @run <= @warmup THEN 1 ELSE 0 END, + DATEDIFF_BIG(MICROSECOND, @t0, @t1) / 1000.0, @cpu1 - @cpu0, @reads1 - @reads0, @phys1 - @phys0, @err); +END; +GO +-- The loop. Before / After: one version after the other. Interleaved: both versions in every run, in alternating order (ABBA). +CREATE PROCEDURE #BenchRun @mode varchar(11) +AS +BEGIN + SET NOCOUNT ON; + + DECLARE @runs int, @warmup int, @rollback bit; + SELECT @runs = runs, @warmup = warmup_runs, @rollback = rollback_each_run FROM #BenchConfig; + + DECLARE @case_id int, @before nvarchar(max), @after nvarchar(max), @run int, @err_before nvarchar(2000), @err_after nvarchar(2000); + + DECLARE bench_cases CURSOR LOCAL FAST_FORWARD FOR + SELECT case_id, + N'EXEC ' + original_call_name + ISNULL(N' ' + arguments, N''), + N'EXEC ' + optimized_call_name + ISNULL(N' ' + arguments, N'') + FROM #BenchCases + ORDER BY case_id; + OPEN bench_cases; + FETCH NEXT FROM bench_cases INTO @case_id, @before, @after; + + WHILE @@FETCH_STATUS = 0 + BEGIN + SET @run = 1; + WHILE @run <= @warmup + @runs + BEGIN + SELECT @err_before = NULL, @err_after = NULL; + + IF @mode = 'Before' + BEGIN + EXEC #BenchOne @case_id, 'Before', @before, @run, @warmup, @rollback, @err_before OUTPUT; + END + ELSE IF @mode = 'After' + BEGIN + EXEC #BenchOne @case_id, 'After', @after, @run, @warmup, @rollback, @err_after OUTPUT; + END + ELSE IF @run % 2 = 1 + BEGIN + EXEC #BenchOne @case_id, 'Before', @before, @run, @warmup, @rollback, @err_before OUTPUT; + EXEC #BenchOne @case_id, 'After', @after, @run, @warmup, @rollback, @err_after OUTPUT; + END + ELSE + BEGIN + EXEC #BenchOne @case_id, 'After', @after, @run, @warmup, @rollback, @err_after OUTPUT; + EXEC #BenchOne @case_id, 'Before', @before, @run, @warmup, @rollback, @err_before OUTPUT; + END; + + IF @err_before IS NOT NULL OR @err_after IS NOT NULL BREAK; -- a failing procedure fails every time: record it once and move on + SET @run += 1; + END; + + FETCH NEXT FROM bench_cases INTO @case_id, @before, @after; + END; + + CLOSE bench_cases; + DEALLOCATE bench_cases; +END; +"; + + private const string VerifyProcedure = @"-- ============================================================================= +-- The result comparison (temporary procedure): same rows, same order? +-- ============================================================================= +IF OBJECT_ID(N'tempdb..#BenchVerify') IS NOT NULL DROP PROCEDURE #BenchVerify; +GO +CREATE PROCEDURE #BenchVerify +AS +BEGIN + SET NOCOUNT ON; + + DECLARE @rollback bit; + SELECT @rollback = rollback_each_run FROM #BenchConfig; + + DECLARE @case_id int, @before nvarchar(max), @after nvarchar(max), @problem nvarchar(1000), @has_result bit, + @columns nvarchar(max), @names nvarchar(max), @a_names nvarchar(max), @b_names nvarchar(max), @sql nvarchar(max), + @rows_a int, @rows_b int, @diff int, @order_diff int, @status nvarchar(40), @detail nvarchar(1000); + + DECLARE bench_pairs CURSOR LOCAL FAST_FORWARD FOR + SELECT case_id, + N'EXEC ' + original_call_name + ISNULL(N' ' + arguments, N''), + N'EXEC ' + optimized_call_name + ISNULL(N' ' + arguments, N'') + FROM #BenchCases + ORDER BY case_id; + OPEN bench_pairs; + FETCH NEXT FROM bench_pairs INTO @case_id, @before, @after; + + WHILE @@FETCH_STATUS = 0 + BEGIN + SELECT @status = NULL, @detail = NULL, @problem = NULL, @has_result = 0, @columns = NULL, @names = NULL, @a_names = NULL, @b_names = NULL, + @rows_a = NULL, @rows_b = NULL, @diff = NULL, @order_diff = NULL; + + -- the shape of the first result set of the original procedure + SELECT TOP (1) @problem = CASE WHEN error_message IS NOT NULL THEN error_message + WHEN name IS NULL THEN N'a result column has no name, so the rows cannot be compared' + ELSE N'a result column has a type that cannot be compared' END + FROM sys.dm_exec_describe_first_result_set(@before, NULL, 0) + WHERE ISNULL(is_hidden, 0) = 0 AND (error_message IS NOT NULL OR name IS NULL OR system_type_name IS NULL); + + IF @problem IS NULL AND EXISTS (SELECT 1 FROM sys.dm_exec_describe_first_result_set(@before, NULL, 0) WHERE ISNULL(is_hidden, 0) = 0) + SET @has_result = 1; + + IF @problem IS NOT NULL + BEGIN + SELECT @status = N'NOT CHECKED', @detail = LEFT(@problem, 1000); + END + ELSE IF @has_result = 0 + BEGIN + SELECT @status = N'NO RESULT SET', @detail = N'The procedure returns no result set, so there are no rows to compare.'; + END + ELSE + BEGIN + SELECT @columns = STUFF((SELECT N', ' + QUOTENAME(name) + N' ' + system_type_name + N' NULL' + FROM sys.dm_exec_describe_first_result_set(@before, NULL, 0) + WHERE ISNULL(is_hidden, 0) = 0 ORDER BY column_ordinal + FOR XML PATH(N''), TYPE).value(N'.', N'nvarchar(max)'), 1, 2, N''), + @names = STUFF((SELECT N', ' + QUOTENAME(name) + FROM sys.dm_exec_describe_first_result_set(@before, NULL, 0) + WHERE ISNULL(is_hidden, 0) = 0 ORDER BY column_ordinal + FOR XML PATH(N''), TYPE).value(N'.', N'nvarchar(max)'), 1, 2, N''), + @a_names = STUFF((SELECT N', a.' + QUOTENAME(name) + FROM sys.dm_exec_describe_first_result_set(@before, NULL, 0) + WHERE ISNULL(is_hidden, 0) = 0 ORDER BY column_ordinal + FOR XML PATH(N''), TYPE).value(N'.', N'nvarchar(max)'), 1, 2, N''), + @b_names = STUFF((SELECT N', b.' + QUOTENAME(name) + FROM sys.dm_exec_describe_first_result_set(@before, NULL, 0) + WHERE ISNULL(is_hidden, 0) = 0 ORDER BY column_ordinal + FOR XML PATH(N''), TYPE).value(N'.', N'nvarchar(max)'), 1, 2, N''); + + SET @sql = N' +DECLARE @A TABLE (__seq int IDENTITY(1,1), ' + @columns + N'); +DECLARE @B TABLE (__seq int IDENTITY(1,1), ' + @columns + N'); +' + CASE WHEN @rollback = 1 THEN N'BEGIN TRANSACTION;' ELSE N'' END + N' +INSERT @A (' + @names + N') ' + @before + N'; +' + CASE WHEN @rollback = 1 THEN N'ROLLBACK TRANSACTION; +BEGIN TRANSACTION;' ELSE N'' END + N' +INSERT @B (' + @names + N') ' + @after + N'; +' + CASE WHEN @rollback = 1 THEN N'ROLLBACK TRANSACTION;' ELSE N'' END + N' +SELECT @rows_a = COUNT(*) FROM @A; +SELECT @rows_b = COUNT(*) FROM @B; +WITH ga AS (SELECT ' + @names + N', COUNT_BIG(*) AS __n FROM @A GROUP BY ' + @names + N'), + gb AS (SELECT ' + @names + N', COUNT_BIG(*) AS __n FROM @B GROUP BY ' + @names + N') +SELECT @diff = COUNT(*) FROM ( + (SELECT * FROM ga EXCEPT SELECT * FROM gb) + UNION ALL + (SELECT * FROM gb EXCEPT SELECT * FROM ga)) AS d; +SELECT @order_diff = COUNT(*) FROM @A AS a JOIN @B AS b ON b.__seq = a.__seq +WHERE EXISTS (SELECT ' + @a_names + N' EXCEPT SELECT ' + @b_names + N');'; + + BEGIN TRY + EXEC sys.sp_executesql @sql, + N'@rows_a int OUTPUT, @rows_b int OUTPUT, @diff int OUTPUT, @order_diff int OUTPUT', + @rows_a OUTPUT, @rows_b OUTPUT, @diff OUTPUT, @order_diff OUTPUT; + + SELECT @status = CASE WHEN @diff > 0 THEN N'DIFFERENT RESULTS' + WHEN @order_diff > 0 THEN N'SAME ROWS, DIFFERENT ORDER' + ELSE N'IDENTICAL' END, + @detail = CASE WHEN @diff > 0 THEN N'The versions return different rows. Check float/real columns and anything that depends on the time or on random values.' + WHEN @order_diff > 0 THEN N'Same rows in a different order. Fine when the original has no ORDER BY; otherwise the optimized copy must sort the same way.' + ELSE N'Same rows in the same order.' END; + END TRY + BEGIN CATCH + IF @rollback = 1 AND @@TRANCOUNT > 0 ROLLBACK TRANSACTION; + SELECT @status = N'NOT CHECKED', @detail = LEFT(ERROR_MESSAGE(), 1000); + END CATCH; + END; + + INSERT #BenchCheck (case_id, status, rows_before, rows_after, differing_rows, detail) + VALUES (@case_id, @status, @rows_a, @rows_b, @diff, @detail); + + FETCH NEXT FROM bench_pairs INTO @case_id, @before, @after; + END; + + CLOSE bench_pairs; + DEALLOCATE bench_pairs; +END; +"; + + private const string Report = @"-- ============================================================================= +-- REPORT +-- ============================================================================= +SET NOCOUNT ON; + +SELECT r.case_id, r.variant, + COUNT(*) AS n, + AVG(r.elapsed_ms) AS mean_ms, + ISNULL(STDEV(r.elapsed_ms), 0) AS sd_ms, + MIN(r.elapsed_ms) AS min_ms, + MAX(r.elapsed_ms) AS max_ms, + AVG(r.cpu_ms) AS mean_cpu_ms, + AVG(CAST(r.logical_reads AS float)) AS mean_reads, + AVG(CAST(r.physical_reads AS float)) AS mean_phys_reads +INTO #BenchStats +FROM #BenchRuns AS r +WHERE r.is_warmup = 0 AND r.error_message IS NULL +GROUP BY r.case_id, r.variant; + +SELECT DISTINCT case_id, variant, + PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY elapsed_ms) OVER (PARTITION BY case_id, variant) AS median_ms +INTO #BenchMedian +FROM #BenchRuns +WHERE is_warmup = 0 AND error_message IS NULL; + +-- RESULT: one row per procedure +SELECT c.case_id AS [#], + c.display_name AS [Procedure], + b.n AS [Timed runs], + CAST(b.mean_ms AS decimal(18,2)) AS [Before (ms)], + CAST(a.mean_ms AS decimal(18,2)) AS [After (ms)], + CAST(100.0 * x.diff / NULLIF(b.mean_ms, 0) AS decimal(9,1)) AS [Time saved %], + CAST(b.mean_ms / NULLIF(a.mean_ms, 0) AS decimal(9,2)) AS [Speed-up (x)], + CAST(b.mean_reads AS decimal(18,1)) AS [Before reads], + CAST(a.mean_reads AS decimal(18,1)) AS [After reads], + CAST(100.0 * (b.mean_reads - a.mean_reads) / NULLIF(b.mean_reads, 0) AS decimal(9,1)) AS [Reads saved %], + CAST(b.mean_cpu_ms AS decimal(18,2)) AS [Before CPU (ms)], + CAST(a.mean_cpu_ms AS decimal(18,2)) AS [After CPU (ms)], + CAST(y.t AS decimal(9,2)) AS [t], + CAST(x.diff - 1.96 * x.se AS decimal(18,2)) AS [95% CI low (ms)], + CAST(x.diff + 1.96 * x.se AS decimal(18,2)) AS [95% CI high (ms)], + CASE WHEN b.n IS NULL OR a.n IS NULL THEN N'NOT COMPARABLE - a run failed (see ERRORS)' + WHEN k.status = N'DIFFERENT RESULTS' THEN N'DO NOT USE - the results differ' + WHEN x.se = 0 AND x.diff = 0 THEN N'NO DIFFERENCE' + WHEN (ABS(y.t) >= 2 OR (x.se = 0 AND x.diff <> 0)) AND ABS(100.0 * x.diff / NULLIF(b.mean_ms, 0)) >= 5 + THEN CASE WHEN x.diff > 0 THEN N'FASTER' ELSE N'SLOWER' END + WHEN ABS(y.t) >= 2 OR (x.se = 0 AND x.diff <> 0) + THEN N'NO MEANINGFUL DIFFERENCE (under 5%)' + ELSE N'NO SIGNIFICANT DIFFERENCE' END AS [Verdict], + ISNULL(k.status, N'not checked') AS [Same results?] +FROM #BenchCases AS c +LEFT JOIN #BenchStats AS b ON b.case_id = c.case_id AND b.variant = 'Before' +LEFT JOIN #BenchStats AS a ON a.case_id = c.case_id AND a.variant = 'After' +LEFT JOIN #BenchCheck AS k ON k.case_id = c.case_id +CROSS APPLY (SELECT diff = b.mean_ms - a.mean_ms, + se = SQRT(POWER(b.sd_ms, 2) / NULLIF(b.n, 0) + POWER(a.sd_ms, 2) / NULLIF(a.n, 0))) AS x +CROSS APPLY (SELECT t = x.diff / NULLIF(x.se, 0)) AS y +ORDER BY c.case_id; + +-- OVERALL: the cost of running every compared procedure once +SELECT COUNT(*) AS [Procedures compared], + CAST(SUM(b.mean_ms) AS decimal(18,2)) AS [Before total (ms)], + CAST(SUM(a.mean_ms) AS decimal(18,2)) AS [After total (ms)], + CAST(100.0 * (SUM(b.mean_ms) - SUM(a.mean_ms)) / NULLIF(SUM(b.mean_ms), 0) AS decimal(9,1)) AS [Time saved %], + CAST(SUM(b.mean_ms) / NULLIF(SUM(a.mean_ms), 0) AS decimal(9,2)) AS [Speed-up (x)], + CAST(SUM(b.mean_reads) AS decimal(18,1)) AS [Before reads], + CAST(SUM(a.mean_reads) AS decimal(18,1)) AS [After reads], + CAST(100.0 * (SUM(b.mean_reads) - SUM(a.mean_reads)) / NULLIF(SUM(b.mean_reads), 0) AS decimal(9,1)) AS [Reads saved %] +FROM #BenchStats AS b +JOIN #BenchStats AS a ON a.case_id = b.case_id AND a.variant = 'After' +WHERE b.variant = 'Before'; + +-- STATISTICS: the numbers behind the result +SELECT c.case_id AS [#], c.display_name AS [Procedure], s.variant AS [Variant], s.n AS [n], + CAST(s.mean_ms AS decimal(18,3)) AS [Mean (ms)], + CAST(m.median_ms AS decimal(18,3)) AS [Median (ms)], + CAST(s.sd_ms AS decimal(18,3)) AS [SD (ms)], + CAST(s.min_ms AS decimal(18,3)) AS [Min (ms)], + CAST(s.max_ms AS decimal(18,3)) AS [Max (ms)], + CAST(s.mean_cpu_ms AS decimal(18,2)) AS [Mean CPU (ms)], + CAST(s.mean_reads AS decimal(18,1)) AS [Mean logical reads], + CAST(s.mean_phys_reads AS decimal(18,1)) AS [Mean physical reads] +FROM #BenchStats AS s +JOIN #BenchMedian AS m ON m.case_id = s.case_id AND m.variant = s.variant +JOIN #BenchCases AS c ON c.case_id = s.case_id +ORDER BY c.case_id, CASE s.variant WHEN 'Before' THEN 0 ELSE 1 END; + +-- RESULT CHECK: does the optimized copy return the same rows as the original? +SELECT c.case_id AS [#], c.display_name AS [Procedure], k.status AS [Status], k.rows_before AS [Rows before], k.rows_after AS [Rows after], + k.differing_rows AS [Differing row groups], k.detail AS [Detail] +FROM #BenchCheck AS k +JOIN #BenchCases AS c ON c.case_id = k.case_id +ORDER BY c.case_id; + +-- ERRORS: only when a run failed +IF EXISTS (SELECT 1 FROM #BenchRuns WHERE error_message IS NOT NULL) + SELECT c.case_id AS [#], c.display_name AS [Procedure], r.variant AS [Variant], r.run_no AS [Run], r.error_message AS [Error] + FROM #BenchRuns AS r + JOIN #BenchCases AS c ON c.case_id = r.case_id + WHERE r.error_message IS NOT NULL + ORDER BY c.case_id, r.variant, r.run_no; + +-- RAW: every measured run, including the warm-up runs +SELECT c.case_id AS [#], c.display_name AS [Procedure], r.variant AS [Variant], r.run_no AS [Run], + CASE r.is_warmup WHEN 1 THEN N'warm-up' ELSE N'timed' END AS [Kind], + CAST(r.elapsed_ms AS decimal(18,3)) AS [Elapsed (ms)], CAST(r.cpu_ms AS decimal(18,0)) AS [CPU (ms)], + r.logical_reads AS [Logical reads], r.physical_reads AS [Physical reads] +FROM #BenchRuns AS r +JOIN #BenchCases AS c ON c.case_id = r.case_id +ORDER BY c.case_id, r.variant DESC, r.run_no; + +PRINT N'Done. Start with the RESULT grid (first one); the formulas are explained at the top of this script.'; +"; + } +} diff --git a/src/SsmsChatExtension/Database/ProcedureTuneRequest.cs b/src/SsmsChatExtension/Database/ProcedureTuneRequest.cs new file mode 100644 index 0000000..9f44ea4 --- /dev/null +++ b/src/SsmsChatExtension/Database/ProcedureTuneRequest.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; +using System.Text; + +namespace SsmsChatExtension.Database +{ + /// What the user picked in the "Tune procedures" dialog, and the chat message that asks Claude to act on it. + internal sealed class ProcedureTuneRequest + { + public ProcedureTuneRequest(string database, IReadOnlyList procedures, int runs, bool rollbackEachRun) + { + Database = database; + Procedures = procedures; + Runs = runs; + RollbackEachRun = rollbackEachRun; + } + + public string Database { get; } + + public IReadOnlyList Procedures { get; } + + public int Runs { get; } + + public bool RollbackEachRun { get; } + + /// + /// The message that goes into the chat. The method (what to look for, how to write the copies) is in the system prompt; + /// this names the procedures and the benchmark settings, and carries what the plan cache already knows about their cost. + /// + public string BuildMessage() + { + var text = new StringBuilder(); + text.Append("Tune ").Append(Procedures.Count == 1 ? "this stored procedure" : "these " + Procedures.Count + " stored procedures") + .Append(" in database ").Append(SqlObjectName.QuoteName(Database)).AppendLine(" for performance and prove the improvement with a benchmark:"); + foreach (var procedure in Procedures) + { + text.Append("- ").Append(procedure.Name.Quoted); + if (procedure.StatsText.Length > 0) text.Append(" (").Append(procedure.StatsText).Append(" since its plan was cached)"); + text.AppendLine(); + } + text.AppendLine(); + text.AppendLine("Follow the stored procedure tuning workflow: read each one, find what makes it slow, write optimized copies that return exactly the same results, " + + "then call create_benchmark_plan once for all of them so the before / after test plan opens in a new query window."); + text.Append("Benchmark settings: ").Append(Runs).Append(" timed runs and ").Append(BenchmarkPlan.DefaultWarmupRuns).Append(" warm-up runs per procedure; ") + .Append(RollbackEachRun ? "every run is rolled back in a transaction (rollback_each_run = true)." : "runs are NOT rolled back (rollback_each_run = false)."); + return text.ToString(); + } + } +} diff --git a/src/SsmsChatExtension/Database/SqlObjectName.cs b/src/SsmsChatExtension/Database/SqlObjectName.cs new file mode 100644 index 0000000..5bc22af --- /dev/null +++ b/src/SsmsChatExtension/Database/SqlObjectName.cs @@ -0,0 +1,159 @@ +using System; +using System.Text; + +namespace SsmsChatExtension.Database +{ + /// + /// A one- or two-part SQL Server object name ([schema].[name]), parsed from text that may or may not use brackets and + /// quoted again safely, so a name can never close its bracket and add SQL of its own. + /// + internal sealed class SqlObjectName + { + private const int MaxPartLength = 128; + + private SqlObjectName(string schema, string name) + { + Schema = schema; + Name = name; + } + + public string Schema { get; } + + public string Name { get; } + + /// [schema].[name] + public string Quoted + { + get { return QuoteName(Schema) + "." + QuoteName(Name); } + } + + public override string ToString() + { + return Schema + "." + Name; + } + + public bool SameAs(SqlObjectName other) + { + return other != null + && string.Equals(Schema, other.Schema, StringComparison.OrdinalIgnoreCase) + && string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase); + } + + public static SqlObjectName FromParts(string schema, string name) + { + return new SqlObjectName(schema, name); + } + + /// QUOTENAME for identifiers: wraps in brackets and doubles any closing bracket. + public static string QuoteName(string identifier) + { + return "[" + (identifier ?? string.Empty).Replace("]", "]]") + "]"; + } + + /// Parses "name", "schema.name", "[schema].[name]"; the schema defaults to dbo. + public static SqlObjectName Parse(string text) + { + if (string.IsNullOrWhiteSpace(text)) throw new ArgumentException("An object name is required."); + + var position = 0; + var trimmed = text.Trim(); + SqlObjectName name; + if (!TryRead(trimmed, ref position, out name, out var problem) || position != trimmed.Length) + { + throw new ArgumentException(problem ?? ("'" + text.Trim() + "' is not a valid object name.")); + } + return name; + } + + /// + /// Reads a name starting at and moves it past the name. On failure returns false and + /// explains why in . + /// + public static bool TryRead(string text, ref int position, out SqlObjectName name, out string problem) + { + name = null; + problem = null; + + string first; + if (!TryReadPart(text, ref position, out first)) + { + problem = "'" + Preview(text, position) + "' is not a valid object name."; + return false; + } + + if (position < text.Length && text[position] == '.') + { + position++; + string second; + if (!TryReadPart(text, ref position, out second)) + { + problem = "'" + Preview(text, 0) + "' is not a valid object name."; + return false; + } + if (position < text.Length && text[position] == '.') + { + problem = "Use schema.name (two parts) for '" + Preview(text, 0) + "'; the database is given separately."; + return false; + } + name = new SqlObjectName(first, second); + } + else + { + name = new SqlObjectName("dbo", first); + } + return true; + } + + private static bool TryReadPart(string text, ref int position, out string part) + { + part = null; + if (position >= text.Length) return false; + + if (text[position] == '[') + { + var builder = new StringBuilder(); + var i = position + 1; + while (i < text.Length) + { + if (text[i] == ']') + { + if (i + 1 < text.Length && text[i + 1] == ']') + { + builder.Append(']'); + i += 2; + continue; + } + break; + } + if (char.IsControl(text[i])) return false; + builder.Append(text[i]); + i++; + } + if (i >= text.Length || builder.Length == 0 || builder.Length > MaxPartLength) return false; + + part = builder.ToString(); + position = i + 1; + return true; + } + + var start = position; + var end = start; + if (end < text.Length && (char.IsLetter(text[end]) || text[end] == '_')) + { + end++; + while (end < text.Length && (char.IsLetterOrDigit(text[end]) || text[end] == '_' || text[end] == '@' || text[end] == '#' || text[end] == '$')) end++; + } + if (end == start || end - start > MaxPartLength) return false; + + part = text.Substring(start, end - start); + position = end; + return true; + } + + private static string Preview(string text, int from) + { + var preview = text.Substring(Math.Min(from, text.Length)).Trim(); + return preview.Length > 60 ? preview.Substring(0, 60) + "..." : preview; + } + } +} diff --git a/src/SsmsChatExtension/Database/StoredProcedureCatalog.cs b/src/SsmsChatExtension/Database/StoredProcedureCatalog.cs new file mode 100644 index 0000000..37dcb7e --- /dev/null +++ b/src/SsmsChatExtension/Database/StoredProcedureCatalog.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Globalization; + +namespace SsmsChatExtension.Database +{ + /// A stored procedure of the current database, with what the plan cache knows about its cost. + internal sealed class ProcedureInfo + { + public ProcedureInfo(SqlObjectName name, bool isEncrypted) + { + Name = name; + IsEncrypted = isEncrypted; + } + + public SqlObjectName Name { get; } + + /// WITH ENCRYPTION: the text cannot be read, so it cannot be tuned. + public bool IsEncrypted { get; } + + /// Executions since the plan was cached; null when the statistics are not available. + public long? Executions { get; set; } + + public double? TotalElapsedMs { get; set; } + + public double? AvgElapsedMs + { + get { return Executions > 0 && TotalElapsedMs.HasValue ? TotalElapsedMs / Executions : null; } + } + + public double? AvgLogicalReads { get; set; } + + public string DisplayName + { + get { return Name.ToString(); } + } + + /// "1,204 runs, avg 340 ms, 18,200 reads/run", or an empty string when the procedure is not in the plan cache. + public string StatsText + { + get + { + if (IsEncrypted) return "encrypted"; + if (!(Executions > 0)) return string.Empty; + return Executions.Value.ToString("N0", CultureInfo.InvariantCulture) + " runs, avg " + + FormatMs(AvgElapsedMs ?? 0) + ", " + (AvgLogicalReads ?? 0).ToString("N0", CultureInfo.InvariantCulture) + " reads/run"; + } + } + + private static string FormatMs(double ms) + { + return ms >= 1000 ? (ms / 1000).ToString("0.##", CultureInfo.InvariantCulture) + " s" : ms.ToString("0.#", CultureInfo.InvariantCulture) + " ms"; + } + } + + /// Lists the T-SQL stored procedures of the connection's current database. + internal static class StoredProcedureCatalog + { + // Totals over every cached plan of the procedure. The DMV needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE + // on SQL Server 2022), so the list falls back to the plain catalog when the login lacks it. + private const string WithStatistics = + "SELECT s.name, p.name, CAST(CASE WHEN m.definition IS NULL THEN 1 ELSE 0 END AS bit), " + + " st.execution_count, st.total_elapsed_time, st.total_logical_reads " + + "FROM sys.procedures p " + + "JOIN sys.schemas s ON s.schema_id = p.schema_id " + + "JOIN sys.sql_modules m ON m.object_id = p.object_id " + + "OUTER APPLY (SELECT SUM(ps.execution_count) AS execution_count, SUM(ps.total_elapsed_time) AS total_elapsed_time, " + + " SUM(ps.total_logical_reads) AS total_logical_reads " + + " FROM sys.dm_exec_procedure_stats ps WHERE ps.object_id = p.object_id AND ps.database_id = DB_ID()) st " + + "WHERE p.is_ms_shipped = 0 AND p.type = 'P' " + + "ORDER BY s.name, p.name"; + + private const string WithoutStatistics = + "SELECT s.name, p.name, CAST(CASE WHEN m.definition IS NULL THEN 1 ELSE 0 END AS bit), " + + " CAST(NULL AS bigint), CAST(NULL AS bigint), CAST(NULL AS bigint) " + + "FROM sys.procedures p " + + "JOIN sys.schemas s ON s.schema_id = p.schema_id " + + "JOIN sys.sql_modules m ON m.object_id = p.object_id " + + "WHERE p.is_ms_shipped = 0 AND p.type = 'P' " + + "ORDER BY s.name, p.name"; + + /// The procedures of the connection's current database, ordered by schema and name. + public static IReadOnlyList List(IDbConnection connection, out bool hasStatistics) + { + try + { + var withStatistics = Read(connection, WithStatistics); + hasStatistics = true; + return withStatistics; + } + catch (DbException) + { + hasStatistics = false; + return Read(connection, WithoutStatistics); + } + } + + private static List Read(IDbConnection connection, string sql) + { + var result = new List(); + using (var command = connection.CreateCommand()) + { + command.CommandText = sql; // nosemgrep: csharp.lang.security.sqli.csharp-sqli.csharp-sqli - only ever one of the two constant catalog queries above + command.CommandTimeout = 60; + using (var reader = command.ExecuteReader()) + { + while (reader.Read()) + { + var info = new ProcedureInfo(SqlObjectName.FromParts(reader.GetString(0), reader.GetString(1)), reader.GetBoolean(2)); + if (!reader.IsDBNull(3) && reader.GetInt64(3) > 0) + { + info.Executions = reader.GetInt64(3); + info.TotalElapsedMs = reader.GetInt64(4) / 1000.0; + info.AvgLogicalReads = reader.GetInt64(5) / (double)info.Executions.Value; + } + result.Add(info); + } + } + } + return result; + } + } +} diff --git a/src/SsmsChatExtension/Mcp/McpHttpServer.cs b/src/SsmsChatExtension/Mcp/McpHttpServer.cs index 0b4518e..66b5c18 100644 --- a/src/SsmsChatExtension/Mcp/McpHttpServer.cs +++ b/src/SsmsChatExtension/Mcp/McpHttpServer.cs @@ -14,8 +14,8 @@ namespace SsmsChatExtension.Mcp { /// - /// Minimal MCP server (Streamable HTTP transport, JSON responses only) on 127.0.0.1 with a random port and a - /// random bearer token, so only the Claude Code process we start (which gets the token) can call it. + /// Minimal MCP server (Streamable HTTP transport, JSON responses only) on the loopback interface with a random port + /// and a random bearer token, so only the Claude Code process we start (which gets the token) can call it. /// A raw TcpListener is used instead of HttpListener because HttpListener needs a URL ACL without admin rights. /// internal sealed class McpHttpServer : IDisposable @@ -43,7 +43,8 @@ public McpHttpServer(IMcpToolProvider tools) public string Url { - get { return "http://127.0.0.1:" + Port + "/mcp"; } + // Loopback only, by design: the listener is bound to IPAddress.Loopback and every call needs the bearer token. + get { return "http://127.0.0.1:" + Port + "/mcp"; } // DevSkim: ignore DS162092 } public void Dispose() diff --git a/src/SsmsChatExtension/Mcp/SsmsMcpTools.cs b/src/SsmsChatExtension/Mcp/SsmsMcpTools.cs index 1d76b43..0542f88 100644 --- a/src/SsmsChatExtension/Mcp/SsmsMcpTools.cs +++ b/src/SsmsChatExtension/Mcp/SsmsMcpTools.cs @@ -39,7 +39,7 @@ public string Instructions return "Tools for the SQL Server Management Studio session the user is chatting from. " + "Database tools use the connection of the active query window. run_query is always read-only; " + "execute_sql and execute_query_window can change the database and may need the user's approval in SSMS. " - + "replace_query_window and open_new_query_window only change the editor text."; + + "replace_query_window, open_new_query_window and create_benchmark_plan only change the editor text."; } } @@ -173,7 +173,71 @@ public JArray ListTools() Tool("open_new_query_window", "Open a new SSMS query window (connected like the active one) containing the given script. It is not executed.", new JObject { ["text"] = Str("The script to put in the new window.") }, - "text") + "text"), + + Tool("create_benchmark_plan", + "Open a before / after performance test plan for stored procedures you optimized in a new SSMS query window. It is not executed: the user reads it and presses Execute. " + + "The script times each original procedure and its optimized copy over repeated runs (elapsed time, CPU time, logical reads), checks that both return the same rows, " + + "and reports time saved %, speed-up, a t statistic and a 95% confidence interval. The original procedures are never changed. " + + "Write each optimized copy under a NEW name ending in _Optimized, pick realistic call_arguments by looking at the data, and call this once for all procedures.", + new JObject + { + ["title"] = Str("Short title for the script header, for example 'Tune order procedures'."), + ["database"] = Str("Database name. Default: the connection's current database."), + ["procedures"] = new JObject + { + ["type"] = "array", + ["description"] = "The procedures to benchmark, at most " + BenchmarkPlan.MaxProcedures + ".", + ["items"] = new JObject + { + ["type"] = "object", + ["properties"] = new JObject + { + ["name"] = Str("The original procedure, schema-qualified, for example dbo.usp_PlaceOrder."), + ["optimized_name"] = Str("Name of the optimized copy; must end in _Optimized. Default: the original name plus _Optimized."), + ["optimized_definition"] = Str("The complete CREATE OR ALTER PROCEDURE statement that creates optimized_name: same parameters, same result sets and same side effects as the original. One batch, no GO lines."), + ["call_arguments"] = Str("The argument list of the EXEC used for both versions, for example @CustomerId = 42, @From = '2025-01-01'. Realistic values from the data. No comments or semicolons."), + ["changes"] = Str("What you changed and why, one short line per change; shown in the script header."), + ["modifies_data"] = new JObject { ["type"] = "boolean", ["description"] = "True when the procedure inserts, updates or deletes data." } + }, + ["required"] = new JArray("name", "optimized_definition") + } + }, + ["supporting_changes"] = new JObject + { + ["type"] = "array", + ["description"] = "Changes both versions depend on, such as an index. Applied between the BEFORE and AFTER runs, so their effect is measured. " + + "Write apply_sql and undo_sql so they can run twice (IF NOT EXISTS, CREATE ... WITH (DROP_EXISTING = ON), DROP ... IF EXISTS). " + + "An index also costs writes: add the procedures that write to the same table to the benchmark.", + ["items"] = new JObject + { + ["type"] = "object", + ["properties"] = new JObject + { + ["description"] = Str("What the change is and why."), + ["apply_sql"] = Str("T-SQL that applies it. GO lines are allowed."), + ["undo_sql"] = Str("T-SQL that reverses it; listed in the cleanup block of the script.") + }, + ["required"] = new JArray("description", "apply_sql") + } + }, + ["runs"] = new JObject + { + ["type"] = "integer", + ["description"] = "Timed runs per procedure and version, " + BenchmarkPlan.MinRuns + "-" + BenchmarkPlan.MaxRuns + ". Default " + BenchmarkPlan.DefaultRuns + "." + }, + ["warmup_runs"] = new JObject + { + ["type"] = "integer", + ["description"] = "Runs before the timed ones, 0-" + BenchmarkPlan.MaxWarmupRuns + ". Default " + BenchmarkPlan.DefaultWarmupRuns + "." + }, + ["rollback_each_run"] = new JObject + { + ["type"] = "boolean", + ["description"] = "Wrap every run in BEGIN TRAN ... ROLLBACK so data changes are undone. Default true." + } + }, + "procedures") }; } @@ -195,6 +259,7 @@ public async Task CallToolAsync(string name, JObject arguments, Cancella case "get_query_window": return await GetQueryWindowAsync(); case "replace_query_window": return await ReplaceQueryWindowAsync(arguments); case "open_new_query_window": return await OpenNewQueryWindowAsync(arguments); + case "create_benchmark_plan": return await CreateBenchmarkPlanAsync(arguments); default: throw new InvalidOperationException("Unknown tool: " + name); } } @@ -532,6 +597,103 @@ private static async Task OpenNewQueryWindowAsync(JObject arguments) return ok ? "Opened a new query window with the script." : "Could not open a new query window."; } + /// + /// Checks the procedures against the database, builds the benchmark script and opens it in a new query window. + /// Nothing is executed, so it needs no approval, like open_new_query_window. + /// + private async Task CreateBenchmarkPlanAsync(JObject arguments) + { + var request = new BenchmarkRequest + { + Title = (string)arguments["title"], + Database = (string)arguments["database"], + Runs = (int?)arguments["runs"] ?? BenchmarkPlan.DefaultRuns, + WarmupRuns = (int?)arguments["warmup_runs"] ?? BenchmarkPlan.DefaultWarmupRuns, + RollbackEachRun = (bool?)arguments["rollback_each_run"] ?? true + }; + foreach (var item in (arguments["procedures"] as JArray ?? new JArray()).OfType()) + { + request.Procedures.Add(new BenchmarkProcedure + { + Name = (string)item["name"], + OptimizedName = (string)item["optimized_name"], + OptimizedDefinition = (string)item["optimized_definition"], + Arguments = (string)item["call_arguments"], + Changes = (string)item["changes"], + ModifiesData = (bool?)item["modifies_data"] ?? false + }); + } + foreach (var item in (arguments["supporting_changes"] as JArray ?? new JArray()).OfType()) + { + request.SupportingChanges.Add(new BenchmarkChange + { + Description = (string)item["description"], + ApplySql = (string)item["apply_sql"], + UndoSql = (string)item["undo_sql"] + }); + } + + var connection = await GetConnectionAsync(); + var script = await WithConnectionAsync(connection, request.Database, c => + { + request.Database = c.Database; + CheckProcedures(c, request); + return BenchmarkPlan.Build(request, DateTime.Now); + }); + + if (!await SsmsEditor.OpenNewQueryAsync(script)) + { + throw new InvalidOperationException("Could not open a new query window. Give the user this script in a sql code block instead:" + Environment.NewLine + script); + } + + var replaced = request.Procedures.Where(p => p.OptimizedExists) + .Select(p => string.IsNullOrWhiteSpace(p.OptimizedName) ? p.Name + BenchmarkPlan.OptimizedSuffix : p.OptimizedName) + .ToList(); + return "Opened the benchmark plan for " + request.Procedures.Count + (request.Procedures.Count == 1 ? " procedure" : " procedures") + + " in " + request.Database + " in a new query window (" + script.Split('\n').Length + " lines). Nothing was executed. " + + "Each procedure runs " + request.WarmupRuns + " warm-up + " + request.Runs + " timed times before and after, " + + (request.RollbackEachRun ? "every run rolled back in a transaction." : "with the runs NOT rolled back.") + + (replaced.Count > 0 ? " It replaces the existing " + string.Join(", ", replaced) + "." : string.Empty) + + " Tell the user to switch on Query Options > Results > Grid > 'Discard results after execution', read the script and press Execute; " + + "the first result grid shows time saved %, speed-up, the t statistic, the 95% confidence interval and whether the results match. " + + "The cleanup block at the end of the script removes the _Optimized copies and the supporting changes. You have not measured anything yourself, so do not quote numbers."; + } + + /// Every original must exist as a stored procedure; remembers which optimized copies already exist (the plan replaces them). + private static void CheckProcedures(IDbConnection connection, BenchmarkRequest request) + { + foreach (var procedure in request.Procedures) + { + SqlObjectName original; + try + { + original = SqlObjectName.Parse(procedure.Name); + } + catch (ArgumentException) + { + continue; // Build reports the bad name + } + if (ExecuteScalar(connection, ProcedureExists, original.Quoted) == null) + { + throw new ArgumentException("Stored procedure " + original + " was not found in " + request.Database + ". Check the name with list_objects."); + } + + try + { + var optimized = string.IsNullOrWhiteSpace(procedure.OptimizedName) + ? SqlObjectName.FromParts(original.Schema, original.Name + BenchmarkPlan.OptimizedSuffix) + : SqlObjectName.Parse(procedure.OptimizedName); + procedure.OptimizedExists = ExecuteScalar(connection, ProcedureExists, optimized.Quoted) != null; + } + catch (ArgumentException) + { + // Build reports the bad name + } + } + } + + private const string ProcedureExists = "SELECT CASE WHEN OBJECT_ID(@name, N'P') IS NULL THEN NULL ELSE 1 END"; + // ---- schema helpers ---- private static JObject Tool(string name, string description, JObject properties, params string[] required) diff --git a/src/SsmsChatExtension/Services/Claude/ClaudeCodeChatService.cs b/src/SsmsChatExtension/Services/Claude/ClaudeCodeChatService.cs index 05056af..141cbe3 100644 --- a/src/SsmsChatExtension/Services/Claude/ClaudeCodeChatService.cs +++ b/src/SsmsChatExtension/Services/Claude/ClaudeCodeChatService.cs @@ -39,6 +39,15 @@ public sealed class ClaudeCodeChatService : IChatService "Stored procedures: to explain one, read it with describe_object and get_dependencies first and walk through its logic, parameters and side effects. " + "To write or change one, use CREATE OR ALTER, follow the existing naming and style, show the script, and deploy it with execute_sql only when the user asks. " + "To run one, call execute_sql with EXEC and the parameters, and say beforehand what it will change. " + + "Tuning stored procedures for performance (the user asks, or presses the Tune procedures button, which names the procedures): for each one read it with describe_object and get_dependencies, " + + "then gather evidence with run_query (row counts and indexes of the tables it touches, sys.dm_exec_procedure_stats, Query Store views when enabled, missing-index DMVs) and look for the usual causes: " + + "row-by-row cursors and loops, scalar or multi-statement functions inside queries, non-sargable predicates (functions on columns, implicit conversions, leading wildcards), SELECT *, missing or unused indexes, " + + "parameter sniffing, the same table read repeatedly, correlated subqueries, needless DISTINCT or sorting, large table variables, long transactions and dynamic SQL built by concatenation. " + + "An optimized copy must keep the parameters and return exactly the same columns, rows and side effects. Never change the original: write each copy as CREATE OR ALTER PROCEDURE _Optimized, " + + "put any index or statistics change in supporting_changes (re-runnable, with undo_sql), and call create_benchmark_plan once for all the procedures; it opens the before / after test plan in a new query window and the user runs it. " + + "Choose call_arguments from real values you looked up, never placeholders, and add a procedure that writes to the same table when you add an index (indexes cost writes). " + + "You cannot run procedures yourself, so you have measured nothing: never quote timings, reads or percentages as results; describe the expected effect in words and let the plan's numbers decide. " + + "After the tool call summarise per procedure what you changed and why, say which result grid to read first, and offer to deploy a copy over the original with execute_sql when the numbers and the result check are good. " + "Diagrams: when the user asks for a flow diagram, ER diagram or other picture of database objects, write Mermaid syntax and call show_diagram, which opens it in their browser. " + "Use flowchart TD for procedure logic (decisions as diamond nodes, one node per main step), erDiagram for tables and foreign keys, sequenceDiagram for procedure call chains. " + "Base diagrams on the real definitions you looked up, keep labels short and wrap labels with special characters in double quotes. " + @@ -406,7 +415,7 @@ private static string DescribeToolUse(string name, JObject input) : name; var detail = (string)input?["file_path"] ?? (string)input?["path"] ?? (string)input?["object"] - ?? (string)input?["pattern"] ?? (string)input?["command"] ?? (string)input?["sql"] ?? (string)input?["url"]; + ?? (string)input?["pattern"] ?? (string)input?["command"] ?? (string)input?["sql"] ?? (string)input?["url"] ?? (string)input?["title"]; if (string.IsNullOrWhiteSpace(detail)) return label; detail = detail.Trim().Replace("\r", " ").Replace("\n", " "); diff --git a/src/SsmsChatExtension/Services/Claude/ClaudeSettings.cs b/src/SsmsChatExtension/Services/Claude/ClaudeSettings.cs index f601abe..fb5cacf 100644 --- a/src/SsmsChatExtension/Services/Claude/ClaudeSettings.cs +++ b/src/SsmsChatExtension/Services/Claude/ClaudeSettings.cs @@ -107,7 +107,9 @@ public static ClaudeSettings Load() { if (File.Exists(FilePath)) { - return JsonConvert.DeserializeObject(File.ReadAllText(FilePath)) ?? new ClaudeSettings(); + var settings = JsonConvert.DeserializeObject(File.ReadAllText(FilePath)) ?? new ClaudeSettings(); + settings.UpgradeProtection(); + return settings; } } catch @@ -123,10 +125,14 @@ public void Save() File.WriteAllText(FilePath, JsonConvert.SerializeObject(this, Formatting.Indented)); } + // Application-specific entropy: without it, any other program running as the same Windows user can call + // ProtectedData.Unprotect on the stored value. Not a secret (DPAPI's user key is), it just binds the blob to this extension. + private static readonly byte[] Entropy = Encoding.UTF8.GetBytes("SsmsChatExtension.ConnectionString.v1"); + private static string Protect(string value) { if (string.IsNullOrEmpty(value)) return string.Empty; - var bytes = ProtectedData.Protect(Encoding.UTF8.GetBytes(value), null, DataProtectionScope.CurrentUser); + var bytes = ProtectedData.Protect(Encoding.UTF8.GetBytes(value), Entropy, DataProtectionScope.CurrentUser); return Convert.ToBase64String(bytes); } @@ -135,13 +141,42 @@ private static string Unprotect(string value) if (string.IsNullOrEmpty(value)) return string.Empty; try { - var bytes = ProtectedData.Unprotect(Convert.FromBase64String(value), null, DataProtectionScope.CurrentUser); - return Encoding.UTF8.GetString(bytes); + var bytes = Convert.FromBase64String(value); + try + { + return Encoding.UTF8.GetString(ProtectedData.Unprotect(bytes, Entropy, DataProtectionScope.CurrentUser)); + } + catch (CryptographicException) + { + // Written by 1.4.x and earlier, which protected it without entropy; Load() re-protects it. + return Encoding.UTF8.GetString(ProtectedData.Unprotect(bytes, null, DataProtectionScope.CurrentUser)); + } } catch { return string.Empty; } } + + /// Re-protects a connection string saved without entropy (an earlier version) and saves the file. + private void UpgradeProtection() + { + if (string.IsNullOrEmpty(ProtectedConnectionString)) return; + try + { + ProtectedData.Unprotect(Convert.FromBase64String(ProtectedConnectionString), Entropy, DataProtectionScope.CurrentUser); + } + catch (CryptographicException) + { + var plain = Unprotect(ProtectedConnectionString); + if (plain.Length == 0) return; + ConnectionStringOverride = plain; + try { Save(); } catch (IOException) { /* read-only profile: it is upgraded next time */ } + } + catch (FormatException) + { + // Not base64: nothing to upgrade. + } + } } } diff --git a/src/SsmsChatExtension/SsmsChatExtension.csproj b/src/SsmsChatExtension/SsmsChatExtension.csproj index 812db8a..db7532c 100644 --- a/src/SsmsChatExtension/SsmsChatExtension.csproj +++ b/src/SsmsChatExtension/SsmsChatExtension.csproj @@ -60,10 +60,14 @@ 4 + + + + @@ -96,6 +100,9 @@ + + ProcedureTuneDialog.xaml + @@ -121,6 +128,10 @@ + + @@ -131,6 +142,10 @@ MSBuild:Compile Designer + + MSBuild:Compile + Designer + MSBuild:Compile Designer diff --git a/src/SsmsChatExtension/UI/ChatControl.xaml b/src/SsmsChatExtension/UI/ChatControl.xaml index 490ce4a..d8207e3 100644 --- a/src/SsmsChatExtension/UI/ChatControl.xaml +++ b/src/SsmsChatExtension/UI/ChatControl.xaml @@ -518,6 +518,8 @@ ToolTip="New chat (the current one stays in History)" />