Created
July 19, 2019 13:04
-
-
Save smoothdeveloper/df7a43b26b045455f2c770a335f1ea18 to your computer and use it in GitHub Desktop.
a bit of F# code handling sql connection statistics
This file contains 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
open System | |
open System.Data.SqlClient | |
type EntryType = | |
| BuffersReceived | |
| BuffersSent | |
| BytesSent | |
| ConnectionTime | |
| CursorOpens | |
| IduCount | |
| IduRows | |
| NetworkServerTime | |
| PreparedExecs | |
| Prepares | |
| SelectCount | |
| SelectRows | |
| ServerRoundtrips | |
| SumResultSets | |
| Transactions | |
| Unkonwn of text: string | |
static member FromString = | |
function | |
| "BuffersReceived" -> BuffersReceived | |
| "BuffersSent" -> BuffersSent | |
| "BytesSent" -> BytesSent | |
| "ConnectionTime" -> ConnectionTime | |
| "CursorOpens" -> CursorOpens | |
| "IduCount" -> IduCount | |
| "IduRows" -> IduRows | |
| "NetworkServerTime" -> NetworkServerTime | |
| "PreparedExecs" -> PreparedExecs | |
| "Prepares" -> Prepares | |
| "SelectCount" -> SelectCount | |
| "SelectRows" -> SelectRows | |
| "ServerRoundtrips" -> ServerRoundtrips | |
| "SumResultSets" -> SumResultSets | |
| "Transactions" -> Transactions | |
| otherwise -> Unkonwn otherwise | |
type StatisticsEntry(entryType: EntryType, value: int64) = | |
struct | |
member x.EntryType = entryType | |
member x.Value = value | |
end | |
type ISqlConnectionStatisticsScope = | |
inherit IDisposable | |
abstract GetStatistics : unit -> StatisticsEntry array option | |
module SqlConnectionStatistics = | |
let enableStats (connection: SqlConnection) = connection.StatisticsEnabled <- true | |
let disableStats (connection: SqlConnection) = connection.StatisticsEnabled <- false | |
let retrieveStats (connection: SqlConnection) = | |
if connection.StatisticsEnabled then | |
let stats = connection.RetrieveStatistics() |> Seq.cast<System.Collections.DictionaryEntry> | |
Some [| for entry in stats -> StatisticsEntry(EntryType.FromString (string entry.Key), entry.Value :?> int64)|] | |
else | |
None | |
let mkRetrieveStatsScope connection = | |
enableStats connection | |
{ new ISqlConnectionStatisticsScope with | |
member x.GetStatistics () = retrieveStats connection | |
member x.Dispose () = disableStats connection | |
} | |
open System.Runtime.CompilerServices | |
[<AutoOpen>] | |
[<Extension>] | |
module SqlConnectionExtensions = | |
type SqlConnection with | |
member x.GetStatisticsArray() = SqlConnectionStatistics.retrieveStats x | |
member x.MakeRetrieveStatisticsScope() = SqlConnectionStatistics.mkRetrieveStatsScope x | |
let [<Extension>] GetStatisticsArray (connection: SqlConnection) = connection.GetStatisticsArray() | |
let [<Extension>] MakeRetrieveStatisticsScope (connection: SqlConnection) = connection.MakeRetrieveStatisticsScope() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment