-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompressionBenchmark.scala
167 lines (151 loc) · 3.66 KB
/
CompressionBenchmark.scala
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
package com.komanov.compression.jmh
import com.komanov.compression.{BlobCompressionRatio, CompressionAlgorithms}
import org.openjdk.jmh.annotations._
import java.nio.file.Files
import java.util.concurrent.TimeUnit
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(value = 1, jvmArgs = Array("-Xmx2G"))
@Threads(2)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
class StubDataCompressionBenchmark {
@Param(Array(
"1024",
"2048",
"3072",
"4096",
"5120",
"6144",
"7168",
"8192",
"9216",
"10240",
"20480",
"30720",
"40960",
"51200",
"61440",
"71680",
"81920",
"92160",
"102400",
))
var length: Int = _
@Param
var compressionRatio: BlobCompressionRatio = _
@Param
var algorithm: CompressionAlgorithms = _
private var data: Array[Byte] = _
private var encoded: Array[Byte] = _
@Setup
def setup(): Unit = {
data = compressionRatio.generateBlob(length)
encoded = algorithm.encode(data)
}
@Benchmark
def encode(counters: Counters): Array[Byte] =
Utils.encode(algorithm, data, counters)
@Benchmark
def decode(counters: Counters): Array[Byte] =
Utils.decode(algorithm, encoded, counters)
}
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(value = 1, jvmArgs = Array("-Xmx2G"))
@Threads(2)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
class RealDataCompressionBenchmark {
@Param(Array(
// "298",
// "319",
// "320",
// "326",
// "366",
// "372",
// "420",
// "459",
// "479",
// "531",
// "538",
// "562",
// "648",
// "665",
// "686",
"34011",
// "35578",
"42223",
"51771",
// "52928",
// "59448",
// "59617",
"62830",
// "67071",
// "67872",
// "68118",
// "68230",
// "79107",
"81207",
// "88114",
"94417",
"607930",
// "668462",
"751048",
// "773419",
"781196",
// "791173",
"866049",
"904172",
// "989390",
"1075724",
"1293080",
// "1356567",
"1448911",
"1599048",
"4072805",
"4287156",
))
var length: Int = _
@Param
var algorithm: CompressionAlgorithms = _
private var input: InputData = _
private var data: Array[Byte] = _
private var encoded: Array[Byte] = _
@Setup
def setup(): Unit = {
input = InputData.values().find(_.name.contains(length.toString)).get
data = Files.readAllBytes(input.path)
encoded = algorithm.encode(data)
}
@Benchmark
def encode(counters: Counters): Array[Byte] =
Utils.encode(algorithm, data, counters)
@Benchmark
def decode(counters: Counters): Array[Byte] =
Utils.decode(algorithm, encoded, counters)
}
@AuxCounters(AuxCounters.Type.EVENTS)
@State(Scope.Thread)
class Counters {
var totalInputBytes = 0L
var totalOutputBytes = 0L
def increment(input: Array[Byte], output: Array[Byte]): Unit = {
totalInputBytes += input.length
totalOutputBytes += output.length
}
}
object Utils {
def encode(algorithm: CompressionAlgorithms, data: Array[Byte], counters: Counters): Array[Byte] = {
val r = algorithm.encode(data)
counters.increment(data, r)
r
}
def decode(algorithm: CompressionAlgorithms, encoded: Array[Byte], counters: Counters): Array[Byte] = {
val r = algorithm.decode(encoded)
counters.increment(encoded, r)
r
}
}