测试代码
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test2010 {
static class Perfmon {
public static PerformanceCounter AddCounter;
public static PerformanceCounter RemoveCounter;
public static PerformanceCounter GetOneCounter;
public static void init() {
if (!PerformanceCounterCategory.Exists("conncurrent dict test")) {
CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
CounterCreationData ccd = new CounterCreationData();
ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
ccd.CounterName = "add/sec";
CCDC.Add(ccd);
ccd = new CounterCreationData();
ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
ccd.CounterName = "remove/sec";
CCDC.Add(ccd);
ccd = new CounterCreationData();
ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
ccd.CounterName = "getone/sec";
CCDC.Add(ccd);
PerformanceCounterCategory.Create("conncurrent dict test",
"Demonstrates usage of the AverageCounter64 performance counter type.",
CCDC);
}
AddCounter = new PerformanceCounter("conncurrent dict test", "add/sec", false);
RemoveCounter = new PerformanceCounter("conncurrent dict test", "remove/sec", false);
GetOneCounter = new PerformanceCounter("conncurrent dict test", "getone/sec", false);
AddCounter.RawValue = 0;
RemoveCounter.RawValue = 0;
GetOneCounter.RawValue = 0;
}
}
interface MyDict<K, V> {
void Add(K k, V v);
bool Get(K k, out V v);
bool Remove(K k);
long Count { get; }
}
class ConcurrentDict<K, V> : MyDict<K, V> {
ConcurrentDictionary<K, V> dict = new ConcurrentDictionary<K, V>();
public void Add(K k, V v) {
dict.TryAdd(k, v);
}
public bool Get(K k, out V v) {
V outv = default(V);
bool ret = dict.TryGetValue(k, out outv);
v = outv;
return ret;
}
public bool Remove(K k) {
V outv = default(V);
return dict.TryRemove(k, out outv);
}
public long Count {
get { return dict.Count; }
}
}
class LockDict<K, V> : MyDict<K, V> {
Dictionary<K, V> dict = new Dictionary<K, V>();
public void Add(K k, V v) {
lock (dict)
dict.Add(k, v);
}
public bool Get(K k, out V v) {
V outv = default(V);
bool ret = false;
lock (dict)
ret = dict.TryGetValue(k, out outv);
v = outv;
return ret;
}
public bool Remove(K k) {
lock (dict)
return dict.Remove(k);
}
public long Count {
get {
long ret = 0;
lock (dict)
ret = dict.Count;
return ret;
}
}
}
class RWSLockDict<K, V> : MyDict<K, V> {
Dictionary<K, V> dict = new Dictionary<K, V>();
ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
public void Add(K k, V v) {
rwl.EnterWriteLock();
try {
dict.Add(k, v);
}
finally {
rwl.ExitWriteLock();
}
}
public bool Get(K k, out V v) {
rwl.EnterReadLock();
bool ret = false;
V outv = default(V);
try {
ret = dict.TryGetValue(k, out outv);
}
finally {
rwl.ExitReadLock();
}
v = outv;
return ret;
}
public bool Remove(K k) {
rwl.EnterWriteLock();
try {
return dict.Remove(k);
}
finally {
rwl.ExitWriteLock();
}
}
public long Count {
get {
rwl.EnterReadLock();
try {
return dict.Count;
}
finally {
rwl.ExitReadLock();
}
}
}
}
class RWLockDict<K, V> : MyDict<K, V> {
Dictionary<K, V> dict = new Dictionary<K, V>();
ReaderWriterLock rwl = new ReaderWriterLock();
public void Add(K k, V v) {
rwl.AcquireWriterLock(Timeout.Infinite);
try {
dict.Add(k, v);
}
finally {
rwl.ReleaseWriterLock();
}
}
public bool Get(K k, out V v) {
rwl.AcquireReaderLock(Timeout.Infinite);
bool ret = false;
V outv = default(V);
try {
ret = dict.TryGetValue(k, out outv);
}
finally {
rwl.ReleaseReaderLock();
}
v = outv;
return ret;
}
public bool Remove(K k) {
rwl.AcquireWriterLock(Timeout.Infinite);
try {
return dict.Remove(k);
}
finally {
rwl.ReleaseWriterLock();
}
}
public long Count {
get {
rwl.AcquireReaderLock(Timeout.Infinite);
try {
return dict.Count;
}
finally {
rwl.ReleaseReaderLock();
}
}
}
}
class Program {
static Dictionary<long, string> dict2 = new Dictionary<long, string>();
static void Main(string[] args) {
Perfmon.init();
//test(new LockDict<long, string>());
//test(new RWLockDict<long, string>());
//test(new RWSLockDict<long, string>());
test(new ConcurrentDict<long, string>());
Console.ReadKey();
}
private static void test(MyDict<long, string> dict) {
new Thread(() => {
long toAdd = 0;
while (true) {
for (int i = 10000 - 1; i >= 0; i--) {
dict.Add(toAdd++, null);
Perfmon.AddCounter.Increment();
}
Thread.Sleep(0);
}
}).Start();
new Thread(() => {
long toGet = 0;
while (true) {
for (int i = 10000 - 1; i >= 0; i--) {
string value = null;
if (dict.Get(toGet++, out value))
Perfmon.GetOneCounter.Increment();
else
Thread.Sleep(0);
}
}
}).Start();
new Thread(() => {
long toRemove = 0;
while (true) {
for (int i = 10000 - 1; i >= 0; i--) {
if (dict.Remove(toRemove++))
Perfmon.RemoveCounter.Increment();
else
Thread.Sleep(0);
}
}
}).Start();
new Thread(() => {
while (true) {
Thread.Sleep(1 * 1000);
float addspeed = Perfmon.AddCounter.NextValue();
float removespeed = Perfmon.RemoveCounter.NextValue();
float getspeed = Perfmon.GetOneCounter.NextValue();
Console.WriteLine("count:{0},addspeed:{1},removespeed:{2},getspeed:{3}",
dict.Count, addspeed, removespeed, getspeed);
if (Process.GetCurrentProcess().PrivateMemorySize64 > 500 * 1000 * 1000)
Debugger.Break();
}
}).Start();
}
}
}
using System.Collections.Concurrent;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
namespace Test2010 {
static class Perfmon {
public static PerformanceCounter AddCounter;
public static PerformanceCounter RemoveCounter;
public static PerformanceCounter GetOneCounter;
public static void init() {
if (!PerformanceCounterCategory.Exists("conncurrent dict test")) {
CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
CounterCreationData ccd = new CounterCreationData();
ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
ccd.CounterName = "add/sec";
CCDC.Add(ccd);
ccd = new CounterCreationData();
ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
ccd.CounterName = "remove/sec";
CCDC.Add(ccd);
ccd = new CounterCreationData();
ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
ccd.CounterName = "getone/sec";
CCDC.Add(ccd);
PerformanceCounterCategory.Create("conncurrent dict test",
"Demonstrates usage of the AverageCounter64 performance counter type.",
CCDC);
}
AddCounter = new PerformanceCounter("conncurrent dict test", "add/sec", false);
RemoveCounter = new PerformanceCounter("conncurrent dict test", "remove/sec", false);
GetOneCounter = new PerformanceCounter("conncurrent dict test", "getone/sec", false);
AddCounter.RawValue = 0;
RemoveCounter.RawValue = 0;
GetOneCounter.RawValue = 0;
}
}
interface MyDict<K, V> {
void Add(K k, V v);
bool Get(K k, out V v);
bool Remove(K k);
long Count { get; }
}
class ConcurrentDict<K, V> : MyDict<K, V> {
ConcurrentDictionary<K, V> dict = new ConcurrentDictionary<K, V>();
public void Add(K k, V v) {
dict.TryAdd(k, v);
}
public bool Get(K k, out V v) {
V outv = default(V);
bool ret = dict.TryGetValue(k, out outv);
v = outv;
return ret;
}
public bool Remove(K k) {
V outv = default(V);
return dict.TryRemove(k, out outv);
}
public long Count {
get { return dict.Count; }
}
}
class LockDict<K, V> : MyDict<K, V> {
Dictionary<K, V> dict = new Dictionary<K, V>();
public void Add(K k, V v) {
lock (dict)
dict.Add(k, v);
}
public bool Get(K k, out V v) {
V outv = default(V);
bool ret = false;
lock (dict)
ret = dict.TryGetValue(k, out outv);
v = outv;
return ret;
}
public bool Remove(K k) {
lock (dict)
return dict.Remove(k);
}
public long Count {
get {
long ret = 0;
lock (dict)
ret = dict.Count;
return ret;
}
}
}
class RWSLockDict<K, V> : MyDict<K, V> {
Dictionary<K, V> dict = new Dictionary<K, V>();
ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
public void Add(K k, V v) {
rwl.EnterWriteLock();
try {
dict.Add(k, v);
}
finally {
rwl.ExitWriteLock();
}
}
public bool Get(K k, out V v) {
rwl.EnterReadLock();
bool ret = false;
V outv = default(V);
try {
ret = dict.TryGetValue(k, out outv);
}
finally {
rwl.ExitReadLock();
}
v = outv;
return ret;
}
public bool Remove(K k) {
rwl.EnterWriteLock();
try {
return dict.Remove(k);
}
finally {
rwl.ExitWriteLock();
}
}
public long Count {
get {
rwl.EnterReadLock();
try {
return dict.Count;
}
finally {
rwl.ExitReadLock();
}
}
}
}
class RWLockDict<K, V> : MyDict<K, V> {
Dictionary<K, V> dict = new Dictionary<K, V>();
ReaderWriterLock rwl = new ReaderWriterLock();
public void Add(K k, V v) {
rwl.AcquireWriterLock(Timeout.Infinite);
try {
dict.Add(k, v);
}
finally {
rwl.ReleaseWriterLock();
}
}
public bool Get(K k, out V v) {
rwl.AcquireReaderLock(Timeout.Infinite);
bool ret = false;
V outv = default(V);
try {
ret = dict.TryGetValue(k, out outv);
}
finally {
rwl.ReleaseReaderLock();
}
v = outv;
return ret;
}
public bool Remove(K k) {
rwl.AcquireWriterLock(Timeout.Infinite);
try {
return dict.Remove(k);
}
finally {
rwl.ReleaseWriterLock();
}
}
public long Count {
get {
rwl.AcquireReaderLock(Timeout.Infinite);
try {
return dict.Count;
}
finally {
rwl.ReleaseReaderLock();
}
}
}
}
class Program {
static Dictionary<long, string> dict2 = new Dictionary<long, string>();
static void Main(string[] args) {
Perfmon.init();
//test(new LockDict<long, string>());
//test(new RWLockDict<long, string>());
//test(new RWSLockDict<long, string>());
test(new ConcurrentDict<long, string>());
Console.ReadKey();
}
private static void test(MyDict<long, string> dict) {
new Thread(() => {
long toAdd = 0;
while (true) {
for (int i = 10000 - 1; i >= 0; i--) {
dict.Add(toAdd++, null);
Perfmon.AddCounter.Increment();
}
Thread.Sleep(0);
}
}).Start();
new Thread(() => {
long toGet = 0;
while (true) {
for (int i = 10000 - 1; i >= 0; i--) {
string value = null;
if (dict.Get(toGet++, out value))
Perfmon.GetOneCounter.Increment();
else
Thread.Sleep(0);
}
}
}).Start();
new Thread(() => {
long toRemove = 0;
while (true) {
for (int i = 10000 - 1; i >= 0; i--) {
if (dict.Remove(toRemove++))
Perfmon.RemoveCounter.Increment();
else
Thread.Sleep(0);
}
}
}).Start();
new Thread(() => {
while (true) {
Thread.Sleep(1 * 1000);
float addspeed = Perfmon.AddCounter.NextValue();
float removespeed = Perfmon.RemoveCounter.NextValue();
float getspeed = Perfmon.GetOneCounter.NextValue();
Console.WriteLine("count:{0},addspeed:{1},removespeed:{2},getspeed:{3}",
dict.Count, addspeed, removespeed, getspeed);
if (Process.GetCurrentProcess().PrivateMemorySize64 > 500 * 1000 * 1000)
Debugger.Break();
}
}).Start();
}
}
}