|
| 1 | +#include <time.h> |
| 2 | +#include "accelerator/apis.h" |
| 3 | +#include "test_define.h" |
| 4 | + |
| 5 | +iota_client_service_t service; |
| 6 | +struct timespec start_time, end_time; |
| 7 | + |
| 8 | +#if defined(ENABLE_STAT) |
| 9 | +#define TEST_COUNT 100 |
| 10 | +#else |
| 11 | +#define TEST_COUNT 1 |
| 12 | +#endif |
| 13 | + |
| 14 | +double diff_time(struct timespec start, struct timespec end) { |
| 15 | + struct timespec diff; |
| 16 | + if (end.tv_nsec - start.tv_nsec < 0) { |
| 17 | + diff.tv_sec = end.tv_sec - start.tv_sec - 1; |
| 18 | + diff.tv_nsec = end.tv_nsec - start.tv_nsec + 1000000000; |
| 19 | + } else { |
| 20 | + diff.tv_sec = end.tv_sec - start.tv_sec; |
| 21 | + diff.tv_nsec = end.tv_nsec - start.tv_nsec; |
| 22 | + } |
| 23 | + return (diff.tv_sec + diff.tv_nsec / 1000000000.0); |
| 24 | +} |
| 25 | + |
| 26 | +void test_generate_address(void) { |
| 27 | + char* json_result; |
| 28 | + double sum = 0; |
| 29 | + |
| 30 | + for (size_t count = 0; count < TEST_COUNT; count++) { |
| 31 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 32 | + TEST_ASSERT_FALSE(api_generate_address(&service, &json_result)); |
| 33 | + clock_gettime(CLOCK_REALTIME, &end_time); |
| 34 | +#if defined(ENABLE_STAT) |
| 35 | + printf("%lf\n", diff_time(start_time, end_time)); |
| 36 | +#endif |
| 37 | + sum += diff_time(start_time, end_time); |
| 38 | + } |
| 39 | + printf("Average time of generate_address: %lf\n", sum / TEST_COUNT); |
| 40 | + free(json_result); |
| 41 | +} |
| 42 | + |
| 43 | +void test_get_tips_pair(void) { |
| 44 | + char* json_result; |
| 45 | + double sum = 0; |
| 46 | + |
| 47 | + for (size_t count = 0; count < TEST_COUNT; count++) { |
| 48 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 49 | + TEST_ASSERT_FALSE(api_get_tips_pair(&service, &json_result)); |
| 50 | + clock_gettime(CLOCK_REALTIME, &end_time); |
| 51 | +#if defined(ENABLE_STAT) |
| 52 | + printf("%lf\n", diff_time(start_time, end_time)); |
| 53 | +#endif |
| 54 | + sum += diff_time(start_time, end_time); |
| 55 | + } |
| 56 | + printf("Average time of get_tips_pair: %lf\n", sum / TEST_COUNT); |
| 57 | + free(json_result); |
| 58 | +} |
| 59 | + |
| 60 | +void test_get_tips(void) { |
| 61 | + char* json_result; |
| 62 | + double sum = 0; |
| 63 | + |
| 64 | + for (size_t count = 0; count < TEST_COUNT; count++) { |
| 65 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 66 | + TEST_ASSERT_FALSE(api_get_tips(&service, &json_result)); |
| 67 | + clock_gettime(CLOCK_REALTIME, &end_time); |
| 68 | +#if defined(ENABLE_STAT) |
| 69 | + printf("%lf\n", diff_time(start_time, end_time)); |
| 70 | +#endif |
| 71 | + sum += diff_time(start_time, end_time); |
| 72 | + } |
| 73 | + printf("Average time of get_tips: %lf\n", sum / TEST_COUNT); |
| 74 | + free(json_result); |
| 75 | +} |
| 76 | + |
| 77 | +void test_send_transfer(void) { |
| 78 | + const char* json = |
| 79 | + "{\"value\":100," |
| 80 | + "\"message\":\"" TAG_MSG "\",\"tag\":\"" TAG_MSG |
| 81 | + "\"," |
| 82 | + "\"address\":\"" TRYTES_81_1 "\"}"; |
| 83 | + char* json_result; |
| 84 | + double sum = 0; |
| 85 | + |
| 86 | + for (size_t count = 0; count < TEST_COUNT; count++) { |
| 87 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 88 | + TEST_ASSERT_FALSE(api_send_transfer(&service, json, &json_result)); |
| 89 | + clock_gettime(CLOCK_REALTIME, &end_time); |
| 90 | +#if defined(ENABLE_STAT) |
| 91 | + printf("%lf\n", diff_time(start_time, end_time)); |
| 92 | +#endif |
| 93 | + sum += diff_time(start_time, end_time); |
| 94 | + } |
| 95 | + printf("Average time of send_transfer: %lf\n", sum / TEST_COUNT); |
| 96 | + free(json_result); |
| 97 | +} |
| 98 | + |
| 99 | +void test_get_transaction_object(void) { |
| 100 | + char* json_result; |
| 101 | + double sum = 0; |
| 102 | + |
| 103 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 104 | + for (size_t count = 0; count < TEST_COUNT; count++) { |
| 105 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 106 | + TEST_ASSERT_FALSE( |
| 107 | + api_get_transaction_object(&service, TRYTES_81_1, &json_result)); |
| 108 | + clock_gettime(CLOCK_REALTIME, &end_time); |
| 109 | +#if defined(ENABLE_STAT) |
| 110 | + printf("%lf\n", diff_time(start_time, end_time)); |
| 111 | +#endif |
| 112 | + sum += diff_time(start_time, end_time); |
| 113 | + } |
| 114 | + printf("Average time of get_transaction_object: %lf\n", sum / TEST_COUNT); |
| 115 | + free(json_result); |
| 116 | +} |
| 117 | + |
| 118 | +void test_find_transactions_by_tag(void) { |
| 119 | + char* json_result; |
| 120 | + double sum = 0; |
| 121 | + |
| 122 | + for (size_t count = 0; count < TEST_COUNT; count++) { |
| 123 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 124 | + TEST_ASSERT_FALSE( |
| 125 | + api_find_transactions_by_tag(&service, TAG_MSG, &json_result)); |
| 126 | + clock_gettime(CLOCK_REALTIME, &end_time); |
| 127 | +#if defined(ENABLE_STAT) |
| 128 | + printf("%lf\n", diff_time(start_time, end_time)); |
| 129 | +#endif |
| 130 | + sum += diff_time(start_time, end_time); |
| 131 | + } |
| 132 | + printf("Average time of find_transactions_by_tag: %lf\n", sum / TEST_COUNT); |
| 133 | + free(json_result); |
| 134 | +} |
| 135 | + |
| 136 | +void test_find_transactions_obj_by_tag(void) { |
| 137 | + char* json_result; |
| 138 | + double sum = 0; |
| 139 | + |
| 140 | + for (size_t count = 0; count < TEST_COUNT; count++) { |
| 141 | + clock_gettime(CLOCK_REALTIME, &start_time); |
| 142 | + TEST_ASSERT_FALSE( |
| 143 | + api_find_transactions_obj_by_tag(&service, TAG_MSG, &json_result)); |
| 144 | + clock_gettime(CLOCK_REALTIME, &end_time); |
| 145 | +#if defined(ENABLE_STAT) |
| 146 | + printf("%lf\n", diff_time(start_time, end_time)); |
| 147 | +#endif |
| 148 | + sum += diff_time(start_time, end_time); |
| 149 | + } |
| 150 | + printf("Average time of find_tx_obj_by_tag: %lf\n", sum / TEST_COUNT); |
| 151 | + free(json_result); |
| 152 | +} |
| 153 | + |
| 154 | +int main(void) { |
| 155 | + UNITY_BEGIN(); |
| 156 | + service.http.path = "/"; |
| 157 | + service.http.host = IRI_HOST; |
| 158 | + service.http.port = IRI_PORT; |
| 159 | + service.http.api_version = 1; |
| 160 | + service.serializer_type = SR_JSON; |
| 161 | + iota_client_core_init(&service); |
| 162 | + |
| 163 | + printf("Total samples for each API test: %d\n", TEST_COUNT); |
| 164 | + RUN_TEST(test_generate_address); |
| 165 | + RUN_TEST(test_get_tips_pair); |
| 166 | + RUN_TEST(test_get_tips); |
| 167 | + RUN_TEST(test_send_transfer); |
| 168 | + RUN_TEST(test_get_transaction_object); |
| 169 | + RUN_TEST(test_find_transactions_by_tag); |
| 170 | + RUN_TEST(test_find_transactions_obj_by_tag); |
| 171 | + iota_client_core_destroy(&service); |
| 172 | + return UNITY_END(); |
| 173 | +} |
0 commit comments