diff options
| author | millert <> | 2017-05-22 17:52:16 +0000 |
|---|---|---|
| committer | millert <> | 2017-05-22 17:52:16 +0000 |
| commit | bda66dbfbd399c867174f3c429d4b37e15f908d9 (patch) | |
| tree | 19e5741835813218f14fb8c5a8bd3d2a12ff8c7f /src | |
| parent | c6a610ee008733eeb3365f48f5063d48e0f0825d (diff) | |
| download | openbsd-bda66dbfbd399c867174f3c429d4b37e15f908d9.tar.gz openbsd-bda66dbfbd399c867174f3c429d4b37e15f908d9.tar.bz2 openbsd-bda66dbfbd399c867174f3c429d4b37e15f908d9.zip | |
Add timing and test name options.
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libc/qsort/qsort_test.c | 95 |
1 files changed, 83 insertions, 12 deletions
diff --git a/src/regress/lib/libc/qsort/qsort_test.c b/src/regress/lib/libc/qsort/qsort_test.c index 45566353a7..03d73216bd 100644 --- a/src/regress/lib/libc/qsort/qsort_test.c +++ b/src/regress/lib/libc/qsort/qsort_test.c | |||
| @@ -14,11 +14,14 @@ | |||
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ | 15 | */ |
| 16 | 16 | ||
| 17 | #include <sys/time.h> | ||
| 18 | |||
| 17 | #include <stdbool.h> | 19 | #include <stdbool.h> |
| 18 | #include <stdio.h> | 20 | #include <stdio.h> |
| 19 | #include <stdlib.h> | 21 | #include <stdlib.h> |
| 20 | #include <string.h> | 22 | #include <string.h> |
| 21 | #include <setjmp.h> | 23 | #include <setjmp.h> |
| 24 | #include <time.h> | ||
| 22 | #include <unistd.h> | 25 | #include <unistd.h> |
| 23 | #include <err.h> | 26 | #include <err.h> |
| 24 | 27 | ||
| @@ -44,7 +47,7 @@ static size_t compares; | |||
| 44 | static size_t max_compares; | 47 | static size_t max_compares; |
| 45 | static size_t abrt_compares; | 48 | static size_t abrt_compares; |
| 46 | static sigjmp_buf cmpjmp; | 49 | static sigjmp_buf cmpjmp; |
| 47 | static bool dump_table, verbose; | 50 | static bool dump_table, timing, verbose; |
| 48 | 51 | ||
| 49 | extern int antiqsort(int n, int *a, int *ptr); | 52 | extern int antiqsort(int n, int *a, int *ptr); |
| 50 | 53 | ||
| @@ -77,11 +80,25 @@ check_result(char *sub, int *got, int *expected, struct test_distribution *d, | |||
| 77 | 80 | ||
| 78 | if (verbose || compares > max_compares) { | 81 | if (verbose || compares > max_compares) { |
| 79 | if (sub != NULL) { | 82 | if (sub != NULL) { |
| 80 | warnx("%s (%s): m: %d, n: %d, %zu compares, max %zu(%zu)", | 83 | if (m != 0) { |
| 81 | d->name, sub, m, n, compares, max_compares, abrt_compares); | 84 | warnx("%s (%s): m: %d, n: %d, %zu compares, " |
| 85 | "max %zu(%zu)", d->name, sub, m, n, | ||
| 86 | compares, max_compares, abrt_compares); | ||
| 87 | } else { | ||
| 88 | warnx("%s (%s): n: %d, %zu compares, " | ||
| 89 | "max %zu(%zu)", d->name, sub, n, | ||
| 90 | compares, max_compares, abrt_compares); | ||
| 91 | } | ||
| 82 | } else { | 92 | } else { |
| 83 | warnx("%s: m: %d, n: %d, %zu compares, max %zu(%zu)", | 93 | if (m != 0) { |
| 84 | d->name, m, n, compares, max_compares, abrt_compares); | 94 | warnx("%s: m: %d, n: %d, %zu compares, " |
| 95 | "max %zu(%zu)", d->name, m, n, | ||
| 96 | compares, max_compares, abrt_compares); | ||
| 97 | } else { | ||
| 98 | warnx("%s: n: %d, %zu compares, " | ||
| 99 | "max %zu(%zu)", d->name, n, | ||
| 100 | compares, max_compares, abrt_compares); | ||
| 101 | } | ||
| 85 | } | 102 | } |
| 86 | } | 103 | } |
| 87 | 104 | ||
| @@ -188,10 +205,33 @@ fill_med3_killer(int *x, int n, int m) | |||
| 188 | } | 205 | } |
| 189 | } | 206 | } |
| 190 | 207 | ||
| 208 | static void | ||
| 209 | print_timing(struct test_distribution *d, char *sub, int m, int n, double elapsed) | ||
| 210 | { | ||
| 211 | if (sub != NULL) { | ||
| 212 | if (m != 0) { | ||
| 213 | warnx("%s (%s): m: %d, n: %d, %f seconds", | ||
| 214 | d->name, sub, m, n, elapsed); | ||
| 215 | } else { | ||
| 216 | warnx("%s (%s): n: %d, %f seconds", | ||
| 217 | d->name, sub, n, elapsed); | ||
| 218 | } | ||
| 219 | } else { | ||
| 220 | if (m != 0) { | ||
| 221 | warnx("%s: m: %d, n: %d, %f seconds", | ||
| 222 | d->name, m, n, elapsed); | ||
| 223 | } else { | ||
| 224 | warnx("%s: n: %d, %f seconds", | ||
| 225 | d->name, n, elapsed); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 191 | static int | 230 | static int |
| 192 | do_test(struct test_distribution *d, char *sub, int m, int n, int *y, int *z) | 231 | do_test(struct test_distribution *d, char *sub, int m, int n, int *y, int *z) |
| 193 | { | 232 | { |
| 194 | int ret = 0; | 233 | int ret = 0; |
| 234 | struct timespec before, after; | ||
| 195 | 235 | ||
| 196 | compares = 0; | 236 | compares = 0; |
| 197 | if (sigsetjmp(cmpjmp, 1) != 0) { | 237 | if (sigsetjmp(cmpjmp, 1) != 0) { |
| @@ -204,7 +244,16 @@ do_test(struct test_distribution *d, char *sub, int m, int n, int *y, int *z) | |||
| 204 | } | 244 | } |
| 205 | ret = 1; | 245 | ret = 1; |
| 206 | } else { | 246 | } else { |
| 247 | if (timing) | ||
| 248 | clock_gettime(CLOCK_MONOTONIC, &before); | ||
| 207 | qsort(y, n, sizeof(y[0]), cmp_checked); | 249 | qsort(y, n, sizeof(y[0]), cmp_checked); |
| 250 | if (timing) { | ||
| 251 | double elapsed; | ||
| 252 | clock_gettime(CLOCK_MONOTONIC, &after); | ||
| 253 | timespecsub(&after, &before, &after); | ||
| 254 | elapsed = after.tv_sec + after.tv_nsec / 1000000000.0; | ||
| 255 | print_timing(d, sub, m, n, elapsed); | ||
| 256 | } | ||
| 208 | if (check_result(sub, y, z, d, m, n) != 0) | 257 | if (check_result(sub, y, z, d, m, n) != 0) |
| 209 | ret = 1; | 258 | ret = 1; |
| 210 | } | 259 | } |
| @@ -307,13 +356,18 @@ test_simple(struct test_distribution *d, int n, int *x, int *y, int *z) | |||
| 307 | static int | 356 | static int |
| 308 | test_antiqsort(struct test_distribution *d, int n, int *x, int *y, int *z) | 357 | test_antiqsort(struct test_distribution *d, int n, int *x, int *y, int *z) |
| 309 | { | 358 | { |
| 359 | struct timespec before, after; | ||
| 310 | int i, ret = 0; | 360 | int i, ret = 0; |
| 311 | 361 | ||
| 312 | /* | 362 | /* |
| 313 | * We expect antiqsort to generate > 1.5 * nlgn compares. | 363 | * We expect antiqsort to generate > 1.5 * nlgn compares. |
| 314 | * If introspection is not used, it will be > 10 * nlgn compares. | 364 | * If introspection is not used, it will be > 10 * nlgn compares. |
| 315 | */ | 365 | */ |
| 366 | if (timing) | ||
| 367 | clock_gettime(CLOCK_MONOTONIC, &before); | ||
| 316 | i = antiqsort(n, x, y); | 368 | i = antiqsort(n, x, y); |
| 369 | if (timing) | ||
| 370 | clock_gettime(CLOCK_MONOTONIC, &after); | ||
| 317 | if (i > abrt_compares) | 371 | if (i > abrt_compares) |
| 318 | ret = 1; | 372 | ret = 1; |
| 319 | if (dump_table) { | 373 | if (dump_table) { |
| @@ -325,9 +379,17 @@ test_antiqsort(struct test_distribution *d, int n, int *x, int *y, int *z) | |||
| 325 | printf("%4d, ", x[i]); | 379 | printf("%4d, ", x[i]); |
| 326 | } | 380 | } |
| 327 | printf("%4d\n};\n", x[i]); | 381 | printf("%4d\n};\n", x[i]); |
| 328 | } else if (verbose || ret != 0) { | 382 | } else { |
| 329 | warnx("%s: n: %d, %d compares, max %zu(%zu)", | 383 | if (timing) { |
| 330 | d->name, n, i, max_compares, abrt_compares); | 384 | double elapsed; |
| 385 | timespecsub(&after, &before, &after); | ||
| 386 | elapsed = after.tv_sec + after.tv_nsec / 1000000000.0; | ||
| 387 | print_timing(d, NULL, 0, n, elapsed); | ||
| 388 | } | ||
| 389 | if (verbose || ret != 0) { | ||
| 390 | warnx("%s: n: %d, %d compares, max %zu(%zu)", | ||
| 391 | d->name, n, i, max_compares, abrt_compares); | ||
| 392 | } | ||
| 331 | } | 393 | } |
| 332 | 394 | ||
| 333 | return ret; | 395 | return ret; |
| @@ -346,7 +408,7 @@ static struct test_distribution distributions[] = { | |||
| 346 | }; | 408 | }; |
| 347 | 409 | ||
| 348 | static int | 410 | static int |
| 349 | run_tests(int n) | 411 | run_tests(int n, const char *name) |
| 350 | { | 412 | { |
| 351 | int *x, *y, *z; | 413 | int *x, *y, *z; |
| 352 | int i, nlgn = 0; | 414 | int i, nlgn = 0; |
| @@ -371,6 +433,8 @@ run_tests(int n) | |||
| 371 | err(1, NULL); | 433 | err(1, NULL); |
| 372 | 434 | ||
| 373 | for (d = distributions; d->name != NULL; d++) { | 435 | for (d = distributions; d->name != NULL; d++) { |
| 436 | if (name != NULL && strcmp(name, d->name) != 0) | ||
| 437 | continue; | ||
| 374 | if (d->test(d, n, x, y, z) != 0) | 438 | if (d->test(d, n, x, y, z) != 0) |
| 375 | ret = 1; | 439 | ret = 1; |
| 376 | } | 440 | } |
| @@ -384,7 +448,7 @@ run_tests(int n) | |||
| 384 | __dead void | 448 | __dead void |
| 385 | usage(void) | 449 | usage(void) |
| 386 | { | 450 | { |
| 387 | fprintf(stderr, "usage: qsort_test [-dv] [num ...]\n"); | 451 | fprintf(stderr, "usage: qsort_test [-dvt] [-n test_name] [num ...]\n"); |
| 388 | exit(1); | 452 | exit(1); |
| 389 | } | 453 | } |
| 390 | 454 | ||
| @@ -393,12 +457,19 @@ main(int argc, char *argv[]) | |||
| 393 | { | 457 | { |
| 394 | char *nums[] = { "100", "1023", "1024", "1025", "4095", "4096", "4097" }; | 458 | char *nums[] = { "100", "1023", "1024", "1025", "4095", "4096", "4097" }; |
| 395 | int ch, n, ret = 0; | 459 | int ch, n, ret = 0; |
| 460 | char *name = NULL; | ||
| 396 | 461 | ||
| 397 | while ((ch = getopt(argc, argv, "dv")) != -1) { | 462 | while ((ch = getopt(argc, argv, "dn:tv")) != -1) { |
| 398 | switch (ch) { | 463 | switch (ch) { |
| 399 | case 'd': | 464 | case 'd': |
| 400 | dump_table = true; | 465 | dump_table = true; |
| 401 | break; | 466 | break; |
| 467 | case 'n': | ||
| 468 | name = optarg; | ||
| 469 | break; | ||
| 470 | case 't': | ||
| 471 | timing = true; | ||
| 472 | break; | ||
| 402 | case 'v': | 473 | case 'v': |
| 403 | verbose = true; | 474 | verbose = true; |
| 404 | break; | 475 | break; |
| @@ -417,7 +488,7 @@ main(int argc, char *argv[]) | |||
| 417 | 488 | ||
| 418 | while (argc > 0) { | 489 | while (argc > 0) { |
| 419 | n = atoi(*argv); | 490 | n = atoi(*argv); |
| 420 | if (run_tests(n) != 0) | 491 | if (run_tests(n, name) != 0) |
| 421 | ret = 1; | 492 | ret = 1; |
| 422 | argc--; | 493 | argc--; |
| 423 | argv++; | 494 | argv++; |
