Skip to content

Commit 29f374b

Browse files
authored
[Perf] Add helper classes for random values (#24303)
1 parent 072b337 commit 29f374b

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

common/Perf/Azure.Test.Perf/RandomStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)