2
0

cost.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. g++ -g -O0 cost.cpp ../../objs/st/libst.a -I../../objs/st -o cost && ./cost | grep COST
  3. */
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <st.h>
  9. #define SRS_UTIME_MILLISECONDS 1000
  10. #define srsu2i(us) ((int)(us))
  11. #define srsu2ms(us) ((us) / SRS_UTIME_MILLISECONDS)
  12. #define srsu2msi(us) int((us) / SRS_UTIME_MILLISECONDS)
  13. int64_t srs_update_system_time()
  14. {
  15. timeval now;
  16. ::gettimeofday(&now, NULL);
  17. return ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;
  18. }
  19. int main(int argc, char** argv)
  20. {
  21. // The cost for srs_update_system_time() itself.
  22. if (true) {
  23. int64_t start = srs_update_system_time();
  24. int64_t ts_gettimeofday = srs_update_system_time();
  25. printf("[COST] gettimeofday=%dus\n", srsu2i(ts_gettimeofday - start));
  26. }
  27. // The cost for allocate 1MB memory.
  28. if (true) {
  29. int64_t start = srs_update_system_time();
  30. int size = 1024 * 1024;
  31. char* p = new char[size];
  32. int64_t ts_allocate = srs_update_system_time();
  33. for (int i = 0; i < size; i++) {
  34. p[i] = 0x0F;
  35. }
  36. int64_t ts_init = srs_update_system_time();
  37. printf("[COST] new[%d]=%dus, init=%dus\n",
  38. size,
  39. srsu2i(ts_allocate - start),
  40. srsu2i(ts_init - ts_allocate)
  41. );
  42. }
  43. // The cost for loop.
  44. if (true) {
  45. int64_t start = srs_update_system_time();
  46. for (long long i = 0; i < 1000000LL; i++);
  47. int64_t ts_loop = srs_update_system_time();
  48. for (long long i = 0; i < 10000000LL; i++);
  49. int64_t ts_loop2 = srs_update_system_time();
  50. printf("[COST] loop 100w=%dus, 1000w=%dus\n", srsu2i(ts_loop - start), srsu2i(ts_loop2 - ts_loop));
  51. }
  52. // The cost for printf.
  53. if (true) {
  54. int64_t start = srs_update_system_time();
  55. printf("TEST: OK\n");
  56. int64_t ts_printf = srs_update_system_time();
  57. printf("TEST: OK OK\n");
  58. int64_t ts_printf2 = srs_update_system_time();
  59. printf("TEST: OK OK %s\n", "OK");
  60. int64_t ts_printf3 = srs_update_system_time();
  61. printf("[COST] printf=%dus %dus %dus\n",
  62. srsu2i(ts_printf - start),
  63. srsu2i(ts_printf2 - ts_printf),
  64. srsu2i(ts_printf3 - ts_printf2)
  65. );
  66. }
  67. // The cost for file open or close.
  68. if (true) {
  69. int64_t start = srs_update_system_time();
  70. int fd = ::open("cost.log", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  71. int64_t ts_open = srs_update_system_time();
  72. ::close(fd);
  73. int64_t ts_close = srs_update_system_time();
  74. printf("[COST] open=%dus, close=%dus\n",
  75. srsu2i(ts_open - start),
  76. srsu2i(ts_close - ts_open)
  77. );
  78. }
  79. // The cost for file writing.
  80. if (true) {
  81. int64_t start = srs_update_system_time();
  82. int fd = ::open("cost.log", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  83. int64_t ts_open = srs_update_system_time();
  84. ::write(fd, "Hello\n", 6);
  85. int64_t ts_write = srs_update_system_time();
  86. ::write(fd, "HelloHello\n", 12);
  87. int64_t ts_write2 = srs_update_system_time();
  88. ::close(fd);
  89. int64_t ts_close = srs_update_system_time();
  90. printf("[COST] write=%dus %dus\n",
  91. srsu2i(ts_write - ts_open),
  92. srsu2i(ts_write2 - ts_write)
  93. );
  94. }
  95. // The cost for file reading.
  96. if (true) {
  97. char buf[128];
  98. int64_t start = srs_update_system_time();
  99. int fd = ::open("cost.log", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  100. int64_t ts_open = srs_update_system_time();
  101. ::read(fd, buf, 6);
  102. int64_t ts_read = srs_update_system_time();
  103. ::read(fd, buf, 6);
  104. int64_t ts_read2 = srs_update_system_time();
  105. ::close(fd);
  106. int64_t ts_close = srs_update_system_time();
  107. printf("[COST] read=%dus %dus\n",
  108. srsu2i(ts_read - ts_open),
  109. srsu2i(ts_read2 - ts_read)
  110. );
  111. }
  112. // The cost for ST timer.
  113. st_set_eventsys(ST_EVENTSYS_ALT);
  114. st_init();
  115. for (;;) {
  116. int64_t start = srs_update_system_time();
  117. st_usleep(20 * 1000);
  118. int64_t cost = srs_update_system_time() - start;
  119. if (cost > (20 + 10) * 1000) {
  120. printf("[COST] timer=%dms\n", (int)(cost / 1000));
  121. }
  122. }
  123. return 0;
  124. }