-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCompressionTests.cs
More file actions
209 lines (173 loc) · 5.71 KB
/
CompressionTests.cs
File metadata and controls
209 lines (173 loc) · 5.71 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Waher.Content;
using Waher.Networking.HTTP.Brotli;
using Waher.Networking.HTTP.ContentEncodings;
using Waher.Networking.HTTP.TransferEncodings;
using Waher.Runtime.Console;
using Waher.Runtime.Profiling;
namespace Waher.Networking.HTTP.Test
{
[TestClass]
public class CompressionTests
{
[TestMethod]
public async Task Test_01_OneFile_GZip()
{
await Test<GZipContentEncoding>("gzip", false); // To remove JIT times
await Test<GZipContentEncoding>("gzip", true);
}
[TestMethod]
public async Task Test_02_OneFile_Deflate()
{
await Test<DeflateContentEncoding>("deflate", false); // To remove JIT times
await Test<DeflateContentEncoding>("deflate", true);
}
[TestMethod]
public async Task Test_03_OneFile_Brotli()
{
await Test<BrotliContentEncoding>("br", false); // To remove JIT times
await Test<BrotliContentEncoding>("br", true);
}
private static async Task Test<T>(string Name, bool Output)
where T : IContentEncoding, new()
{
Profiler Profiler = new(Name, ProfilerThreadType.Sequential);
Profiler.Start();
Profiler.NewState("Init");
string FilePath = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\..\Waher.IoTGateway.Resources\Highlight\highlight.pack.js");
Profiler.NewState("Load");
byte[] Data = File.ReadAllBytes(FilePath);
Profiler.NewState("Setup");
T ContentEncoding = new();
MemoryStream Result = new();
InternalTransfer TransferEncoding = new(Result);
TransferEncoding Compressor = ContentEncoding.GetEncoder(TransferEncoding, Data.Length, Name);
Profiler.NewState("Compress");
await Compressor.EncodeAsync(true, Data, 0, Data.Length, true);
await Compressor.FlushAsync(true);
Profiler.NewState("Result");
byte[] Compressed = Result.ToArray();
if (Output)
{
ConsoleOut.WriteLine("Input: " + Data.Length + " bytes");
ConsoleOut.WriteLine("Output: " + Compressed.Length + " bytes");
ConsoleOut.WriteLine();
}
Profiler.Stop();
if (Output)
{
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("```uml");
ConsoleOut.WriteLine(Profiler.ExportPlantUml(TimeUnit.MilliSeconds));
ConsoleOut.WriteLine("```");
}
}
[TestMethod]
public async Task Test_04_Statistics_GZip()
{
await Test<GZipContentEncoding>("GZip", "Red");
}
[TestMethod]
public async Task Test_05_Statistics_Deflate()
{
await Test<DeflateContentEncoding>("Deflate", "Green");
}
[TestMethod]
public async Task Test_06_Statistics_Brotli()
{
await Test<BrotliContentEncoding>("Brotli", "Blue");
}
private static async Task Test<T>(string VariableName, string Color)
where T : IContentEncoding, new()
{
string FolderPath = Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\..\Waher.IoTGateway.Resources");
string[] FileNames = Directory.GetFiles(FolderPath, "*.*", SearchOption.AllDirectories);
Stopwatch Watch = new();
Watch.Start();
await Test<T>(FileNames[0], false, Watch, false); // To remove JIT times
ConsoleOut.Write(VariableName);
ConsoleOut.WriteLine(":=[");
int i, c = FileNames.Length;
for (i = 0; i < c; i++)
{
string FileName = FileNames[i];
switch (Path.GetExtension(FileName))
{
case ".md":
case ".css":
case ".js":
case ".uml":
case ".xslt":
case ".ws":
case ".dot":
case ".json":
case ".txt":
case ".manifest":
await Test<T>(FileName, true, Watch, i == c - 1);
break;
}
}
ConsoleOut.WriteLine("];");
ConsoleOut.Write(VariableName);
ConsoleOut.Write("Size:=scatter2d(");
ConsoleOut.Write(VariableName);
ConsoleOut.Write("[0,],");
ConsoleOut.Write(VariableName);
ConsoleOut.Write("[1,],\"");
ConsoleOut.Write(Color);
ConsoleOut.WriteLine("\");");
ConsoleOut.Write(VariableName);
ConsoleOut.WriteLine("Size.LabelX:='File (Bytes)';");
ConsoleOut.Write(VariableName);
ConsoleOut.WriteLine("Size.LabelY:='Compressed (Bytes)';");
ConsoleOut.Write(VariableName);
ConsoleOut.WriteLine("Size.Title:='Size';");
ConsoleOut.Write(VariableName);
ConsoleOut.Write("Time:=scatter2d(");
ConsoleOut.Write(VariableName);
ConsoleOut.Write("[0,],");
ConsoleOut.Write(VariableName);
ConsoleOut.Write("[2,],\"");
ConsoleOut.Write(Color);
ConsoleOut.WriteLine("\");");
ConsoleOut.Write(VariableName);
ConsoleOut.WriteLine("Time.LabelX:='File (Bytes)';");
ConsoleOut.Write(VariableName);
ConsoleOut.WriteLine("Time.LabelY:='Time (ms)';");
ConsoleOut.Write(VariableName);
ConsoleOut.WriteLine("Time.Title:='Time';");
}
private static async Task Test<T>(string FilePath, bool Output, Stopwatch Watch, bool Last)
where T : IContentEncoding, new()
{
long StartTicks = Watch.ElapsedTicks;
byte[] Data = File.ReadAllBytes(FilePath);
T ContentEncoding = new();
MemoryStream Result = new();
InternalTransfer TransferEncoding = new(Result);
TransferEncoding Compressor = ContentEncoding.GetEncoder(TransferEncoding, Data.Length, null);
await Compressor.EncodeAsync(true, Data, 0, Data.Length, true);
await Compressor.FlushAsync(true);
byte[] Compressed = Result.ToArray();
double TicksPerMs = 1000.0 * (Watch.ElapsedTicks - StartTicks) / Stopwatch.Frequency;
if (Output)
{
ConsoleOut.Write("\t[");
ConsoleOut.Write(Data.Length);
ConsoleOut.Write(',');
ConsoleOut.Write(Compressed.Length);
ConsoleOut.Write(',');
ConsoleOut.Write(CommonTypes.Encode(TicksPerMs));
ConsoleOut.Write(",\"");
ConsoleOut.Write(JSON.Encode(FilePath));
ConsoleOut.Write("\"]");
if (!Last)
ConsoleOut.Write(',');
ConsoleOut.WriteLine();
}
}
}
}