Skip to content
This repository was archived by the owner on Apr 7, 2023. It is now read-only.

Commit 8ed482a

Browse files
committed
migrated memory and performance from MS Test to NBench + NUnit
1 parent 2480d79 commit 8ed482a

File tree

13 files changed

+587
-381
lines changed

13 files changed

+587
-381
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
4+
namespace OpenMcdf.MemTest
5+
{
6+
[SetUpFixture]
7+
public class AssetDeployer
8+
{
9+
[OneTimeSetUp]
10+
public void DeployAssetOnce()
11+
{
12+
Directory.SetCurrentDirectory(TestContext.CurrentContext.WorkDirectory);
13+
14+
var codeBaseDir = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
15+
var assetsDir = Path.Combine(codeBaseDir.FullName, "assets");
16+
while (!Directory.Exists(assetsDir) &&
17+
!Path.GetPathRoot(codeBaseDir.FullName).Equals(codeBaseDir.FullName))
18+
{
19+
codeBaseDir = codeBaseDir.Parent;
20+
assetsDir = Path.Combine(codeBaseDir.FullName, "assets");
21+
}
22+
23+
foreach (var assetPath in Directory.GetFiles(assetsDir))
24+
{
25+
var assetInTests = Path.Combine(TestContext.CurrentContext.WorkDirectory, Path.GetFileName(assetPath));
26+
File.Copy(assetPath, assetInTests, true);
27+
}
28+
}
29+
}
30+
}

