File tree Expand file tree Collapse file tree 4 files changed +70
-1
lines changed
common/Perf/Azure.Test.Perf Expand file tree Collapse file tree 4 files changed +70
-1
lines changed Original file line number Diff line number Diff line change 1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+
4+ namespace Azure . Test . Perf
5+ {
6+ public static class RandomByteArray
7+ {
8+ public static byte [ ] Create ( int size )
9+ {
10+ var bytes = new byte [ size ] ;
11+ ThreadsafeRandom . NextBytes ( bytes ) ;
12+ return bytes ;
13+ }
14+ }
15+ }
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public static class RandomStream
1111 private static readonly Lazy < byte [ ] > _randomBytes = new Lazy < byte [ ] > ( ( ) =>
1212 {
1313 var randomData = new byte [ 1024 * 1024 ] ;
14- ( new Random ( 0 ) ) . NextBytes ( randomData ) ;
14+ ThreadsafeRandom . NextBytes ( randomData ) ;
1515 return randomData ;
1616 } ) ;
1717
Original file line number Diff line number Diff line change 1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+
4+ namespace Azure . Test . Perf
5+ {
6+ public static class RandomString
7+ {
8+ private const string _alphanumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
9+
10+ public static string CreateAlphanumeric ( int length )
11+ {
12+ var buffer = new char [ length ] ;
13+ for ( var i = 0 ; i < length ; i ++ )
14+ {
15+ buffer [ i ] = _alphanumeric [ ThreadsafeRandom . Next ( _alphanumeric . Length ) ] ;
16+ }
17+ return new string ( buffer ) ;
18+ }
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+
4+ using System ;
5+ using System . Threading ;
6+
7+ namespace Azure . Test . Perf
8+ {
9+ // Adapted from https://devblogs.microsoft.com/pfxteam/getting-random-numbers-in-a-thread-safe-way/
10+ public static class ThreadsafeRandom
11+ {
12+ private static readonly Random _globalRandom = new Random ( ) ;
13+
14+ private static readonly ThreadLocal < Random > _threadRandom = new ThreadLocal < Random > ( ( ) =>
15+ {
16+ lock ( _globalRandom )
17+ {
18+ return new Random ( _globalRandom . Next ( ) ) ;
19+ }
20+ } ) ;
21+
22+ private static Random ThreadRandom => _threadRandom . Value ;
23+
24+ public static int Next ( ) => ThreadRandom . Next ( ) ;
25+
26+ public static int Next ( int minValue , int maxValue ) => ThreadRandom . Next ( minValue , maxValue ) ;
27+
28+ public static int Next ( int maxValue ) => ThreadRandom . Next ( maxValue ) ;
29+
30+ public static void NextBytes ( byte [ ] buffer ) => ThreadRandom . NextBytes ( buffer ) ;
31+
32+ public static double NextDouble ( ) => ThreadRandom . NextDouble ( ) ;
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments