-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_pglz.c
363 lines (304 loc) · 12 KB
/
test_pglz.c
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
/*--------------------------------------------------------------------------
*
* test_pglz.c
* Test pglz performance.
*
* Copyright (c) 2019, PostgreSQL Global Development Group
*
* IDENTIFICATION
* contrib/test_pglz.c
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "miscadmin.h"
#include <limits.h>
#include <time.h>
#include "common/pg_lzcompress.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(test_pglz);
typedef int32 (*decompress_func)(const char *source, int32 slen, char *dest,
int32 rawsize, bool check_complete);
typedef int32 (*compress_func)(const char *source, int32 slen, char *dest,
const PGLZ_Strategy *strategy);
int32
pglz_compress_vanilla(const char *source, int32 slen, char *dest,
const PGLZ_Strategy *strategy);
int32
pglz_compress_hacked(const char *source, int32 slen, char *dest,
const PGLZ_Strategy *strategy);
int32
pglz_decompress_hacked_unrolled(const char *source, int32 slen, char *dest,
const PGLZ_Strategy *strategy);
int32
pglz_decompress_vanilla(const char *source, int32 slen, char *dest,
int32 rawsize, bool check_complete);
int32
pglz_decompress_hacked(const char *source, int32 slen, char *dest,
int32 rawsize, bool check_complete);
int32
pglz_decompress_hacked8(const char *source, int32 slen, char *dest,
int32 rawsize, bool check_complete);
int32
pglz_decompress_hacked16(const char *source, int32 slen, char *dest,
int32 rawsize, bool check_complete);
double do_test(int compressor, int decompressor, int payload, bool decompression_time);
double do_sliced_test(int compressor, int decompressor, int payload, int slice_size, bool decompression_time);
compress_func compressors[] = {pglz_compress_vanilla, pglz_compress_hacked};
char *compressor_name[] = {"pglz_compress_vanilla", "pglz_compress_hacked"};
int compressors_count = 2;
decompress_func decompressors[] =
{
pglz_decompress_vanilla,
pglz_decompress_hacked,
pglz_decompress_hacked_unrolled,
pglz_decompress_hacked8,
pglz_decompress_hacked16,
pglz_decompress_vanilla,
};
char *decompressor_name[] =
{
"pglz_decompress_vanilla - warmup", /* do vanilla test at the beginning and at the end */
"pglz_decompress_hacked",
//"pglz_decompress_hacked_unrolled",
"pglz_decompress_hacked8",
//"pglz_decompress_hacked16",
"pglz_decompress_vanilla",
};
int decompressors_count = 4;
char *payload_names[] =
{
"000000010000000000000001",
"000000010000000000000006",
"000000010000000000000008",
"16398",
"shakespeare.txt",
"mr","dickens","mozilla","nci","ooffice","osdb","reymont","samba","sao","webster","x-ray","xml"
};
void **payloads;
long *payload_sizes;
int payload_count = 17;
/* benchmark returns ns per byte of payload to decompress */
double do_test(int compressor, int decompressor, int payload, bool decompression_time)
{
ereport(LOG,
(errmsg("Testing payload %s\tcompressor %s\tdecompressor %s",
payload_names[payload], compressor_name[compressor], decompressor_name[decompressor]),
errhidestmt(true)));
void *data = payloads[payload];
long size = payload_sizes[payload];
void *extracted_data = palloc(size);
void *compressed = palloc(size * 2);
compressors[compressor](data, size, compressed, PGLZ_strategy_default);
clock_t compression_begin = clock();
int comp_size = compressors[compressor](data, size, compressed, PGLZ_strategy_default);
clock_t copmression_end = clock();
clock_t decompression_begin = clock();
if (decompressors[decompressor](compressed, comp_size, extracted_data, size, true) != size)
{}
// elog(ERROR, "decompressed wrong size %d instead of %d",decompressors[decompressor](compressed, comp_size, extracted_data, size, true),size);
clock_t decopmression_end = clock();
//if (memcmp(extracted_data, data, size))
// elog(ERROR, "decompressed different data");
ereport(LOG,
(errmsg("Compression %ld\t(%f seconds)\tDecompression %ld\t(%f seconds)\tRatio %f",
copmression_end - compression_begin, ((float)copmression_end - compression_begin) / CLOCKS_PER_SEC,
decopmression_end - decompression_begin, ((float)decopmression_end - decompression_begin) / CLOCKS_PER_SEC,
comp_size/(float)size),
errhidestmt(true)));
pfree(extracted_data);
pfree(compressed);
if (decompression_time)
return ((double)decopmression_end - decompression_begin) * (1000000000.0L / size) / CLOCKS_PER_SEC;
else
return ((double)copmression_end - compression_begin) * (1000000000.0L / size) / CLOCKS_PER_SEC;
}
double do_sliced_test(int compressor, int decompressor, int payload, int slice_size, bool decompression_time)
{
ereport(LOG,
(errmsg("Testing %dKb slicing payload %s\tcompressor %s\tdecompressor %s", slice_size / 1024,
payload_names[payload], compressor_name[compressor], decompressor_name[decompressor]),
errhidestmt(true)));
char *data = payloads[payload];
long size = payload_sizes[payload];
int slice_count = size / slice_size;
void **extracted_data = palloc(slice_count * sizeof(void*));
void **compressed = palloc(slice_count * sizeof(void*));
int *comp_size = palloc(slice_count * sizeof(int));
int i;
for (i = 0; i < slice_count; i++)
{
extracted_data[i] = palloc(slice_size * 2);
compressed[i] = palloc(slice_size * 2);
}
clock_t compression_begin = clock();
for (i = 0; i < slice_count; i++)
comp_size[i] = compressors[compressor](data + slice_size * i, slice_size, compressed[i], PGLZ_strategy_default);
clock_t compression_end = clock();
clock_t decompression_begin = clock();
for (i = 0; i < slice_count; i++)
{
if (comp_size[i] == -1)
continue; /* no decompression */
int decompressed_size = decompressors[decompressor](compressed[i], comp_size[i], extracted_data[i], slice_size, false);
if (decompressed_size != slice_size)
elog(ERROR, "decompressed wrong size %d instead of %d, compressed size %d", decompressed_size, slice_size, comp_size[i]);
}
clock_t decopmression_end = clock();
for (i = 0; i< slice_count; i++)
{
pfree(extracted_data[i]);
pfree(compressed[i]);
}
pfree(extracted_data);
pfree(comp_size);
pfree(compressed);
if (decompression_time)
return ((double)decopmression_end - decompression_begin) * (1000000000.0L / size) / CLOCKS_PER_SEC;
else
return ((double)compression_end - compression_begin) * (1000000000.0L / size) / CLOCKS_PER_SEC;
}
static void prepare_payloads()
{
payloads = palloc(sizeof(void*) * payload_count);
payload_sizes = palloc(sizeof(long) * payload_count);
char share_path[MAXPGPATH];
char path[MAXPGPATH];
FILE *f;
long size;
int i;
for (i=0; i< payload_count; i++)
{
get_share_path(my_exec_path, share_path);
snprintf(path, MAXPGPATH, "%s/extension/%s", share_path, payload_names[i]);
f = fopen(path, "r");
if (!f)
elog(ERROR, "unable to open payload");
fseek (f , 0 , SEEK_END);
size = ftell (f);
rewind (f);
void* data = palloc(size);
if (fread(data, size, 1, f) != 1)
elog(ERROR, "unable to read payload");
fclose(f);
payloads[i] = data;
payload_sizes[i] = size;
}
}
/*
* SQL-callable entry point to perform all tests.
*/
Datum
test_pglz(PG_FUNCTION_ARGS)
{
double decompression_results[10][10];
double decompression_sliced_2kb_results[10][10];
double decompression_sliced_8kb_results[10][10];
double decompressor_results[10];
double compression_results[10][10];
double compression_sliced_2kb_results[10][10];
double compression_sliced_8kb_results[10][10];
double compressor_results[10];
int iterations = 5;
int iteration;
int i,p;
int old_verbosity = Log_error_verbosity;
prepare_payloads();
for (p = 0; p < payload_count; p++)
for (i = 0; i < decompressors_count; i++)
{
decompressor_results[i] = 0;
decompression_results[p][i] = 0;
for (iteration = 0; iteration < iterations; iteration++)
decompression_results[p][i] += do_test(0, i, p, true);
decompression_results[p][i] /= iterations;
decompression_sliced_2kb_results[p][i] = 0;
for (iteration = 0; iteration < iterations; iteration++)
decompression_sliced_2kb_results[p][i] += do_sliced_test(0, i, p, 2048, true);
decompression_sliced_2kb_results[p][i] /= iterations;
decompression_results[p][i] /= iterations;
decompression_sliced_8kb_results[p][i] = 0;
for (iteration = 0; iteration < iterations; iteration++)
decompression_sliced_8kb_results[p][i] += do_sliced_test(0, i, p, 4096, true);
decompression_sliced_8kb_results[p][i] /= iterations;
}
for (p = 0; p < payload_count; p++)
for (i = 0; i < compressors_count; i++)
{
compressor_results[i] = 0;
compression_results[p][i] = 0;
for (iteration = 0; iteration < iterations; iteration++)
compression_results[p][i] += do_test(i, 0, p, false);
compression_results[p][i] /= iterations;
compression_sliced_2kb_results[p][i] = 0;
for (iteration = 0; iteration < iterations; iteration++)
compression_sliced_2kb_results[p][i] += do_sliced_test(i, 0, p, 2048, false);
compression_sliced_2kb_results[p][i] /= iterations;
compression_results[p][i] /= iterations;
compression_sliced_8kb_results[p][i] = 0;
for (iteration = 0; iteration < iterations; iteration++)
compression_sliced_8kb_results[p][i] += do_sliced_test(i, 0, p, 4096, false);
compression_sliced_8kb_results[p][i] /= iterations;
}
Log_error_verbosity = PGERROR_TERSE;
ereport(NOTICE, (errmsg("Time to decompress one byte in ns:"), errhidestmt(true)));
for (p = 0; p < payload_count; p++)
{
ereport(NOTICE, (errmsg("Payload %s", payload_names[p]), errhidestmt(true)));
for (i = 1; i < decompressors_count; i++)
{
ereport(NOTICE, (errmsg("Decompressor %s result %f", decompressor_name[i], decompression_results[p][i]), errhidestmt(true)));
decompressor_results[i] += decompression_results[p][i];
}
ereport(NOTICE, (errmsg("Payload %s sliced by 2Kb", payload_names[p]), errhidestmt(true)));
for (i = 1; i < decompressors_count; i++)
{
ereport(NOTICE, (errmsg("Decompressor %s result %f", decompressor_name[i], decompression_results[p][i]), errhidestmt(true)));
decompressor_results[i] += decompression_sliced_2kb_results[p][i];
}
ereport(NOTICE, (errmsg("Payload %s sliced by 8Kb", payload_names[p]), errhidestmt(true)));
for (i = 1; i < decompressors_count; i++)
{
ereport(NOTICE, (errmsg("Decompressor %s result %f", decompressor_name[i], decompression_sliced_8kb_results[p][i]), errhidestmt(true)));
decompressor_results[i] += decompression_sliced_8kb_results[p][i];
}
}
ereport(NOTICE, (errmsg("\n\nDecompressor score (summ of all times):"), errhidestmt(true)));
for (i = 1; i < decompressors_count; i++)
{
ereport(NOTICE, (errmsg("Decompressor %s result %f", decompressor_name[i], decompressor_results[i]), errhidestmt(true)));
}
Log_error_verbosity = PGERROR_TERSE;
ereport(NOTICE, (errmsg("Time to compress one byte in ns:"), errhidestmt(true)));
for (p = 0; p < payload_count; p++)
{
ereport(NOTICE, (errmsg("Payload %s", payload_names[p]), errhidestmt(true)));
for (i = 0; i < compressors_count; i++)
{
ereport(NOTICE, (errmsg("Compressor %s result %f", compressor_name[i], compression_results[p][i]), errhidestmt(true)));
compressor_results[i] += compression_results[p][i];
}
ereport(NOTICE, (errmsg("Payload %s sliced by 2Kb", payload_names[p]), errhidestmt(true)));
for (i = 0; i < compressors_count; i++)
{
ereport(NOTICE, (errmsg("Compressor %s result %f", compressor_name[i], compression_results[p][i]), errhidestmt(true)));
compressor_results[i] += compression_sliced_2kb_results[p][i];
}
ereport(NOTICE, (errmsg("Payload %s sliced by 8Kb", payload_names[p]), errhidestmt(true)));
for (i = 0; i < compressors_count; i++)
{
ereport(NOTICE, (errmsg("Compressor %s result %f", compressor_name[i], compression_sliced_8kb_results[p][i]), errhidestmt(true)));
compressor_results[i] += compression_sliced_8kb_results[p][i];
}
}
ereport(NOTICE, (errmsg("\n\nCompressor score (summ of all times):"), errhidestmt(true)));
for (i = 0; i < compressors_count; i++)
{
ereport(NOTICE, (errmsg("Compressor %s result %f", compressor_name[i], compressor_results[i]), errhidestmt(true)));
}
Log_error_verbosity = old_verbosity;
PG_RETURN_VOID();
}