diff --git a/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs b/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs
index 1464e7370f2..630ecd967c7 100644
--- a/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs
+++ b/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs
@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System.IO;
+using System.Linq;
namespace System
{
@@ -31,5 +32,15 @@ public static string CreateString(int length)
return new string(str);
}
+
+ ///
+ /// Helper method to create a string containing random alphanumeric
+ /// characters equal to the specified length
+ ///
+ public static string CreateRandomString(int length, string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789", int seed = 42)
+ {
+ Random random = new(seed); // use the given seed, to make the benchmarks repeatable
+ return new string(Enumerable.Repeat(alphabet, length).Select(s => s[random.Next(s.Length)]).ToArray());
+ }
}
}
diff --git a/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs b/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs
index 28e8bd0b037..4a4646e7c61 100644
--- a/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs
+++ b/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs
@@ -154,13 +154,20 @@ public string TrimEnd_CharArr(string s, char[] c)
=> s.TrimEnd(c);
[Benchmark]
- [Arguments("Hello", 'l', '!')] // Contains two 'l'
- [Arguments("Hello", 'a', 'b')] // Contains one 'a'
- [Arguments("This is a very nice sentence", 'z', 'y')] // 'z' does not exist in the string
- [Arguments("This is a very nice sentence", 'i', 'I')] // 'i' occuress 3 times in the string
+ [ArgumentsSource(nameof(ReplaceArguments))]
public string Replace_Char(string text, char oldChar, char newChar)
=> text.Replace(oldChar, newChar);
+ public static IEnumerable