tests/OpenMcdf.MemTest/Helpers.cs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
5+
namespace OpenMcdf.MemTest
6+
{
7+
public static class Helpers
8+
{
9+
internal static byte[] GetBuffer(int count)
10+
{
11+
Random r = new Random();
12+
byte[] b = new byte[count];
13+
r.NextBytes(b);
14+
return b;
15+
}
16+
17+
internal static byte[] GetBuffer(int count, byte c)
18+
{
19+
byte[] b = new byte[count];
20+
for (int i = 0; i < b.Length; i++)
21+
{
22+
b[i] = c;
23+
}
24+
25+
return b;
26+
}
27+
28+
internal static bool CompareBuffer(byte[] b, byte[] p)
29+
{
30+
if (b == null && p == null)
31+
throw new Exception("Null buffers");
32+
33+
if (b == null && p != null) return false;
34+
if (b != null && p == null) return false;
35+
36+
if (b.Length != p.Length)
37+
return false;
38+
39+
for (int i = 0; i < b.Length; i++)
40+
{
41+
if (b[i] != p[i])
42+
return false;
43+
}
44+
45+
return true;
46+
}
47+
48+
internal static void StressMemory()
49+
{
50+
const int N_LOOP = 20;
51+
const int MB_SIZE = 10;
52+
53+
byte[] b = Helpers.GetBuffer(1024 * 1024 * MB_SIZE); //2GB buffer
54+
byte[] cmp = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
55+
56+
CompoundFile cf = new CompoundFile(CFSVersion.Ver_4, CFSConfiguration.Default);
57+
CFStream st = cf.RootStorage.AddStream("MySuperLargeStream");
58+
cf.Save("LARGE.cfs");
59+
cf.Close();
60+
61+
//Console.WriteLine("Closed save");
62+
//Console.ReadKey();
63+
64+
cf = new CompoundFile("LARGE.cfs", CFSUpdateMode.Update, CFSConfiguration.Default);
65+
CFStream cfst = cf.RootStorage.GetStream("MySuperLargeStream");
66+
67+
Stopwatch sw = new Stopwatch();
68+
sw.Start();
69+
for (int i = 0; i < N_LOOP; i++)
70+
{
71+
72+
cfst.Append(b);
73+
cf.Commit(true);
74+
75+
Console.WriteLine(" Updated " + i.ToString());
76+
//Console.ReadKey();
77+
}
78+
79+
cfst.Append(cmp);
80+
cf.Commit(true);
81+
sw.Stop();
82+
83+
84+
cf.Close();
85+
86+
Console.WriteLine(sw.Elapsed.TotalMilliseconds);
87+
sw.Reset();
88+
89+
//Console.WriteLine(sw.Elapsed.TotalMilliseconds);
90+
91+
//Console.WriteLine("Closed Transacted");
92+
//Console.ReadKey();
93+
94+
cf = new CompoundFile("LARGE.cfs");
95+
int count = 8;
96+
sw.Reset();
97+
sw.Start();
98+
byte[] data = new byte[count];
99+
count = cf.RootStorage.GetStream("MySuperLargeStream").Read(data, b.Length * (long)N_LOOP, count);
100+
sw.Stop();
101+
Console.Write(count);
102+
cf.Close();
103+
104+
Console.WriteLine("Closed Final " + sw.ElapsedMilliseconds);
105+
Console.ReadKey();
106+
107+
}
108+
109+
internal static void DummyFile()
110+
{
111+
Console.WriteLine("Start");
112+
FileStream fs = new FileStream("myDummyFile", FileMode.Create);
113+
fs.Close();
114+
115+
Stopwatch sw = new Stopwatch();
116+
117+
byte[] b = Helpers.GetBuffer(1024 * 1024 * 50); //2GB buffer
118+
119+
fs = new FileStream("myDummyFile", FileMode.Open);
120+
sw.Start();
121+
for (int i = 0; i < 42; i++)
122+
{
123+
124+
fs.Seek(b.Length * i, SeekOrigin.Begin);
125+
fs.Write(b, 0, b.Length);
126+
127+
}
128+
129+
fs.Close();
130+
sw.Stop();
131+
Console.WriteLine("Stop - " + sw.ElapsedMilliseconds);
132+
sw.Reset();
133+
134+
Console.ReadKey();
135+
}
136+
137+
internal static void AddNodes(String depth, CFStorage cfs)
138+
{
139+
140+
Action<CFItem> va = delegate (CFItem target)
141+
{
142+
143+
String temp = target.Name + (target is CFStorage ? "" : " (" + target.Size + " bytes )");
144+
145+
//Stream
146+
147+
Console.WriteLine(depth + temp);
148+
149+
if (target is CFStorage)
150+
{ //Storage
151+
152+
String newDepth = depth + " ";
153+
154+
//Recursion into the storage
155+
AddNodes(newDepth, (CFStorage)target);
156+
157+
}
158+
};
159+
160+
//Visit NON-recursively (first level only)
161+
cfs.VisitEntries(va, false);
162+
}
163+
}
164+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
using System;
2+
using System.IO;
3+
using System.Diagnostics;
4+
using NBench;
5+
using NUnit.Framework;
6+
using OpenMcdf.PerfTest;
7+
8+
//This project is used for profiling memory and performances of OpenMCDF .
9+
10+
namespace OpenMcdf.MemTest
11+
{
12+
public class MemoryTest : PerformanceTestStuite<MemoryTest>
13+
{
14+
private Counter _testCounter;
15+
16+
[PerfSetup]
17+
public void Setup(BenchmarkContext context)
18+
{
19+
_testCounter = context.GetCounter("TestCounter");
20+
}
21+
22+
[PerfBenchmark(NumberOfIterations = 1, RunMode = RunMode.Iterations, TestMode = TestMode.Test, SkipWarmups = true)]
23+
[CounterMeasurement("TestCounter")]
24+
[CounterTotalAssertion("TestCounter", MustBe.LessThanOrEqualTo, 6000.0d)] // max 6 sec
25+
[MemoryAssertion(MemoryMetric.TotalBytesAllocated, MustBe.LessThanOrEqualTo, 450 * 1024 * 1024)] // max 450 Mb in RAM
26+
public void TestMultipleCodeFeatures()
27+
{
28+
const int N_FACTOR = 1000;
29+
30+
byte[] bA = Helpers.GetBuffer(20 * 1024 * N_FACTOR, 0x0A);
31+
byte[] bB = Helpers.GetBuffer(5 * 1024, 0x0B);
32+
byte[] bC = Helpers.GetBuffer(5 * 1024, 0x0C);
33+
byte[] bD = Helpers.GetBuffer(5 * 1024, 0x0D);
34+
byte[] bE = Helpers.GetBuffer(8 * 1024 * N_FACTOR + 1, 0x1A);
35+
byte[] bF = Helpers.GetBuffer(16 * 1024 * N_FACTOR, 0x1B);
36+
byte[] bG = Helpers.GetBuffer(14 * 1024 * N_FACTOR, 0x1C);
37+
byte[] bH = Helpers.GetBuffer(12 * 1024 * N_FACTOR, 0x1D);
38+
byte[] bE2 = Helpers.GetBuffer(8 * 1024 * N_FACTOR, 0x2A);
39+
byte[] bMini = Helpers.GetBuffer(1027, 0xEE);
40+
41+
Stopwatch sw = new Stopwatch();
42+
sw.Start();
43+
44+
var cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.SectorRecycle);
45+
cf.RootStorage.AddStream("A").SetData(bA);
46+
cf.Save("OneStream.cfs");
47+
48+
cf.Close();
49+
50+
cf = new CompoundFile("OneStream.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.SectorRecycle);
51+
52+
cf.RootStorage.AddStream("B").SetData(bB);
53+
cf.RootStorage.AddStream("C").SetData(bC);
54+
cf.RootStorage.AddStream("D").SetData(bD);
55+
cf.RootStorage.AddStream("E").SetData(bE);
56+
cf.RootStorage.AddStream("F").SetData(bF);
57+
cf.RootStorage.AddStream("G").SetData(bG);
58+
cf.RootStorage.AddStream("H").SetData(bH);
59+
60+
cf.Save("8_Streams.cfs");
61+
62+
cf.Close();
63+
64+
File.Copy("8_Streams.cfs", "6_Streams.cfs", true);
65+
66+
cf = new CompoundFile("6_Streams.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle|CFSConfiguration.EraseFreeSectors);
67+
cf.RootStorage.Delete("D");
68+
cf.RootStorage.Delete("G");
69+
cf.Commit();
70+
71+
cf.Close();
72+
73+
File.Copy("6_Streams.cfs", "6_Streams_Shrinked.cfs", true);
74+
75+
cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
76+
cf.RootStorage.AddStream("ZZZ").SetData(bF);
77+
cf.RootStorage.GetStream("E").Append(bE2);
78+
cf.Commit();
79+
cf.Close();
80+
81+
cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
82+
cf.RootStorage.CLSID = new Guid("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
83+
cf.Commit();
84+
cf.Close();
85+
86+
cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
87+
cf.RootStorage.AddStorage("MyStorage").AddStream("ANS").Append(bE);
88+
cf.Commit();
89+
cf.Close();
90+
91+
cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
92+
cf.RootStorage.AddStorage("AnotherStorage").AddStream("ANS").Append(bE);
93+
cf.RootStorage.Delete("MyStorage");
94+
cf.Commit();
95+
cf.Close();
96+
97+
CompoundFile.ShrinkCompoundFile("6_Streams_Shrinked.cfs");
98+
99+
cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
100+
cf.RootStorage.AddStorage("MiniStorage").AddStream("miniSt").Append(bMini);
101+
cf.RootStorage.GetStorage("MiniStorage").AddStream("miniSt2").Append(bMini);
102+
cf.Commit();
103+
cf.Close();
104+
105+
cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
106+
cf.RootStorage.GetStorage("MiniStorage").Delete("miniSt");
107+
108+
109+
cf.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").Append(bE);
110+
cf.Commit();
111+
cf.Close();
112+
113+
cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.SectorRecycle);
114+
115+
var myStream = cf.RootStorage.GetStream("C");
116+
var data = myStream.GetData();
117+
Console.WriteLine(data[0] + " : " + data[data.Length - 1]);
118+
119+
myStream = cf.RootStorage.GetStream("B");
120+
data = myStream.GetData();
121+
Console.WriteLine(data[0] + " : " + data[data.Length - 1]);
122+
123+
cf.Close();
124+
125+
sw.Stop();
126+
Console.WriteLine(sw.ElapsedMilliseconds);
127+
128+
for (int i = 0; i < sw.ElapsedMilliseconds; i++)
129+
{
130+
_testCounter.Increment();
131+
}
132+
}
133+
134+
[PerfBenchmark(NumberOfIterations = 1, RunMode = RunMode.Iterations, TestMode = TestMode.Test, SkipWarmups = false)]
135+
[CounterMeasurement("TestCounter")]
136+
[CounterTotalAssertion("TestCounter", MustBe.LessThanOrEqualTo, 5500.0d)] // max 5.5 sec
137+
[MemoryAssertion(MemoryMetric.TotalBytesAllocated, MustBe.LessThanOrEqualTo, 3 * 1024 * 1024)] // max 3 Mb in RAM
138+
public void TestMultipleStreamCommit()
139+
{
140+
File.Copy("report.xls", "reportOverwriteMultiple.xls", true);
141+
142+
CompoundFile cf = new CompoundFile("reportOverwriteMultiple.xls", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
143+
144+
Random r = new Random();
145+
146+
Stopwatch sw = new Stopwatch();
147+
sw.Start();
148+
149+
for (int i = 0; i < 1000; i++)
150+
{
151+
byte[] buffer = Helpers.GetBuffer(r.Next(100, 3500), 0x0A);
152+
153+
if (i > 0)
154+
{
155+
if (r.Next(0, 100) > 50)
156+
{
157+
cf.RootStorage.Delete("MyNewStream" + (i - 1).ToString());
158+
}
159+
}
160+
161+
CFStream addedStream = cf.RootStorage.AddStream("MyNewStream" + i.ToString());
162+
163+
addedStream.SetData(buffer);
164+
165+
// Random commit, not on single addition
166+
if (r.Next(0, 100) > 50)
167+
cf.Commit();
168+
}
169+
170+
cf.Close();
171+
172+
sw.Stop();
173+
Console.WriteLine(sw.ElapsedMilliseconds);
174+
175+
for (int i = 0; i < sw.ElapsedMilliseconds; i++)
176+
{
177+
_testCounter.Increment();
178+
}
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)