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, ...) {
37 bool send_meta(void *c, const char *msg, int len) {
40 char *logfilename = NULL;
43 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
44 int fd = *(int *)handle;
45 send(fd, data, len, 0);
49 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
53 static void receive_data(sptps_t *sptps) {
55 int fd = *(int *)sptps->handle;
56 size_t len = recv(fd, buf, sizeof buf, 0);
57 if(!sptps_receive_data(sptps, buf, len))
61 struct timespec start;
67 static void clock_start() {
69 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
72 static bool clock_countto(double seconds) {
73 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
74 elapsed = end.tv_sec + end.tv_nsec * 1e-9 - start.tv_sec - start.tv_nsec * 1e-9;
78 rate = count / elapsed;
82 int main(int argc, char *argv[]) {
84 ecdh_t *ecdh1, *ecdh2;
85 sptps_t sptps1, sptps2;
86 char buf1[4096], buf2[4096], buf3[4096];
87 double duration = argc > 1 ? atof(argv[1]) : 10;
91 randomize(buf1, sizeof buf1);
92 randomize(buf2, sizeof buf2);
93 randomize(buf3, sizeof buf3);
97 fprintf(stderr, "Generating keys for %lg seconds: ", duration);
98 for(clock_start(); clock_countto(duration);)
99 ecdsa_free(ecdsa_generate());
100 fprintf(stderr, "%17.2lf op/s\n", rate);
102 key1 = ecdsa_generate();
103 key2 = ecdsa_generate();
107 fprintf(stderr, "ECDSA sign for %lg seconds: ", duration);
108 for(clock_start(); clock_countto(duration);)
109 ecdsa_sign(key1, buf1, 256, buf2);
110 fprintf(stderr, "%22.2lf op/s\n", rate);
112 fprintf(stderr, "ECDSA verify for %lg seconds: ", duration);
113 for(clock_start(); clock_countto(duration);)
114 ecdsa_verify(key1, buf1, 256, buf2);
115 fprintf(stderr, "%20.2lf op/s\n", rate);
117 ecdh1 = ecdh_generate_public(buf1);
118 fprintf(stderr, "ECDH for %lg seconds: ", duration);
119 for(clock_start(); clock_countto(duration);) {
120 ecdh2 = ecdh_generate_public(buf2);
121 ecdh_compute_shared(ecdh2, buf1, buf3);
123 fprintf(stderr, "%28.2lf op/s\n", rate);
126 // SPTPS authentication phase
129 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
130 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
134 struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
136 fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
137 for(clock_start(); clock_countto(duration);) {
138 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
139 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
140 while(poll(pfd, 2, 0)) {
142 receive_data(&sptps1);
144 receive_data(&sptps2);
149 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
153 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
154 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
155 while(poll(pfd, 2, 0)) {
157 receive_data(&sptps1);
159 receive_data(&sptps2);
161 fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
162 for(clock_start(); clock_countto(duration);) {
163 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
165 receive_data(&sptps2);
167 rate *= 2 * 1451 * 8;
169 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
171 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
173 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
177 // SPTPS datagram authentication phase
182 if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
183 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
187 fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
188 for(clock_start(); clock_countto(duration);) {
189 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
190 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
191 while(poll(pfd, 2, 0)) {
193 receive_data(&sptps1);
195 receive_data(&sptps2);
200 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
202 // SPTPS datagram data
204 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
205 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
206 while(poll(pfd, 2, 0)) {
208 receive_data(&sptps1);
210 receive_data(&sptps2);
212 fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
213 for(clock_start(); clock_countto(duration);) {
214 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
216 receive_data(&sptps2);
218 rate *= 2 * 1451 * 8;
220 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
222 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
224 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);