This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
/
palloc_bench.c
90 lines (65 loc) · 1.84 KB
/
palloc_bench.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
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <limits.h>
#include "postgres.h"
#include "utils/datum.h"
#include "utils/array.h"
#include "utils/memutils.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#define GET_AGG_CONTEXT(fname, fcinfo, aggcontext) \
if (! AggCheckCallContext(fcinfo, &aggcontext)) { \
elog(ERROR, "%s called in non-aggregate context", fname); \
}
/* prototypes */
PG_FUNCTION_INFO_V1(palloc_bench);
// #define TRACKING_FLAG
Datum palloc_bench(PG_FUNCTION_ARGS);
Datum
palloc_bench(PG_FUNCTION_ARGS)
{
/* info for anyelement */
int i = 0;
int32 ncontexts = PG_GETARG_INT32(0);
int32 niterations = PG_GETARG_INT32(1);
int32 allocsize = PG_GETARG_INT32(2);
struct timeval start_time, end_time;
/* memory contexts */
MemoryContext initctx = CurrentMemoryContext;
MemoryContext ctx = initctx;
/* switch to the per-group hash-table memory context */
for (i = 0; i < ncontexts; i++) {
char name[256];
sprintf(name, "test context %d", i);
#ifdef TRACKING_FLAG
ctx = AllocSetContextCreateTracked(ctx,
name,
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE,
true);
#else
ctx = AllocSetContextCreate(ctx,
name,
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
#endif
}
MemoryContextSwitchTo(ctx);
gettimeofday(&start_time, NULL);
for (i = 0; i < niterations; i++) {
char * p = palloc(allocsize);
if (p != NULL)
pfree(p);
}
gettimeofday(&end_time, NULL);
MemoryContextSwitchTo(initctx);
elog(WARNING, "duration = %.2f ms", (end_time.tv_sec - start_time.tv_sec) * 1000 + (end_time.tv_usec - start_time.tv_usec) / 1000.0);
PG_RETURN_VOID();
}