From 2b83af9e87002b77db85a39125ffa971a843aaa2 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Sat, 3 May 2008 00:46:33 +0200 Subject: [PATCH] New programs to benchmark the transfert rate. The results are really interesting! - bench-bandwidth-slave.c - bench-bandwidth-master.c --- tests/Makefile.am | 10 +++++++++- tests/bench-bandwidth-master.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/bench-bandwidth-slave.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/wscript | 14 ++++++++++++++ 4 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 tests/bench-bandwidth-master.c create mode 100644 tests/bench-bandwidth-slave.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 4b9af06..fe1cca8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,7 +1,9 @@ noinst_PROGRAMS = \ unit-test-master \ unit-test-slave \ - test-master-random + test-master-random \ + bench-bandwidth-slave \ + bench-bandwidth-master common_ldflags = \ $(top_builddir)/modbus/libmodbus.la @@ -15,5 +17,11 @@ unit_test_slave_LDADD = $(common_ldflags) test_master_random_SOURCES = test-master-random.c test_master_random_LDADD = $(common_ldflags) +bench_bandwidth_slave_SOURCES = bench-bandwidth-slave.c +bench_bandwidth_slave_LDADD = $(common_ldflags) + +bench_bandwidth_master_SOURCES = bench-bandwidth-master.c +bench_bandwidth_master_LDADD = $(common_ldflags) + INCLUDES = -I$(top_srcdir) CLEANFILES = *~ diff --git a/tests/bench-bandwidth-master.c b/tests/bench-bandwidth-master.c new file mode 100644 index 0000000..e23d502 --- /dev/null +++ b/tests/bench-bandwidth-master.c @@ -0,0 +1,141 @@ +/* + * Copyright © 2008 Stéphane Raimbault + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include + +/* Tests based on PI-MBUS-300 documentation */ +#define SLAVE 0x11 +#define NB_LOOPS 1000 + +#define G_USEC_PER_SEC 1000000 +uint32_t gettime(void) +{ + struct timeval tv; + gettimeofday (&tv, NULL); + + return (uint32_t) tv.tv_sec * G_USEC_PER_SEC + tv.tv_usec; +} + +int main(void) +{ + uint8_t *tab_rp_status; + uint16_t *tab_rp_registers; + modbus_param_t mb_param; + int i; + int ret; + int nb_points; + double elapsed; + uint32_t start; + uint32_t end; + uint32_t bytes; + uint32_t rate; + + /* TCP */ + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); + + modbus_connect(&mb_param); + + /* Allocate and initialize the memory to store the status */ + tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t)); + memset(tab_rp_status, 0, MAX_STATUS * sizeof(uint8_t)); + + /* Allocate and initialize the memory to store the registers */ + tab_rp_registers = (uint16_t *) malloc(MAX_REGISTERS * sizeof(uint16_t)); + memset(tab_rp_registers, 0, MAX_REGISTERS * sizeof(uint16_t)); + + printf("READ COIL STATUS\n\n"); + + nb_points = MAX_STATUS; + start = gettime(); + for (i=0; i + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include + +#include + +int main(void) +{ + int socket; + modbus_param_t mb_param; + modbus_mapping_t mb_mapping; + int ret; + + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); + + ret = modbus_mapping_new(&mb_mapping, MAX_STATUS, 0, MAX_REGISTERS, 0); + if (ret == FALSE) { + printf("Memory allocation failed\n"); + exit(1); + } + + socket = modbus_init_listen_tcp(&mb_param); + + while (1) { + uint8_t query[MAX_MESSAGE_LENGTH]; + int query_size; + + ret = modbus_listen(&mb_param, query, &query_size); + if (ret == 0) { + manage_query(&mb_param, query, query_size, &mb_mapping); + } else if (ret == CONNECTION_CLOSED) { + /* Connection closed by the client, end of server */ + break; + } else { + printf("Error in modbus_listen (%d)\n", ret); + } + } + + close(socket); + modbus_mapping_free(&mb_mapping); + modbus_close(&mb_param); + + return 0; +} + diff --git a/tests/wscript b/tests/wscript index 33718e8..68fa73b 100644 --- a/tests/wscript +++ b/tests/wscript @@ -19,3 +19,17 @@ def build(bld): obj.uselib_local = 'modbus' obj.target = 'test-master-random' obj.inst_var = 0 + + obj = bld.create_obj('cc', 'program') + obj.source = 'bench-bandwidth-slave.c' + obj.includes = '. ..' + obj.uselib_local = 'modbus' + obj.target = 'bench-bandwidth-slave' + obj.inst_var = 0 + + obj = bld.create_obj('cc', 'program') + obj.source = 'bench-bandwidth-master.c' + obj.includes = '. ..' + obj.uselib_local = 'modbus' + obj.target = 'bench-bandwidth-master' + obj.inst_var = 0 -- libgit2 0.21.4