|
@@ -56,6 +56,9 @@ namespace Google.Protobuf.Benchmarks
|
|
// key is the encodedSize of string values
|
|
// key is the encodedSize of string values
|
|
Dictionary<int, string[]> stringValues;
|
|
Dictionary<int, string[]> stringValues;
|
|
|
|
|
|
|
|
+ // key is the encodedSize of string values
|
|
|
|
+ Dictionary<int, string[]> nonAsciiStringValues;
|
|
|
|
+
|
|
// key is the encodedSize of string values
|
|
// key is the encodedSize of string values
|
|
Dictionary<int, ByteString[]> byteStringValues;
|
|
Dictionary<int, ByteString[]> byteStringValues;
|
|
|
|
|
|
@@ -66,6 +69,8 @@ namespace Google.Protobuf.Benchmarks
|
|
|
|
|
|
public IEnumerable<int> StringEncodedSizes => new[] { 1, 4, 10, 105, 10080 };
|
|
public IEnumerable<int> StringEncodedSizes => new[] { 1, 4, 10, 105, 10080 };
|
|
|
|
|
|
|
|
+ public IEnumerable<int> NonAsciiStringEncodedSizes => new[] { 4, 10, 105, 10080 };
|
|
|
|
+
|
|
[GlobalSetup]
|
|
[GlobalSetup]
|
|
public void GlobalSetup()
|
|
public void GlobalSetup()
|
|
{
|
|
{
|
|
@@ -86,12 +91,19 @@ namespace Google.Protobuf.Benchmarks
|
|
floatValues = CreateRandomFloats(random, BytesToWrite / sizeof(float));
|
|
floatValues = CreateRandomFloats(random, BytesToWrite / sizeof(float));
|
|
|
|
|
|
stringValues = new Dictionary<int, string[]>();
|
|
stringValues = new Dictionary<int, string[]>();
|
|
|
|
+
|
|
byteStringValues = new Dictionary<int, ByteString[]>();
|
|
byteStringValues = new Dictionary<int, ByteString[]>();
|
|
foreach(var encodedSize in StringEncodedSizes)
|
|
foreach(var encodedSize in StringEncodedSizes)
|
|
{
|
|
{
|
|
stringValues.Add(encodedSize, CreateStrings(BytesToWrite / encodedSize, encodedSize));
|
|
stringValues.Add(encodedSize, CreateStrings(BytesToWrite / encodedSize, encodedSize));
|
|
byteStringValues.Add(encodedSize, CreateByteStrings(BytesToWrite / encodedSize, encodedSize));
|
|
byteStringValues.Add(encodedSize, CreateByteStrings(BytesToWrite / encodedSize, encodedSize));
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ nonAsciiStringValues = new Dictionary<int, string[]>();
|
|
|
|
+ foreach(var encodedSize in NonAsciiStringEncodedSizes)
|
|
|
|
+ {
|
|
|
|
+ nonAsciiStringValues.Add(encodedSize, CreateNonAsciiStrings(BytesToWrite / encodedSize, encodedSize));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// Total number of bytes that each benchmark will write.
|
|
// Total number of bytes that each benchmark will write.
|
|
@@ -318,6 +330,35 @@ namespace Google.Protobuf.Benchmarks
|
|
ctx.CheckNoSpaceLeft();
|
|
ctx.CheckNoSpaceLeft();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ [Benchmark]
|
|
|
|
+ [ArgumentsSource(nameof(NonAsciiStringEncodedSizes))]
|
|
|
|
+ public void WriteNonAsciiString_CodedOutputStream(int encodedSize)
|
|
|
|
+ {
|
|
|
|
+ var values = nonAsciiStringValues[encodedSize];
|
|
|
|
+ var cos = new CodedOutputStream(outputBuffer);
|
|
|
|
+ foreach (var value in values)
|
|
|
|
+ {
|
|
|
|
+ cos.WriteString(value);
|
|
|
|
+ }
|
|
|
|
+ cos.Flush();
|
|
|
|
+ cos.CheckNoSpaceLeft();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ [Benchmark]
|
|
|
|
+ [ArgumentsSource(nameof(NonAsciiStringEncodedSizes))]
|
|
|
|
+ public void WriteNonAsciiString_WriteContext(int encodedSize)
|
|
|
|
+ {
|
|
|
|
+ var values = nonAsciiStringValues[encodedSize];
|
|
|
|
+ var span = new Span<byte>(outputBuffer);
|
|
|
|
+ WriteContext.Initialize(ref span, out WriteContext ctx);
|
|
|
|
+ foreach (var value in values)
|
|
|
|
+ {
|
|
|
|
+ ctx.WriteString(value);
|
|
|
|
+ }
|
|
|
|
+ ctx.Flush();
|
|
|
|
+ ctx.CheckNoSpaceLeft();
|
|
|
|
+ }
|
|
|
|
+
|
|
[Benchmark]
|
|
[Benchmark]
|
|
[ArgumentsSource(nameof(StringEncodedSizes))]
|
|
[ArgumentsSource(nameof(StringEncodedSizes))]
|
|
public void WriteBytes_CodedOutputStream(int encodedSize)
|
|
public void WriteBytes_CodedOutputStream(int encodedSize)
|
|
@@ -399,6 +440,18 @@ namespace Google.Protobuf.Benchmarks
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static string[] CreateNonAsciiStrings(int valueCount, int encodedSize)
|
|
|
|
+ {
|
|
|
|
+ var str = ParseRawPrimitivesBenchmark.CreateNonAsciiStringWithEncodedSize(encodedSize);
|
|
|
|
+
|
|
|
|
+ var result = new string[valueCount];
|
|
|
|
+ for (int i = 0; i < valueCount; i++)
|
|
|
|
+ {
|
|
|
|
+ result[i] = str;
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
private static ByteString[] CreateByteStrings(int valueCount, int encodedSize)
|
|
private static ByteString[] CreateByteStrings(int valueCount, int encodedSize)
|
|
{
|
|
{
|
|
var str = ParseRawPrimitivesBenchmark.CreateStringWithEncodedSize(encodedSize);
|
|
var str = ParseRawPrimitivesBenchmark.CreateStringWithEncodedSize(encodedSize);
|