2 sptps_speed.c -- SPTPS benchmark
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 // Symbols necessary to link with logger.o
31 bool send_request(void *c, const char *msg, ...) { return false; }
33 bool send_meta(void *c, const char *msg , int len) { return false; }
34 char *logfilename = NULL;
37 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
38 int fd = *(int *)handle;
39 send(fd, data, len, 0);
43 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
47 static void receive_data(sptps_t *sptps) {
49 int fd = *(int *)sptps->handle;
50 size_t len = recv(fd, buf, sizeof buf, 0);
51 if(!sptps_receive_data(sptps, buf, len))
55 struct timespec start;
61 static void clock_start() {
63 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
66 static bool clock_countto(double seconds) {
67 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
68 elapsed = end.tv_sec + end.tv_nsec * 1e-9 - start.tv_sec - start.tv_nsec * 1e-9;
72 rate = count / elapsed;
76 int main(int argc, char *argv[]) {
78 ecdh_t *ecdh1, *ecdh2;
79 sptps_t sptps1, sptps2;
80 char buf1[4096], buf2[4096], buf3[4096];
81 double duration = argc > 1 ? atof(argv[1]) : 10;
85 randomize(buf1, sizeof buf1);
86 randomize(buf2, sizeof buf2);
87 randomize(buf3, sizeof buf3);
91 fprintf(stderr, "Generating keys for %lg seconds: ", duration);
92 for(clock_start(); clock_countto(duration);)
93 ecdsa_free(ecdsa_generate());
94 fprintf(stderr, "%17.2lf op/s\n", rate);
96 key1 = ecdsa_generate();
97 key2 = ecdsa_generate();
101 fprintf(stderr, "ECDSA sign for %lg seconds: ", duration);
102 for(clock_start(); clock_countto(duration);)
103 ecdsa_sign(key1, buf1, 256, buf2);
104 fprintf(stderr, "%22.2lf op/s\n", rate);
106 fprintf(stderr, "ECDSA verify for %lg seconds: ", duration);
107 for(clock_start(); clock_countto(duration);)
108 ecdsa_verify(key1, buf1, 256, buf2);
109 fprintf(stderr, "%20.2lf op/s\n", rate);
111 ecdh1 = ecdh_generate_public(buf1);
112 fprintf(stderr, "ECDH for %lg seconds: ", duration);
113 for(clock_start(); clock_countto(duration);) {
114 ecdh2 = ecdh_generate_public(buf2);
115 ecdh_compute_shared(ecdh2, buf1, buf3);
117 fprintf(stderr, "%28.2lf op/s\n", rate);
120 // SPTPS authentication phase
123 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
124 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
128 struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
130 fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
131 for(clock_start(); clock_countto(duration);) {
132 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
133 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
134 while(poll(pfd, 2, 0)) {
136 receive_data(&sptps1);
138 receive_data(&sptps2);
143 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
147 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
148 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
149 while(poll(pfd, 2, 0)) {
151 receive_data(&sptps1);
153 receive_data(&sptps2);
155 fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
156 for(clock_start(); clock_countto(duration);) {
157 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
159 receive_data(&sptps2);
161 rate *= 2 * 1451 * 8;
163 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
165 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
167 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
171 // SPTPS datagram authentication phase
176 if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
177 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
181 fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
182 for(clock_start(); clock_countto(duration);) {
183 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
184 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
185 while(poll(pfd, 2, 0)) {
187 receive_data(&sptps1);
189 receive_data(&sptps2);
194 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
196 // SPTPS datagram data
198 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
199 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
200 while(poll(pfd, 2, 0)) {
202 receive_data(&sptps1);
204 receive_data(&sptps2);
206 fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
207 for(clock_start(); clock_countto(duration);) {
208 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
210 receive_data(&sptps2);
212 rate *= 2 * 1451 * 8;
214 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
216 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
218 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);