|
| 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