forked from mischasan/hx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf_x.c
205 lines (174 loc) · 5.91 KB
/
perf_x.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
// Copyright (C) 2001-2013 Mischa Sandberg <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License Version 2 as
// published by the Free Software Foundation. You may not use, modify or
// distribute this program under any other version of the GNU General
// Public License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// IF YOU HAVE NO WAY OF WORKING WITH GPL, CONTACT ME.
//-------------------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "hx.h"
#include "hx_.h"
#include "util.h"
int
main(int argc, char **argv)
{
char const *hx = getenv("hx");
if (!hx)
hx = ".";
int mapmode = HX_MMAP;
char inpdflt[strlen(hx) + sizeof "/data.tab"];
char const *inpfile = inpdflt;
int memsize = 0;
strcat(strcpy(inpdflt, hx), "/data.tab");
if (argc == 1)
usage("[-d] inpfile [membits | 0]\n"
" -d: use disk io, not mmap\n"
" membits: size of RAM for hxbuild (default:20)\n"
" membits=0 means use the existing perf_x.hx");
if (!strcmp(argv[1], "-d"))
mapmode = 0, ++argv, --argc;
switch (argc) {
case 3:
memsize = atoi(argv[2]);
case 2:
inpfile = argv[1];
case 1:
break;
}
// Ensure that dirname(argv[0]) is in LD_LIBRARY_PATH:
char *dir = strrchr(argv[0], '/'), empty[] = "";
char const *llp = getenv("LD_LIBRARY_PATH");
dir = dir ? (*dir = 0, argv[0]) : empty;
if (!llp)
llp = "";
char llpath[strlen(dir) + strlen(llp) + 2];
setenv("LD_LIBRARY_PATH", strcat(strcat(strcpy(llpath, dir), ":"), llp),
/*OVERRIDE*/ 1);
HXFILE *hp;
HXSTAT info;
HXRET rc;
FILE *fp = strcmp(inpfile, "-") ? fopen(inpfile, "r") : stdin;
if (!fp)
die(": cannot read %s:", inpfile);
setvbuf(fp, NULL, _IOFBF, 65536);
if (memsize) {
memsize = 1 << memsize;
hxcreate("perf_x.hx", 0644, 4096, 0, 0);
hp = hxopen("perf_x.hx", HX_UPDATE);
rc = hxbuild(hp, fp, memsize, 0.0);
if (rc < 0)
die("hxbuild(%d): %s", memsize, hxerror(rc));
hxclose(hp);
}
if (hxdebug)
system
("echo;echo built; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx");
hp = hxopen("perf_x.hx", HX_UPDATE | mapmode);
if (!hp)
die("cannot open perf_x.hx%s:", mapmode ? " with MMAP" : "");
char buf[4096], rec[4096];
int len;
double t0 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
buf[strlen(buf) - 1] = 0;
hx_load(hp, rec, sizeof rec, buf);
hxget(hp, rec, sizeof rec);
}
t0 = tick() - t0;
double t1 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
buf[strlen(buf) - 1] = 0;
buf[0] ^= 0x80;
hx_load(hp, rec, sizeof rec, buf);
hxget(hp, rec, sizeof rec);
}
t1 = tick() - t1;
double t2 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
buf[strlen(buf) - 1] = 0;
strcat(buf, "hello, world");
len = hx_load(hp, rec, sizeof rec, buf);
hxput(hp, rec, len);
}
t2 = tick() - t2;
if (hxdebug)
system
("echo;echo put+12; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx");
double t3 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
buf[strlen(buf) - 1] = 0;
len = hx_load(hp, rec, sizeof rec, buf);
hxput(hp, rec, len);
}
t3 = tick() - t3;
if (hxdebug)
system
("echo;echo put-0; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx");
double t4 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
strcpy(buf + strlen(buf) - 1,
"---------1---------2---------3---------4---------5---------6---------7---------8---------9---------0\n");
len = hx_load(hp, rec, sizeof rec, buf);
hxput(hp, rec, len);
}
t4 = tick() - t4;
if (hxdebug)
system
("echo;echo put+100; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx");
double t5 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
buf[strlen(buf) - 1] = 0;
hx_load(hp, rec, sizeof rec, buf);
hxget(hp, rec, sizeof rec);
}
t5 = tick() - t5;
double t6 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
buf[strlen(buf) - 1] = 0;
buf[0] ^= 0x80;
hx_load(hp, rec, sizeof rec, buf);
hxget(hp, rec, sizeof rec);
}
t6 = tick() - t6;
double t7 = tick();
rewind(fp);
while (fgets(buf, sizeof buf, fp)) {
buf[strlen(buf) - 1] = 0;
buf[0] ^= 0x80;
hx_load(hp, rec, sizeof rec, buf);
hxput(hp, rec, sizeof rec);
}
t7 = tick() - t7;
hxstat(hp, &info);
fprintf(stderr, "nrecs: %g build: %.2fM rec/sec\n(usec:)\n"
"\tget-y\t%.2f\n\tget-n\t%.2f\n"
"\tput+12\t%.2f\n\tput+0\t%.2f\n\tput+100\t%.2f\n"
"\tget-y\t%.2f\n\tget-n\t%.2f\n\tput-xx\t%.2f\n",
info.nrecs, info.nrecs / 1E6 / t0, t0 * 1E6 / info.nrecs,
t1 * 1E6 / info.nrecs, t2 * 1E6 / info.nrecs,
t3 * 1E6 / info.nrecs, t4 * 1E6 / info.nrecs,
t5 * 1E6 / info.nrecs, t6 * 1E6 / info.nrecs,
t7 * 1E6 / info.nrecs);
return 0;
}