/
Program.cs
88 lines (69 loc) · 2.35 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace testMemCache
{
class Program
{
const int MAX_KEY = 100000;
const int THREAD_COUNT = 20;
static void Main(string[] args)
{
testMicrosoft();
testDictionary();
Console.ReadLine();
}
static void runMultyThread(Action action, int threadCount)
{
Thread[] thArray = new Thread[threadCount];
for (int i = 0; i < threadCount; i++)
{
thArray[i] = new Thread(new ThreadStart(action));
thArray[i].Start();
}
thArray.ToList().ForEach(t => t.Join());
}
static void testMicrosoft()
{
int startTime = System.Environment.TickCount;
runMultyThread(() =>
{
IObjectCacheClient memCacheMicosoft = new CacheByMicrosoft();
loadData(memCacheMicosoft);
getData(memCacheMicosoft);
}, THREAD_COUNT);
int totalTime = System.Environment.TickCount - startTime;
Console.WriteLine($"Total time for microsoft cache = {totalTime}");
}
static void testDictionary()
{
int startTime = System.Environment.TickCount;
runMultyThread(() =>
{
IObjectCacheClient memCacheDictionary = new CacheByDictionary();
loadData(memCacheDictionary);
getData(memCacheDictionary);
}, THREAD_COUNT);
int totalTime = System.Environment.TickCount - startTime;
Console.WriteLine($"Total time for dictionary cache = {totalTime}");
}
static void loadData(IObjectCacheClient cache)
{
for(int i = 0;i < MAX_KEY;i++)
{
var val = new string('D', 100);
cache.Set(i.ToString(), val, null);
}
}
static void getData(IObjectCacheClient cache)
{
for (int i = 0; i < MAX_KEY; i++)
{
var val = cache.Get<string>(i.ToString());
}
}
}
}