2 sptps_speed.c -- SPTPS benchmark
3 Copyright (C) 2013 Guus Sliepen <guus@tinc-vpn.org>,
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; }
32 struct list_t *connection_list = NULL;
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 char *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 char *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 sptps_receive_data(sptps, buf, len);
54 struct timespec start;
60 static void clock_start() {
62 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
65 static bool clock_countto(int seconds) {
66 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
67 elapsed = end.tv_sec + end.tv_nsec * 1e-9 - start.tv_sec - start.tv_nsec * 1e-9;
71 rate = count / elapsed;
75 int main(int argc, char *argv[]) {
77 ecdh_t *ecdh1, *ecdh2;
78 sptps_t sptps1, sptps2;
79 char buf1[4096], buf2[4096], buf3[4096];
85 fprintf(stderr, "Generating keys for 10 seconds: ");
86 for(clock_start(); clock_countto(10);)
87 ecdsa_free(ecdsa_generate());
88 fprintf(stderr, "%13.2lf op/s\n", rate);
90 key1 = ecdsa_generate();
91 key2 = ecdsa_generate();
95 fprintf(stderr, "ECDSA sign for 10 seconds: ");
96 for(clock_start(); clock_countto(10);)
97 ecdsa_sign(key1, buf1, 256, buf2);
98 fprintf(stderr, "%18.2lf op/s\n", rate);
100 fprintf(stderr, "ECDSA verify for 10 seconds: ");
101 for(clock_start(); clock_countto(10);)
102 ecdsa_verify(key1, buf1, 256, buf2);
103 fprintf(stderr, "%16.2lf op/s\n", rate);
105 ecdh1 = ecdh_generate_public(buf1);
106 fprintf(stderr, "ECDH for 10 seconds: ");
107 for(clock_start(); clock_countto(10);) {
108 ecdh2 = ecdh_generate_public(buf2);
109 ecdh_compute_shared(ecdh2, buf1, buf3);
111 fprintf(stderr, "%24.2lf op/s\n", rate);
114 // SPTPS authentication phase
117 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
118 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
122 struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
124 fprintf(stderr, "SPTPS authenticate for 10 seconds: ");
125 for(clock_start(); clock_countto(10);) {
126 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
127 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
128 while(poll(pfd, 2, 0)) {
130 receive_data(&sptps1);
132 receive_data(&sptps2);
137 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
141 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
142 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
143 while(poll(pfd, 2, 0)) {
145 receive_data(&sptps1);
147 receive_data(&sptps2);
149 fprintf(stderr, "SPTPS transmit for 10 seconds: ");
150 for(clock_start(); clock_countto(10);) {
151 sptps_send_record(&sptps1, 0, buf1, 1451);
152 receive_data(&sptps2);
154 rate *= 2 * 1451 * 8;
156 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
158 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
160 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);