1+ using System ;
2+ using System . Diagnostics ;
3+ using System . IO ;
4+ using System . Net ;
5+ using System . Net . Sockets ;
6+ using System . Threading ;
7+
8+ namespace HdrHistogram . Examples
9+ {
10+ /// <summary>
11+ /// A simple example of using HdrHistogram's Recorder: run for 20 seconds collecting the
12+ /// time it takes to perform a simple Datagram Socket create/close operation,
13+ /// and report a histogram of the times at the end.
14+ /// </summary>
15+ static class RecorderExample
16+ {
17+ private static readonly Recorder Recorder = new Recorder ( 1 , TimeSpan . TicksPerHour , 3 , ( id , low , high , sf ) => new LongHistogram ( id , low , high , sf ) ) ;
18+ private static readonly Lazy < AddressFamily > AddressFamily = new Lazy < AddressFamily > ( ( ) => GetAddressFamily ( "google.com" ) ) ;
19+ private static readonly TimeSpan RunPeriod = TimeSpan . FromSeconds ( 10 ) ;
20+ private static readonly LongHistogram AccumulatingHistogram = new LongHistogram ( TimeSpan . TicksPerHour , 3 ) ;
21+ private const string LogPath = "DatagramSocket.histogram.log" ;
22+ private static HistogramLogWriter _logWriter ;
23+ private static FileStream _outputStream ;
24+ private static int _isCompleted = 0 ;
25+
26+ public static void Run ( )
27+ {
28+ Console . WriteLine ( $ "Running for { RunPeriod . TotalSeconds } sec.") ;
29+
30+ _outputStream = File . Create ( LogPath ) ;
31+ _logWriter = new HistogramLogWriter ( _outputStream ) ;
32+ //Write the headers, but no histograms (as we don't have any yet).
33+ _logWriter . Write ( DateTime . Now ) ;
34+
35+ var outputThread = new Thread ( ts => WriteToDisk ( ) ) ;
36+ outputThread . Start ( ) ;
37+ RecordMeasurements ( ) ;
38+ }
39+
40+ private static void WriteToDisk ( )
41+ {
42+ //Sample every second until flagged as completed.
43+ while ( _isCompleted == 0 )
44+ {
45+ Thread . Sleep ( 1000 ) ;
46+
47+ var histogram = Recorder . GetIntervalHistogram ( ) ;
48+ AccumulatingHistogram . Add ( histogram ) ;
49+ _logWriter . Append ( histogram ) ;
50+ Console . WriteLine ( $ "{ DateTime . Now : o} Interval.TotalCount = { histogram . TotalCount , 10 : G} . Accumulated.TotalCount = { AccumulatingHistogram . TotalCount , 10 : G} .") ;
51+ }
52+ _logWriter . Dispose ( ) ;
53+ _outputStream . Dispose ( ) ;
54+ Console . WriteLine ( "Log contents" ) ;
55+ Console . WriteLine ( File . ReadAllText ( LogPath ) ) ;
56+ Console . WriteLine ( "Output thread finishing." ) ;
57+ }
58+
59+ /// <summary>
60+ /// Shows a sample loop where an action is executed, and the latency of each execution is recorded.
61+ /// </summary>
62+ private static void RecordMeasurements ( )
63+ {
64+ var timer = Stopwatch . StartNew ( ) ;
65+ Action actionToMeasure = CreateAndCloseDatagramSocket ;
66+ do
67+ {
68+ Recorder . Record ( actionToMeasure ) ;
69+ } while ( timer . Elapsed < RunPeriod ) ;
70+ Interlocked . Increment ( ref _isCompleted ) ;
71+ }
72+
73+ private static void CreateAndCloseDatagramSocket ( )
74+ {
75+ try
76+ {
77+ using ( var socket = new Socket ( AddressFamily . Value , SocketType . Stream , ProtocolType . Tcp ) )
78+ {
79+ }
80+ }
81+ catch ( SocketException )
82+ {
83+ }
84+ }
85+
86+ private static AddressFamily GetAddressFamily ( string url )
87+ {
88+ var hostIpAddress = Dns . GetHostEntry ( url ) . AddressList [ 0 ] ;
89+ var hostIpEndPoint = new IPEndPoint ( hostIpAddress , 80 ) ;
90+ return hostIpEndPoint . AddressFamily ;
91+ }
92+ }
93+ }
0 commit comments