bench-bandwidth-master.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright © 2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include <modbus/modbus.h>
  24. /* Tests based on PI-MBUS-300 documentation */
  25. #define SLAVE 0x11
  26. #define NB_LOOPS 1000
  27. #define G_USEC_PER_SEC 1000000
  28. uint32_t gettime(void)
  29. {
  30. struct timeval tv;
  31. gettimeofday (&tv, NULL);
  32. return (uint32_t) tv.tv_sec * G_USEC_PER_SEC + tv.tv_usec;
  33. }
  34. int main(void)
  35. {
  36. uint8_t *tab_rp_status;
  37. uint16_t *tab_rp_registers;
  38. modbus_param_t mb_param;
  39. int i;
  40. int ret;
  41. int nb_points;
  42. double elapsed;
  43. uint32_t start;
  44. uint32_t end;
  45. uint32_t bytes;
  46. uint32_t rate;
  47. /* TCP */
  48. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  49. modbus_connect(&mb_param);
  50. /* Allocate and initialize the memory to store the status */
  51. tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t));
  52. memset(tab_rp_status, 0, MAX_STATUS * sizeof(uint8_t));
  53. /* Allocate and initialize the memory to store the registers */
  54. tab_rp_registers = (uint16_t *) malloc(MAX_REGISTERS * sizeof(uint16_t));
  55. memset(tab_rp_registers, 0, MAX_REGISTERS * sizeof(uint16_t));
  56. printf("READ COIL STATUS\n\n");
  57. nb_points = MAX_STATUS;
  58. start = gettime();
  59. for (i=0; i<NB_LOOPS; i++) {
  60. ret = read_coil_status(&mb_param, SLAVE, 0, nb_points, tab_rp_status);
  61. }
  62. end = gettime();
  63. elapsed = (end - start) / 1000;
  64. rate = (NB_LOOPS * nb_points) * G_USEC_PER_SEC / (end - start);
  65. printf("Transfert rate in points/seconds:\n");
  66. printf("* %'d points/s\n", rate);
  67. printf("\n");
  68. bytes = NB_LOOPS * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  69. rate = bytes / 1024 * G_USEC_PER_SEC / (end - start);
  70. printf("Values:\n");
  71. printf("* %d x %d values\n", NB_LOOPS, nb_points);
  72. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  73. printf("* %'d KiB/s\n", rate);
  74. printf("\n");
  75. /* TCP: Query and reponse header and values */
  76. bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  77. printf("Values and TCP Modbus overhead:\n");
  78. printf("* %d x %d bytes\n", NB_LOOPS, bytes);
  79. bytes = NB_LOOPS * bytes;
  80. rate = bytes / 1024 * G_USEC_PER_SEC / (end - start);
  81. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  82. printf("* %'d KiB/s\n", rate);
  83. printf("\n\n");
  84. printf("READ HOLDING REGISTERS\n\n");
  85. nb_points = MAX_REGISTERS;
  86. start = gettime();
  87. for (i=0; i<NB_LOOPS; i++) {
  88. ret = read_holding_registers(&mb_param, SLAVE, 0, nb_points, tab_rp_registers);
  89. }
  90. end = gettime();
  91. elapsed = (end - start) / 1000;
  92. rate = (NB_LOOPS * nb_points) * G_USEC_PER_SEC / (end - start);
  93. printf("Transfert rate in points/seconds:\n");
  94. printf("* %'d registers/s\n", rate);
  95. printf("\n");
  96. bytes = NB_LOOPS * nb_points * sizeof(uint16_t);
  97. rate = bytes / 1024 * G_USEC_PER_SEC / (end - start);
  98. printf("Values:\n");
  99. printf("* %d x %d values\n", NB_LOOPS, nb_points);
  100. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  101. printf("* %'d KiB/s\n", rate);
  102. printf("\n");
  103. /* TCP:Query and reponse header and values */
  104. bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
  105. printf("Values and TCP Modbus overhead:\n");
  106. printf("* %d x %d bytes\n", NB_LOOPS, bytes);
  107. bytes = NB_LOOPS * bytes;
  108. rate = bytes / 1024 * G_USEC_PER_SEC / (end - start);
  109. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  110. printf("* %'d KiB/s\n", rate);
  111. printf("\n");
  112. /* Free the memory */
  113. free(tab_rp_status);
  114. free(tab_rp_registers);
  115. /* Close the connection */
  116. modbus_close(&mb_param);
  117. return 0;
  118. }