/
engine.go
463 lines (398 loc) · 13.8 KB
/
engine.go
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package engine
import (
"bytes"
flate "compress/flate"
gzip "compress/gzip"
lzw "compress/lzw"
zlib "compress/zlib"
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
ent "github.com/kzahedi/goent/discrete"
arithmetic "github.com/mrfleap/custom-compression/compressor/arithmetic"
dmc "github.com/mrfleap/custom-compression/compressor/dmc"
huffman "github.com/mrfleap/custom-compression/compressor/huffman"
lz "github.com/mrfleap/custom-compression/compressor/lz"
mcc "github.com/mrfleap/custom-compression/compressor/mcc"
"html/template"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
"reflect"
"runtime/debug"
"sort"
"strconv"
"strings"
"sync"
"time"
)
// Engines is a slice of strings representing possible algorithms.
var Engines = [...]string{"all", "suite", "lzss", "dmc", "huffman", "mcc", "flate", "gzip", "lzw", "zlib", "arithmetic"}
// Suites is a map of strings to strings representing a suite name and the contained algorithms.
var Suites = map[string][]string{"all": Engines[2:], "suite": {"lzss", "dmc", "huffman", "mcc", "flate", "gzip", "lzw", "zlib", "arithmetic"}}
// CompressedFile is a struct used to read a compressed file or write to a compressed file.
type CompressedFile struct {
CompressionEngine string
Compressed []byte
Decompressed []byte
pos int
MaxSearchBufferLength int
}
// Readers represents a map of algorithm names to their NewReader interfaces.
var Readers = map[string]interface{}{
"lzss": lz.NewReader,
"dmc": dmc.NewReader,
"mcc": mcc.NewReader,
"huffman": huffman.NewReader,
"arithmetic": arithmetic.NewReader,
"zlib": zlib.NewReader,
"flate": flate.NewReader,
"gzip": gzip.NewReader,
"lzw": lzw.NewReader,
}
func (f *CompressedFile) Read(content []byte) (int, error) {
if f.Decompressed == nil {
newReader := Readers[f.CompressionEngine]
var r io.Reader
var b io.Reader
b = bytes.NewReader(f.Compressed)
var err error
switch f.CompressionEngine {
default:
r = newReader.(func(io.Reader) io.Reader)(b)
case "zlib":
r, err = newReader.(func(r io.Reader) (io.ReadCloser, error))(b)
case "flate":
r = newReader.(func(r io.Reader) io.ReadCloser)(b)
case "gzip":
r, err = newReader.(func(r io.Reader) (*gzip.Reader, error))(b)
case "lzw":
// LZW requires special parameters for lzw
r = newReader.(func(io.Reader, lzw.Order, int) io.ReadCloser)(b, lzw.MSB, 8)
}
check(err)
f.Decompressed, err = ioutil.ReadAll(r)
check(err)
}
bytesToWriteOut := len(f.Decompressed[f.pos:])
if len(content) < bytesToWriteOut {
bytesToWriteOut = len(content)
}
for i := 0; i < bytesToWriteOut; i++ {
content[i] = f.Decompressed[f.pos:][i]
}
var err error
if len(f.Decompressed[f.pos:]) <= len(content) {
err = io.EOF
} else {
f.pos += len(content)
}
return bytesToWriteOut, err
}
// Writers represents a map of algorithm names to their NewWriter interfaces.
var Writers = map[string]interface{}{
"lzss": lz.NewWriter,
"dmc": dmc.NewWriter,
"mcc": mcc.NewWriter,
"huffman": huffman.NewWriter,
"arithmetic": arithmetic.NewWriter,
"zlib": zlib.NewWriter,
"flate": flate.NewWriter,
"gzip": gzip.NewWriter,
"lzw": lzw.NewWriter,
}
func (f *CompressedFile) Write(content []byte) (int, error) {
var compressed []byte
newWriter := Writers[f.CompressionEngine]
var b bytes.Buffer
var w io.WriteCloser
var err error
switch f.CompressionEngine {
default:
w = newWriter.(func(io.Writer) io.WriteCloser)(&b)
case "zlib":
w = newWriter.(func(r io.Writer) *zlib.Writer)(&b)
case "flate":
w, err = newWriter.(func(w io.Writer, level int) (*flate.Writer, error))(&b, 9)
case "gzip":
w = newWriter.(func(w io.Writer) *gzip.Writer)(&b)
case "lzw":
// LZW requires special parameters for lzw
w = newWriter.(func(w io.Writer, order lzw.Order, litWidth int) io.WriteCloser)(&b, lzw.MSB, 8)
}
check(err)
w.Write(content)
w.Close()
compressed = b.Bytes()
f.Compressed = append(f.Compressed, compressed...)
return len(compressed), nil
}
// GetCompressedFileFromPath takes a path variable and returns a CompressedFile object or an error.
func GetCompressedFileFromPath(path string) (CompressedFile, error) {
var cf CompressedFile
fileContents, err := ioutil.ReadFile(path)
cf = CompressedFile{Compressed: fileContents}
return cf, err
}
// CompressFile takes a compression algorithm as a string and a path to a file and writes out the file in the same path with .compressed appended to the end.
func CompressFile(compressionEngine string, fileString string) {
fileContents, err := ioutil.ReadFile(fileString)
check(err)
fmt.Printf("Compressing...\n")
file := CompressedFile{MaxSearchBufferLength: 4096}
file.CompressionEngine = compressionEngine
file.Write(fileContents)
var compressedFilePath = filepath.Base(fileString) + ".compressed"
err = ioutil.WriteFile(compressedFilePath, file.Compressed, 0644)
fmt.Printf("Original bytes: %v\n", len(fileContents))
fmt.Printf("Compressed bytes: %v\n", len(file.Compressed))
percentageDiff := float32(len(file.Compressed)) / float32(len(fileContents)) * 100
fmt.Printf("Compression ratio: %.2f%%\n", percentageDiff)
}
// DecompressFile takes a compression algorithm as a string and a path to a file and writes out the decompressed file in the same path with .decompressed appended to the end.
func DecompressFile(compressionEngine string, fileString string) []byte {
compressedFile, err := GetCompressedFileFromPath(fileString)
compressedFile.CompressionEngine = compressionEngine
check(err)
fmt.Printf("Decompressing...\n")
stream := make([]byte, 0)
out := make([]byte, 512)
for {
n, err := compressedFile.Read(out)
if err != nil && err != io.EOF {
panic(err)
} else {
stream = append(stream, out[0:n]...)
}
if err == io.EOF {
break
}
}
var decompressedFilePath = filepath.Base(strings.Replace(fileString, ".compressed", "", -1))
err = ioutil.WriteFile(decompressedFilePath, stream, 0644)
check(err)
return stream
}
// Result is an intermediary object used to represent the benchmarked results of a certain file and algorithm.
type Result struct {
CompressionEngine string
TimeTaken string
Ratio float32
ActualEntropy float32
Entropy float64
Lossless bool
Failed bool
}
// BenchmarkSuite takes a set of files and algorithms and returns the result as an html table if generateHTML is set.
// The result is also outputted to stdout.
func BenchmarkSuite(files []string, algorithms [][]string, generateHTML bool) string {
var html string
timeout := 2 * time.Minute
for i, fileString := range files {
fmt.Printf("Compressing file %d/%d - %s\n", i+1, len(files), fileString)
results := make([]Result, 0)
failedResults := make([]Result, 0)
fileContents, err := ioutil.ReadFile(fileString)
check(err)
fileSize := int64(len(fileContents))
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleLight)
t.AppendHeader(table.Row{"engine", "time taken", "compression ratio", "actual entropy", "theoretical entropy", "lossless"})
resultChans := make(map[string]chan Result)
var wg sync.WaitGroup
for _, algorithmsInLayer := range algorithms {
algorithmsString := strings.Join(algorithmsInLayer[:], ",")
fmt.Println("Benchmarking", algorithmsString)
resultChannel := make(chan Result, 1)
resultChans[algorithmsString] = resultChannel
wg.Add(1)
go AsyncBenchmarkFile(resultChannel, &wg, algorithmsInLayer, fileString)
}
waitTimeout(&wg, timeout)
for compressionEngineName, resultChan := range resultChans {
select {
case result := <-resultChan:
if result.Failed {
failedResults = append(failedResults, result)
} else {
results = append(results, result)
}
default:
result := Result{}
result.CompressionEngine = compressionEngineName
result.TimeTaken = fmt.Sprintf(">%s", timeout)
result.Lossless = false
result.Failed = true
failedResults = append(failedResults, result)
}
}
sort.Slice(results, func(i, j int) bool {
if results[j].Lossless && results[i].Lossless {
return results[j].Ratio > results[i].Ratio
} else if results[j].Lossless && !results[i].Lossless {
return false
} else if !results[j].Lossless && results[i].Lossless {
return true
} else {
return results[j].Ratio > results[i].Ratio
}
})
for _, result := range results {
t.AppendRow([]interface{}{result.CompressionEngine, result.TimeTaken, fmt.Sprintf("%.2f%%", result.Ratio), fmt.Sprintf("%.2f", result.ActualEntropy), fmt.Sprintf("%.2f", result.Entropy), result.Lossless})
}
t.AppendSeparator()
for _, result := range failedResults {
t.AppendRow([]interface{}{result.CompressionEngine, result.TimeTaken, "DNF", "DNF", "DNF", result.Lossless})
}
t.AppendSeparator()
t.AppendRow(table.Row{"File", fileString, "Size", ByteCountSI(fileSize)})
t.Render()
if generateHTML {
html = html + "<br>" + t.RenderHTML()
}
}
if generateHTML {
tmpl := template.Must(template.ParseFiles("templates/benchmark.html"))
var b bytes.Buffer
tmpl.Execute(&b, struct {
Tables template.HTML
Created string
}{Tables: template.HTML(html), Created: strconv.FormatInt(time.Now().Unix(), 10)})
return b.String()
}
return ""
}
// AsyncBenchmarkFile takes a channel to push the result, a waitgroup, engines, a file string and runs the benchmark.
// The function will push the result to the channel or push a failed result if it is able to catch an error during execution.
func AsyncBenchmarkFile(resultChannel chan Result, wg *sync.WaitGroup, compressionEngines []string, fileString string) {
defer wg.Done()
algorithmsString := strings.Join(compressionEngines[:], ",")
errorHandler := func() {
if r := recover(); r != nil {
fmt.Printf("%s errored during execution, continuing\n", algorithmsString)
fmt.Println("Err:", r)
fmt.Println(string(debug.Stack()))
fmt.Println("Continuing")
result := Result{}
result.CompressionEngine = algorithmsString
result.TimeTaken = "failed"
result.Lossless = false
result.Failed = true
resultChannel <- result
}
}
defer errorHandler()
start := time.Now()
result := BenchmarkFile(compressionEngines, fileString, NewSuiteSettings())
duration := time.Since(start)
result.TimeTaken = fmt.Sprintf("%s", duration.Round(10*time.Microsecond).String())
fmt.Printf("%s finished benchmarking\n", algorithmsString)
resultChannel <- result
}
// Settings represents an object that can be used to modify the settings when benchmarking files with BenchmarkFile
type Settings struct {
WriteOutFiles bool
PrintStats bool
PrintStatus bool
}
// NewSuiteSettings returns common settings for a testing suite as a Settings object
func NewSuiteSettings() Settings {
s := Settings{}
s.PrintStatus = true
return s
}
// BenchmarkFile takes a set of algorithms, a file path, and a settings object.
// It benchmarks the file and returns the result as a Result object.
func BenchmarkFile(algorithms []string, fileString string, settings Settings) Result {
fileContents, err := ioutil.ReadFile(fileString)
check(err)
algorithmsString := strings.Join(algorithms[:], ",")
if settings.PrintStatus {
fmt.Printf("%s Compressing...\n", algorithmsString)
}
symbolFrequencies := make(map[byte]int)
for _, c := range []byte(fileContents) {
symbolFrequencies[c]++
}
total := len([]byte(fileContents))
freqs := make([]float64, len(symbolFrequencies))
i := 0
for _, freq := range symbolFrequencies {
freqs[i] = float64(freq) / float64(total)
i++
}
start := time.Now()
content := fileContents
for _, algorithm := range algorithms {
file := CompressedFile{MaxSearchBufferLength: 4096}
file.CompressionEngine = algorithm
file.Write(content)
if settings.WriteOutFiles {
var compressedFilePath = filepath.Base(fileString) + ".compressed"
err = ioutil.WriteFile(compressedFilePath, file.Compressed, 0644)
}
content = file.Compressed
}
compressed := content
if settings.PrintStatus {
fmt.Printf("%s Decompressing...\n", algorithmsString)
}
for i := len(algorithms) - 1; i >= 0; i-- {
algorithm := algorithms[i]
file := CompressedFile{}
file.Compressed = content
file.CompressionEngine = algorithm
stream := make([]byte, 0)
out := make([]byte, 512)
for {
n, err := file.Read(out)
if err != nil && err != io.EOF {
panic(err)
} else {
stream = append(stream, out[0:n]...)
}
if err == io.EOF {
break
}
}
content = file.Decompressed
if settings.WriteOutFiles {
var decompressedFilePath = filepath.Base(fileString) + ".decompressed"
err = ioutil.WriteFile(decompressedFilePath, stream, 0644)
check(err)
}
}
decompressed := content
duration := time.Since(start)
lossless := reflect.DeepEqual(fileContents, decompressed)
percentageDiff := float32(len(compressed)) / float32(len(fileContents)) * 100
entropy := ent.Entropy(freqs, math.Log)
symbolFrequencies = make(map[byte]int)
for _, c := range content {
symbolFrequencies[c]++
}
total = len(compressed)
freqs = make([]float64, len(symbolFrequencies))
i = 0
for _, freq := range symbolFrequencies {
freqs[i] = float64(freq) / float64(total)
i++
}
actualEntropy := float32(ent.Entropy(freqs, math.Log))
timeTaken := fmt.Sprintf("%s", duration.Round(10*time.Microsecond).String())
if settings.PrintStats {
fmt.Printf("Lossless: %t\n", lossless)
fmt.Printf("Original bytes: %v\n", len(fileContents))
fmt.Printf("Compressed bytes: %v\n", len(compressed))
if !lossless {
fmt.Printf("Decompressed bytes: %v\n", len(decompressed))
}
fmt.Printf("Compression ratio: %.2f%%\n", percentageDiff)
fmt.Printf("Original Shannon entropy: %.2f\n", entropy)
fmt.Printf("Compressed Shannon entropy: %.2f\n", actualEntropy)
fmt.Printf("Time taken: %s\n", timeTaken)
}
return Result{algorithmsString, timeTaken, percentageDiff, actualEntropy, entropy, lossless, false}
}