Merge from vendor branch NCURSES:
[dragonfly.git] / test / sysperf / memcpy.c
1 /*
2  * memcpy.c
3  *
4  * $DragonFly: src/test/sysperf/memcpy.c,v 1.1 2004/04/29 16:14:53 dillon Exp $
5  */
6
7 #include "blib.h"
8
9 int glob[16384];
10
11 void test_using(const char *ctl, char *buf, int bytes, void (*copyf)(const void *s1, void *d, size_t bytes));
12
13 extern void docopy1(const void *s, void *d, size_t bytes);
14 extern void docopy2(const void *s, void *d, size_t bytes);
15 extern void docopy3(const void *s, void *d, size_t bytes);
16 extern void docopy4(const void *s, void *d, size_t bytes);
17 extern void docopy5(const void *s, void *d, size_t bytes);
18 extern void docopy6(const void *s, void *d, size_t bytes);
19 extern void docopy7(const void *s, void *d, size_t bytes);
20 extern void fpcleanup(void);
21
22 int
23 main(int ac, char **av)
24 {
25     int bytes;
26     char *ptr;
27     char *buf;
28
29     if (ac == 1) {
30         fprintf(stderr, "%s bytes\n", av[0]);
31         exit(1);
32     }
33
34     bytes = strtol(av[1], &ptr, 0);
35     switch(*ptr) {
36     case 'k':
37     case 'K':
38         bytes *= 1024;
39         break;
40     case 'm':
41     case 'M':
42         bytes *= 1024 * 1024;
43         break;
44     case 'g':
45     case 'G':
46         bytes *= 1024 * 1024 * 1024;
47         break;
48     case 0:
49         break;
50     default:
51         fprintf(stderr, "suffix '%s' not understood\n");
52         exit(1);
53     }
54     if (bytes <= 0 && (bytes & 127)) {
55         fprintf(stderr, "# of bytes must be a multiple of 128\n");
56         exit(1);
57     }
58     buf = mmap(NULL, bytes * 2, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
59     if (buf == MAP_FAILED) {
60         perror("mmap/buffer");
61         exit(1);
62     }
63     bzero(buf, bytes * 2);
64
65     test_using("bcopy", buf, bytes, bcopy);
66     test_using("docopy1", buf, bytes, docopy1);
67     test_using("docopy2", buf, bytes, docopy2);
68     test_using("docopy3", buf, bytes, docopy3);
69     test_using("docopy4", buf, bytes, docopy4);
70     test_using("docopy5", buf, bytes, docopy5);
71     test_using("docopy6", buf, bytes, docopy6);
72     test_using("docopy7", buf, bytes, docopy7);
73     return(0);
74 }
75
76 void
77 test_using(const char *ctl, char *buf, int bytes, void (*copyf)(const void *s1, void *d, size_t bytes))
78 {
79     int i;
80     int loops;
81     long long us;
82
83     start_timing();
84     for (i = 0; (i & 31) || stop_timing(0, NULL) == 0; ++i) {
85         copyf(buf, buf + bytes, bytes);
86     }
87
88     loops = i * 2;
89     start_timing();
90     for (i = loops - 1; i >= 0; --i) {
91         copyf(buf, buf + bytes, bytes);
92     }
93     fpcleanup();
94     stop_timing(loops, ctl);
95     us = get_timing();
96     printf("%s %d %5.2f MBytes/sec\n", ctl, bytes, 
97         (double)loops * (double)bytes / (double)us);
98 }
99