Commit fc06d78c authored by Marc Gravell's avatar Marc Gravell Committed by Nick Craver

remove vectors from basictest; it breaks *everything*

parent 0e428a7c
...@@ -13,17 +13,10 @@ ...@@ -13,17 +13,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" /> <PackageReference Include="BenchmarkDotNet" Version="0.10.14" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\StackExchange.Redis\StackExchange.Redis.csproj" /> <ProjectReference Include="..\StackExchange.Redis\StackExchange.Redis.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="t8.shakespeare.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>
...@@ -46,9 +46,7 @@ public class RedisBenchmarks : IDisposable ...@@ -46,9 +46,7 @@ public class RedisBenchmarks : IDisposable
/// </summary> /// </summary>
public RedisBenchmarks() public RedisBenchmarks()
{ {
mgr = new SocketManager(GetType().Name); var options = ConfigurationOptions.Parse("127.0.0.1:6379");
var options = ConfigurationOptions.Parse("127.0.0.1:6379,syncTimeout=1000");
options.SocketManager = mgr;
connection = ConnectionMultiplexer.Connect(options); connection = ConnectionMultiplexer.Connect(options);
db = connection.GetDatabase(3); db = connection.GetDatabase(3);
} }
......
using System;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using BenchmarkDotNet.Attributes;
#pragma warning disable CS1591
namespace BasicTest
{
[Config(typeof(CustomConfig))]
public class TextBenchmarks
{
private readonly string[] corpus;
private readonly byte[] buffer;
public TextBenchmarks()
{
corpus = File.ReadAllLines("t8.shakespeare.txt");
buffer = new byte[enc.GetMaxByteCount(corpus.Max(x => x.Length))];
}
private static readonly Encoding enc = Encoding.UTF8;
[Benchmark]
public long Measure()
{
long total = 0;
for (int i = 0; i < corpus.Length; i++)
total += enc.GetByteCount(corpus[i]);
return total;
}
[Benchmark]
public long MeasureAndEncode()
{
long total = 0;
var buffer = this.buffer;
for (int i = 0; i < corpus.Length; i++)
{
string s = corpus[i];
total += enc.GetByteCount(s);
enc.GetBytes(s, 0, s.Length, buffer, 0);
}
return total;
}
[Benchmark]
public long MeasureVectorized()
{
long total = 0;
for (int i = 0; i < corpus.Length; i++)
total += GetEncodedLength(corpus[i], out _);
return total;
}
[Benchmark]
public long MeasureAndEncodeVectorized()
{
long total = 0;
var buffer = this.buffer;
for (int i = 0; i < corpus.Length; i++)
{
string s = corpus[i];
total += GetEncodedLength(s, out var asciiChunks);
Encode(s, buffer, asciiChunks);
}
return total;
}
private static readonly Vector<ushort> NonAsciiMask = new Vector<ushort>(0xFF80);
internal static
#if NET47
unsafe
#endif
int GetEncodedLength(string value, out int asciiChunks)
{
asciiChunks = 0;
if (value.Length == 0) return 0;
int offset = 0;
if (Vector.IsHardwareAccelerated && value.Length >= Vector<ushort>.Count)
{
var charSpan = MemoryMarshal.Cast<char, Vector<ushort>>(value.AsSpan());
var nonAscii = NonAsciiMask;
int i;
for (i = 0; i < charSpan.Length; i++)
{
if ((charSpan[i] & nonAscii) != Vector<ushort>.Zero) break;
}
offset = Vector<ushort>.Count * i;
asciiChunks = i;
}
int remaining = value.Length - offset;
if (remaining == 0) return offset; // all ASCII (nice round length, and Vector support)
// handles a) no Vector support, b) anything from the fisrt non-ASCII chunk, c) tail end
#if NET47
fixed (char* ptr = value)
{
return offset + Encoding.UTF8.GetByteCount(ptr + offset, remaining);
}
#else
return offset + enc.GetByteCount(s: value, index: offset, count: remaining);
#endif
}
private int Encode(string value, byte[] buffer, int asciiChunks)
{
int offset = 0;
if (Vector.IsHardwareAccelerated && asciiChunks != 0)
{
var charSpan = MemoryMarshal.Cast<char, Vector<ushort>>(value.AsSpan());
var byteSpan = MemoryMarshal.Cast<byte, Vector<byte>>(buffer);
var nonAscii = NonAsciiMask;
int i = 0;
asciiChunks >>= 1; // half it - we can only use double-chunks
for (int chunk = 0; chunk < asciiChunks; chunk++)
{
byteSpan[chunk] = Vector.Narrow(charSpan[i++], charSpan[i++]);
}
offset = Vector<ushort>.Count * i;
asciiChunks = i;
}
int remaining = value.Length - offset;
if (remaining == 0) return offset; // all ASCII (nice round length, and Vector support)
// handles a) no Vector support, b) anything from the fisrt non-ASCII chunk, c) tail end
return offset + enc.GetBytes(value, offset, remaining, buffer, offset);
}
}
}
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using StackExchange.Redis;
namespace TestConsole namespace TestConsole
{ {
...@@ -9,46 +7,22 @@ internal static class Program ...@@ -9,46 +7,22 @@ internal static class Program
{ {
private static int Main() private static int Main()
{ {
var s = new StringWriter();
var watch = Stopwatch.StartNew();
try try
{ {
using (var obj = new BasicTest.RedisBenchmarks())
var config = new ConfigurationOptions
{
ConnectRetry = 0,
EndPoints = { "127.0.0.1:6379" },
};
using (var conn = ConnectionMultiplexer.Connect(config, log: Console.Out))
{ {
Execute(conn); var watch = Stopwatch.StartNew();
obj.Execute();
watch.Stop();
Console.WriteLine($"{watch.ElapsedMilliseconds}ms");
} }
return 0; return 0;
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.Error.WriteLine(ex); Console.Error.WriteLine(ex.Message);
return -1; return -1;
} }
finally
{
watch.Stop();
Console.WriteLine();
Console.WriteLine($"{watch.ElapsedMilliseconds}ms (done)");
}
}
private static void Execute(ConnectionMultiplexer conn)
{
Console.WriteLine("Executing...");
var key = "abc";
var db = conn.GetDatabase(0);
var t = db.CreateTransaction();
t.HashSetAsync(key, "foo", "bar");
t.KeyExpireAsync(key, TimeSpan.FromSeconds(3600));
t.Execute();
Console.WriteLine("Done");
} }
} }
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.1;net462</TargetFrameworks> <TargetFrameworks>netcoreapp2.0;netcoreapp2.1;net47</TargetFrameworks>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BasicTest\BasicTest.csproj" />
<ProjectReference Include="..\StackExchange.Redis\StackExchange.Redis.csproj" /> <ProjectReference Include="..\StackExchange.Redis\StackExchange.Redis.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment