Skip to content

Instantly share code, notes, and snippets.

@serkanberksoy
Created March 12, 2017 12:49
Show Gist options
  • Save serkanberksoy/53a9fc7d3830fc16c7df55b8c51240ec to your computer and use it in GitHub Desktop.
Save serkanberksoy/53a9fc7d3830fc16c7df55b8c51240ec to your computer and use it in GitHub Desktop.
DictionaryVSRedisClientTest.cs is a performance test between ConcurrentDictionary, Dictionary and StackExchange.Redis client to redis server.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using StackExchange.Redis;
using ZeroFormatter;
namespace ConsoleApplication2
{
[ZeroFormattable]
// [Serializable]
public class TestObject
{
[Index(0)]
public virtual int Id { get; set; }
[Index(1)]
public virtual string Name { get; set; }
[Index(2)]
public virtual string Description { get; set; }
}
class Program
{
private const int TEST_COUNT = 10000;
private static string temp = "X".PadRight(1024 * 100, 'X');
static void Main(string[] args)
{
Console.WriteLine("Shall we?");
Console.ReadKey();
RunConcurrentDictionaryTests();
RunDictionaryTests();
RunRedisTests();
Console.ReadKey();
}
private static void RunConcurrentDictionaryTests()
{
Console.WriteLine("\nConcurrent Dictionary Test:");
var sw = new Stopwatch();
sw.Start();
ConcurrentDictionary<int, TestObject> dictionary = new ConcurrentDictionary<int, TestObject>();
for (int i = 0; i < TEST_COUNT; i++)
{
dictionary.TryAdd(i, new TestObject {Id = i, Name = temp, Description = temp});
}
sw.Stop();
Console.WriteLine("write to dictionary: " + sw.Elapsed.TotalMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < 10000; i++)
{
string x = dictionary[i].Name;
}
sw.Stop();
Console.WriteLine("read from dictionary: " + sw.Elapsed.TotalMilliseconds);
}
private static void RunDictionaryTests()
{
Console.WriteLine("\nDictionary Test:");
var sw = new Stopwatch();
sw.Start();
Dictionary<int, TestObject> dictionary = new Dictionary<int, TestObject>();
for (int i = 0; i < TEST_COUNT; i++)
{
dictionary.Add(i, new TestObject { Id = i, Name = temp, Description = temp });
}
sw.Stop();
Console.WriteLine("write to dictionary: " + sw.Elapsed.TotalMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < TEST_COUNT; i++)
{
string x = dictionary[i].Name;
}
sw.Stop();
Console.WriteLine("read from dictionary: " + sw.Elapsed.TotalMilliseconds);
}
private static void RunRedisTests()
{
using (var muxer = ConnectionMultiplexer.Connect("127.0.0.1"))
{
// muxer.GetServer("127.0.0.1:6379").FlushAllDatabases();
var db = muxer.GetDatabase(db:0);
/*
byte[] bytes = ZeroFormatterSerializer.Serialize(new TestObject {Description = "test desc", Id = 1, Name = "test name"});
TestObject mc2 = ZeroFormatterSerializer.Deserialize<TestObject>(bytes);
Console.WriteLine(mc2.Name);
string strBytes2 = Convert.ToBase64String(ZeroFormatterSerializer.Serialize(new TestObject { Description = "test desc", Id = 1, Name = "test name" }));
TestObject mc3 = ZeroFormatterSerializer.Deserialize<TestObject>(Convert.FromBase64String(strBytes2));
Console.WriteLine(mc3.Name);
*/
Console.WriteLine("\n redis Test:");
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < TEST_COUNT; i++)
{
/*
db.SetAddAsync(i.ToString(), ZeroFormatterSerializer.Serialize(new TestObject
{
Id = i,
Name = temp,
Description = temp
}));
*/
SetItem2<TestObject>(db, i.ToString(), new TestObject
{
Id = i,
Name = temp,
Description = temp
});
}
sw.Stop();
Console.WriteLine("write to redis: " + sw.Elapsed.TotalMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < TEST_COUNT; i++)
{
string x = GetItem2<TestObject>(db, i.ToString()).Name;
/*
RedisValue redisValue = db.StringGet(i.ToString());
string x = ZeroFormatterSerializer.Deserialize<TestObject>(redisValue).Name;
*/
}
sw.Stop();
Console.WriteLine("read from redis: " + sw.Elapsed.TotalMilliseconds);
}
}
public static void SetItem2<T>(IDatabase redDb, string key, T value)
{
redDb.StringSet(key, ZeroFormatterSerializer.Serialize<T>(value));
}
public static T GetItem2<T>(IDatabase redDb, string key)
{
RedisValue redisResult = redDb.StringGet(key);
T objResult = ZeroFormatterSerializer.Deserialize<T>(redisResult);
return objResult;
}
public static void SetItem<T>(IDatabase redDb, string key, T value)
{
redDb.StringSet(key, ToByteArray<T>(value));
}
public static T GetItem<T>(IDatabase redDb, string key)
{
RedisValue redisResult = redDb.StringGet(key);
T objResult = FromByteArray<T>(redisResult);
return objResult;
}
public static byte[] ToByteArray<T>(T obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
public static T FromByteArray<T>(byte[] data)
{
if (data == null)
return default(T);
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(data))
{
object obj = bf.Deserialize(ms);
return (T)obj;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment