Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit c92d422

Browse files
authored
Merge pull request #82 from wusyong/api_testcase
feat: add accelerator test driver
2 parents a7914c9 + b40068d commit c92d422

File tree

3 files changed

+204
-4
lines changed

3 files changed

+204
-4
lines changed

accelerator/common_core.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,7 @@ int ta_find_transactions_obj_by_tag(const iota_client_service_t* const service,
386386
flex_trit_t* hash_trits = NULL;
387387

388388
ta_find_transactions_res_t* hash_res = ta_find_transactions_res_new();
389-
ta_get_transaction_object_res_t* obj_res =
390-
ta_get_transaction_object_res_new();
391-
if (hash_res == NULL || obj_res == NULL) {
389+
if (hash_res == NULL) {
392390
ret = -1;
393391
goto done;
394392
}
@@ -402,6 +400,12 @@ int ta_find_transactions_obj_by_tag(const iota_client_service_t* const service,
402400
// get transaction obj
403401
for (hash_trits = hash243_queue_peek(hash_res->hashes); hash_trits != NULL;
404402
hash_trits = hash243_queue_peek(hash_res->hashes)) {
403+
ta_get_transaction_object_res_t* obj_res =
404+
ta_get_transaction_object_res_new();
405+
if (obj_res == NULL) {
406+
ret = -1;
407+
goto done;
408+
}
405409
flex_trits_to_trytes((tryte_t*)hash_trytes, NUM_TRYTES_HASH, hash_trits,
406410
NUM_TRITS_HASH, NUM_TRITS_HASH);
407411
hash243_queue_pop(&hash_res->hashes);
@@ -411,10 +415,10 @@ int ta_find_transactions_obj_by_tag(const iota_client_service_t* const service,
411415
break;
412416
}
413417
utarray_push_back(res->txn_obj, obj_res->txn);
418+
ta_get_transaction_object_res_free(&obj_res);
414419
}
415420

416421
done:
417422
ta_find_transactions_res_free(&hash_res);
418-
ta_get_transaction_object_res_free(&obj_res);
419423
return ret;
420424
}

tests/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ cc_library(
2525
],
2626
)
2727

28+
cc_test(
29+
name = "driver",
30+
srcs = [
31+
"driver.c",
32+
],
33+
deps = [
34+
":test_define",
35+
"//accelerator:apis",
36+
],
37+
)
38+
39+
cc_binary(
40+
name = "driver_stat",
41+
srcs = [
42+
"driver.c",
43+
],
44+
copts = ["-DENABLE_STAT"],
45+
deps = [
46+
":test_define",
47+
"//accelerator:apis",
48+
],
49+
)
50+
2851
cc_binary(
2952
name = "test_cache",
3053
srcs = [

tests/driver.c

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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

Comments
 (0)