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
|
/*
* Copyright 2015 Kevin Murray <spam@kdmurray.id.au>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/*
* ============================================================================
*
* Filename: benchmarks.c
*
* Description: Some benchmarks
* License: GPLv3+
* Author: Kevin Murray, spam@kdmurray.id.au
*
* ============================================================================
*/
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <qes_file.h>
#include <qes_seqfile.h>
#ifdef ZLIB_FOUND
# include <zlib.h>
#else
# include <sys/stat.h>
# include <fcntl.h>
#endif
#include "helpers.h"
#include "kseq.h"
void bench_qes_file_readline_realloc_file(int silent);
void bench_qes_file_readline_file(int silent);
#ifdef GELINE_FOUND
void bench_gnu_getline_file(int silent);
#endif
void bench_qes_seqfile_parse_fq(int silent);
void bench_kseq_parse_fq(int silent);
void bench_qes_seqfile_write(int silent);
#ifdef OPENMP_FOUND
void bench_qes_seqfile_par_iter_fq_macro(int silent);
#endif
#ifdef ZLIB_FOUND
KSEQ_INIT(gzFile, gzread)
#else
KSEQ_INIT(int, read)
#endif
static char *infile;
typedef struct __bench {
const char *name;
void (*fn)(int silent);
} bench_t;
void
bench_qes_file_readline_realloc_file(int silent)
{
size_t bsz = 1<<4;
char *buf = malloc(bsz);
ssize_t len = 0;
off_t flen = 0;
struct qes_file *file = qes_file_open(infile, "r");
assert(buf != NULL);
while ((len = qes_file_readline_realloc(file, &buf, &bsz)) != EOF) {
flen += len;
}
if (!silent)
printf("[qes_file_readline_realloc]\tFile of %lu chars\n",
(long unsigned)flen);
qes_file_close(file);
free(buf);
}
void
bench_qes_file_readline_file(int silent)
{
size_t bsz = 1<<10;
char buf[bsz];
ssize_t len = 0;
off_t flen = 0;
struct qes_file *file = qes_file_open(infile, "r");
while ((len = qes_file_readline(file, buf, bsz)) != EOF) {
flen += len;
}
if (!silent)
printf("[qes_file_readline]\t\tFile of %lu chars\n",
(long unsigned)flen);
qes_file_close(file);
}
#ifdef GELINE_FOUND
void
bench_gnu_getline_file(int silent)
{
size_t bsz = 1<<4;
char *buf = malloc(bsz);
ssize_t len = 0;
off_t flen = 0;
FILE *file = fopen(infile, "r");
assert(buf != NULL);
while ((len = getline(&buf, &bsz, file)) != EOF) {
flen += len;
}
if (!silent)
printf("[getline]\t\tFile of %lu chars\n",
(long unsigned)flen);
fclose(file);
free(buf);
}
#endif
#ifdef OPENMP_FOUND
void
bench_qes_seqfile_par_iter_fq_macro(int silent)
{
struct qes_seqfile *sf = qes_seqfile_create(infile, "r");
size_t total_len = 0;
QES_SEQFILE_ITER_PARALLEL_SINGLE_BEGIN(sf, seq, seq_len, shared(total_len))
#pragma omp critical
{
total_len += seq->seq.len;
}
QES_SEQFILE_ITER_PARALLEL_SINGLE_END(seq)
if (!silent) {
printf("[qes_seqfile_iter_fq_macro] Total seq len %lu\n",
(long unsigned)total_len);
}
qes_seqfile_destroy(sf);
}
#endif
void
bench_qes_seqfile_parse_fq(int silent)
{
struct qes_seq *seq = qes_seq_create();
struct qes_seqfile *sf = qes_seqfile_create(infile, "r");
ssize_t res = 0;
size_t n_recs = 0;
size_t seq_len = 0;
while (res != EOF) {
res = qes_seqfile_read(sf, seq);
if (res < 1) {
break;
}
seq_len += res;
n_recs++;
}
if (!silent) {
printf("[qes_seqfile_fq] Total seq len %lu\n",
(long unsigned)seq_len);
}
qes_seqfile_destroy(sf);
qes_seq_destroy(seq);
}
void
bench_kseq_parse_fq(int silent)
{
#ifdef ZLIB_FOUND
gzFile fp = gzopen(infile, "r");
#else
int fp = open(infile, O_RDONLY);
#endif
kseq_t *seq = kseq_init(fp);
ssize_t res = 0;
size_t n_recs = 0;
size_t seq_len = 0;
while ((res = kseq_read(seq)) >= 0) {
seq_len += res;
n_recs++;
}
if (!silent) {
printf("[kseq_fq] Total seq len %lu\n",
(long unsigned) seq_len);
}
kseq_destroy(seq);
#ifdef ZLIB_FOUND
gzclose(fp);
#else
close(fp);
#endif
}
void
bench_qes_seqfile_write(int silent)
{
struct qes_seq *seq = qes_seq_create();
ssize_t res = 0;
struct qes_seqfile *sf = NULL;
char *fname = tmpnam(NULL);
size_t iii = 0;
/* Make a seq to write */
qes_seq_fill_name(seq, "HWI-TEST", 8);
qes_seq_fill_comment(seq, "testseq 1 2 3", 13);
qes_seq_fill_seq(seq, "ACTCAATT", 8);
qes_seq_fill_qual(seq, "IIIIIIII", 8);
/* Setup file for writing */
sf = qes_seqfile_create(fname, "wT");
qes_seqfile_set_format(sf, FASTQ_FMT);
for (iii = 0; iii < 1<<11; iii++) {
res += qes_seqfile_write(sf, seq);
}
if (!silent) {
printf("[qes_seqfile_write] Total file len %lu to %s\n",
(long unsigned)res, fname);
}
qes_seqfile_destroy(sf);
qes_seq_destroy(seq);
remove(fname);
}
static const bench_t benchmarks[] = {
{ "qes_file_readline", &bench_qes_file_readline_file},
{ "qes_file_readline_realloc", &bench_qes_file_readline_realloc_file},
#ifdef GELINE_FOUND
{ "gnu_getline", &bench_gnu_getline_file},
#endif
{ "qes_seqfile_parse_fq", &bench_qes_seqfile_parse_fq},
#ifdef OPENMP_FOUND
{ "qes_seqfile_par_iter_fq_macro", &bench_qes_seqfile_par_iter_fq_macro},
#endif
{ "kseq_parse_fq", &bench_kseq_parse_fq},
{ "qes_seqfile_write", &bench_qes_seqfile_write},
{ NULL, NULL}
};
int
main (int argc, char *argv[])
{
bench_t thisbench;
clock_t start, end;
size_t iii = 0;
int rnds = 0;
size_t nbench = 0;
if (argc == 1) {
fprintf(stderr, "USAGE:\nbench <file> <rounds> <bench> [<bench> ...]\n\n");
fprintf(stderr, "\nAvalilable benchmarks are:\n");
nbench = 0;
while (1) {
thisbench = benchmarks[nbench++];
if ((thisbench.name && thisbench.fn)) {
fprintf(stderr, "%s\n", thisbench.name);
} else {
return EXIT_FAILURE;
}
}
}
if (argc < 4) {
fprintf(stderr, "USAGE:\nbench <file> <rounds> <bench> [<bench> ...]\n\n");
return EXIT_FAILURE;
} else {
infile = strdup(argv[1]);
rnds = atoi(argv[2]);
printf("Beginning benchmarks.\n");
printf("---------------------------------------------------------------------\n");
}
for (iii = 3; iii < (unsigned int) argc; iii++) {
nbench = 0;
while (1) {
thisbench = benchmarks[nbench++];
if (!(thisbench.name && thisbench.fn)) {
fprintf(stderr, "bad benchmark %s\n", argv[iii]);
break;
}
if (strcmp(argv[iii], thisbench.name) == 0) {
int rnd = 0;
start = clock();
for (rnd = 0; rnd<rnds; rnd++) {
(*thisbench.fn)(rnd != rnds - 1);
}
end = clock();
printf("Benchmark %s took %0.6fs per round [%d rounds]\n",
thisbench.name,
(float)(end - start) / (float)(CLOCKS_PER_SEC * rnds),
rnds);
printf("---------------------------------------------------------------------\n");
break;
}
}
}
free(infile);
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
|