RyanJsonMemoryFootprintTest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "RyanJsonTest.h"
  2. static void *yy_malloc(void *ctx, size_t size) { return v_malloc(size); }
  3. static void *yy_realloc(void *ctx, void *ptr, size_t old_size, size_t size) { return v_realloc(ptr, size); }
  4. static void yy_free(void *ctx, void *ptr) { v_free(ptr); }
  5. static int RyanJsonMemoryFootprint(char *jsonstr)
  6. {
  7. int32_t use = vallocGetUse();
  8. RyanJsonInitHooks(v_malloc, v_free, v_realloc);
  9. RyanJson_t json = RyanJsonParse(jsonstr);
  10. if (json == NULL)
  11. {
  12. printf("%s:%d 解析失败\r\n", __FILE__, __LINE__);
  13. return -1;
  14. }
  15. use = vallocGetUse() - use;
  16. RyanJsonDelete(json);
  17. return use;
  18. }
  19. static int cJSONMemoryFootprint(char *jsonstr)
  20. {
  21. int32_t use = vallocGetUse();
  22. cJSON_Hooks hooks = {.malloc_fn = v_malloc, .free_fn = v_free};
  23. cJSON_InitHooks(&hooks);
  24. cJSON *json = cJSON_Parse(jsonstr);
  25. if (json == NULL)
  26. {
  27. printf("%s:%d 解析失败\r\n", __FILE__, __LINE__);
  28. return -1;
  29. }
  30. use = vallocGetUse() - use;
  31. cJSON_Delete(json);
  32. return use;
  33. }
  34. static int yyjsonMemoryFootprint(char *jsonstr)
  35. {
  36. static yyjson_alc yyalc = {yy_malloc, yy_realloc, yy_free, NULL};
  37. // 先解析成只读文档(可用自定义分配器 yyalc)
  38. yyjson_doc *doc = yyjson_read_opts(jsonstr, strlen(jsonstr), YYJSON_READ_NOFLAG, &yyalc, NULL);
  39. if (doc == NULL) { return -1; }
  40. // 从只读文档拷贝为可变文档(用于后续读写修改)
  41. yyjson_mut_doc *mdoc = yyjson_doc_mut_copy(doc, &yyalc);
  42. yyjson_doc_free(doc);
  43. if (mdoc == NULL) { return -1; }
  44. // 统计当前分配器的占用
  45. int area = 0, use = 0;
  46. v_mcheck(&area, &use);
  47. // 用完释放可变文档
  48. yyjson_mut_doc_free(mdoc);
  49. return use;
  50. }
  51. static void printfJsonCompera(char *jsonstr)
  52. {
  53. int RyanJsonCount = 0;
  54. int cJSONCount = 0;
  55. int yyjsonCount = 0;
  56. RyanJsonCount = RyanJsonMemoryFootprint(jsonstr);
  57. cJSONCount = cJSONMemoryFootprint(jsonstr);
  58. yyjsonCount = yyjsonMemoryFootprint(jsonstr);
  59. printf("json原始文本长度为 %ld, 序列化后RyanJson内存占用: %d, cJSON内存占用: %d, yyjson内存占用: %d\r\n", strlen(jsonstr),
  60. RyanJsonCount, cJSONCount, yyjsonCount);
  61. double save_vs_cjson = 100.0 - ((double)RyanJsonCount * 100.0) / (double)cJSONCount;
  62. double save_vs_yyjson = 100.0 - ((double)RyanJsonCount * 100.0) / (double)yyjsonCount;
  63. printf("比cJSON节省: %.2f%% 内存占用, 比yyjson节省: %.2f%% 内存占用\r\n", save_vs_cjson, save_vs_yyjson);
  64. }
  65. RyanJsonBool_e RyanJsonMemoryFootprintTest(void)
  66. {
  67. char *jsonstr;
  68. printf("\r\n--------------------------- 混合类型json数据测试 --------------------------\r\n");
  69. jsonstr = "{\"item1\":{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  70. "\"item\":{\"inter\":16,"
  71. "\"double\":16.89,\"string\":\"hello\","
  72. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,"
  73. "16.89,16.89],"
  74. "\"arrayString\":[\"hello\",\"hello\",\"hello\","
  75. "\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,\"double\":16.89,"
  76. "\"string\":\"hello\","
  77. "\"boolTrue\":true,\"boolFalse\":false,"
  78. "\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null}]"
  79. "},\"item2\":{"
  80. "\"inter\":16,\"double\":16.89,\"string\":"
  81. "\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,\"item\":{\"inter\":16,\"double\":16.89,\"string\":"
  82. "\"hello\",\"boolTrue\":"
  83. "true,\"boolFalse\":false,\"null\":null},"
  84. "\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89],\"arrayString\":[\"hello\",\"hello\","
  85. "\"hello\",\"hello\","
  86. "\"hello\"],\"array\":[16,16.89,\"hello\","
  87. "true,false,null],\"arrayItem\":[{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":"
  88. "false,\"null\":null},{"
  89. "\"inter\":16,\"double\":16.89,\"string\":"
  90. "\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null}]},\"item3\":{\"inter\":16,\"double\":16.89,\"string\":"
  91. "\"hello\",\"boolTrue\":"
  92. "true,\"boolFalse\":false,\"null\":null,"
  93. "\"item\":{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},"
  94. "\"arrayInt\":[16,16,16,16,"
  95. "16],\"arrayDouble\":[16.89,16.89,16.89,"
  96. "16.89,16.89],\"arrayString\":[\"hello\",\"hello\",\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,"
  97. "false,null],"
  98. "\"arrayItem\":[{\"inter\":16,\"double\":16.89,"
  99. "\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":"
  100. "\"hello\",\"boolTrue\":"
  101. "true,\"boolFalse\":false,\"null\":null}]}"
  102. ",\"item4\":{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,"
  103. "\"item\":{\"inter\":16,"
  104. "\"double\":16.89,\"string\":\"hello\","
  105. "\"boolTrue\":true,\"boolFalse\":false,\"null\":null},\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,"
  106. "16.89,16.89],"
  107. "\"arrayString\":[\"hello\",\"hello\",\"hello\","
  108. "\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null],\"arrayItem\":[{\"inter\":16,\"double\":16.89,"
  109. "\"string\":\"hello\","
  110. "\"boolTrue\":true,\"boolFalse\":false,"
  111. "\"null\":null},{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null}]"
  112. "}}";
  113. printfJsonCompera(jsonstr);
  114. printf("\r\n--------------------------- 对象占多json数据测试 --------------------------\r\n");
  115. jsonstr =
  116. "{\"message\":\"success感谢又拍云(upyun.com)提供CDN赞助\",\"status\":200,\"date\":\"20230822\",\"time\":\"2023-08-22 "
  117. "09:44:54\",\"cityInfo\":{\"city\":\"郑州市\",\"citykey\":\"101180101\",\"parent\":\"河南\",\"updateTime\":\"07:46\"},"
  118. "\"data\":{\"shidu\":"
  119. "\"85%\",\"pm25\":20,\"pm10\":56,"
  120. "\"quality\":\"良\",\"wendu\":\"29\",\"ganmao\":\"极少数敏感人群应减少户外活动\",\"forecast\":[{\"date\":\"22\",\"high\":"
  121. "\"高温 "
  122. "35℃\",\"low\":\"低温 "
  123. "23℃\",\"ymd\":\"2023-08-22\",\"week\":\"星期二\",\"sunrise\":\"05:51\",\"sunset\":\"19:05\",\"aqi\":78,\"fx\":\"东南风\","
  124. "\"fl\":\"2级\","
  125. "\"type\":\"晴\",\"notice\":"
  126. "\"愿你拥有比阳光明媚的心情\"},{\"date\":\"23\",\"high\":\"高温 33℃\",\"low\":\"低温 "
  127. "23℃\",\"ymd\":\"2023-08-23\",\"week\":\"星期三\",\"sunrise\":\"05:52\",\"sunset\":\"19:04\",\"aqi\":71,\"fx\":\"南风\","
  128. "\"fl\":\"2级\","
  129. "\"type\":\"中雨\",\"notice\":"
  130. "\"记得随身携带雨伞哦\"},{\"date\":\"24\",\"high\":\"高温 31℃\",\"low\":\"低温 "
  131. "21℃\",\"ymd\":\"2023-08-24\",\"week\":\"星期四\",\"sunrise\":\"05:52\",\"sunset\":\"19:03\",\"aqi\":74,\"fx\":\"东风\","
  132. "\"fl\":\"2级\","
  133. "\"type\":\"晴\",\"notice\":"
  134. "\"愿你拥有比阳光明媚的心情\"},{\"date\":\"25\",\"high\":\"高温 30℃\",\"low\":\"低温 "
  135. "23℃\",\"ymd\":\"2023-08-25\",\"week\":\"星期五\",\"sunrise\":\"05:53\",\"sunset\":\"19:02\",\"aqi\":93,\"fx\":\"东风\","
  136. "\"fl\":\"1级\","
  137. "\"type\":\"小雨\",\"notice\":"
  138. "\"雨虽小,注意保暖别感冒\"},{\"date\":\"26\",\"high\":\"高温 25℃\",\"low\":\"低温 "
  139. "22℃\",\"ymd\":\"2023-08-26\",\"week\":\"星期六\",\"sunrise\":\"05:54\",\"sunset\":\"19:00\",\"aqi\":80,\"fx\":\"东北风\","
  140. "\"fl\":\"1级\","
  141. "\"type\":\"阴\",\"notice\":"
  142. "\"不要被阴云遮挡住好心情\"},{\"date\":\"27\",\"high\":\"高温 27℃\",\"low\":\"低温 "
  143. "20℃\",\"ymd\":\"2023-08-27\",\"week\":\"星期日\",\"sunrise\":\"05:55\",\"sunset\":\"18:59\",\"aqi\":74,\"fx\":\"西北风\","
  144. "\"fl\":\"1级\","
  145. "\"type\":\"阴\",\"notice\":"
  146. "\"不要被阴云遮挡住好心情\"},{\"date\":\"28\",\"high\":\"高温 30℃\",\"low\":\"低温 "
  147. "20℃\",\"ymd\":\"2023-08-28\",\"week\":\"星期 "
  148. "一\",\"sunrise\":\"05:55\",\"sunset\":\"18:58\",\"aqi\":80,\"fx\":\"东北风\",\"fl\":\"2级\",\"type\":\"多云\",\"notice\":"
  149. "\"阴晴之间,谨防紫外线侵扰\"},{\"date\":\"29\",\"high\":"
  150. "\"高温 30℃\",\"low\":\"低温 "
  151. "20℃\",\"ymd\":\"2023-08-29\",\"week\":\"星期二\",\"sunrise\":\"05:56\",\"sunset\":\"18:56\",\"aqi\":80,\"fx\":\"东北风\","
  152. "\"fl\":\"2级\","
  153. "\"type\":\"多云\",\"notice\":"
  154. "\"阴晴之间,谨防紫外线侵扰\"},{\"date\":\"30\",\"high\":\"高温 31℃\",\"low\":\"低温 "
  155. "20℃\",\"ymd\":\"2023-08-30\",\"week\":\"星期三\",\"sunrise\":\"05:57\",\"sunset\":\"18:55\",\"aqi\":92,\"fx\":\"南风\","
  156. "\"fl\":\"1级\","
  157. "\"type\":\"晴\",\"notice\":"
  158. "\"愿你拥有比阳光明媚的心情\"},{\"date\":\"31\",\"high\":\"高温 33℃\",\"low\":\" 低温 "
  159. "22℃\",\"ymd\":\"2023-08-31\",\"week\":\"星期四\",\"sunrise\":\"05:57\",\"sunset\":\"18:54\",\"aqi\":91,\"fx\":\"南风\","
  160. "\"fl\":\"1级\","
  161. "\"type\":\"晴\",\"notice\":"
  162. "\"愿你拥有比阳光明媚的心情\"},{\"date\":\"01\",\"high\":\"高温 34℃\",\"low\":\"低温 "
  163. "23℃\",\"ymd\":\"2023-09-01\",\"week\":\"星期五\",\"sunrise\":\"05:58\",\"sunset\":\"18:52\",\"aqi\":91,\"fx\":\"西风\","
  164. "\"fl\":\"1级\","
  165. "\"type\":\"晴\",\"notice\":"
  166. "\"愿你拥有比阳光明媚的心情\"},{\"date\":\"02\",\"high\":\"高温 36℃\",\"low\":\"低温 "
  167. "25℃\",\"ymd\":\"2023-09-02\",\"week\":\"星期六\",\"sunrise\":\"05:59\",\"sunset\":\"18:51\",\"aqi\":78,\"fx\":\"南风\","
  168. "\"fl\":\"1级\","
  169. "\"type\":\"阴\",\"notice\":"
  170. "\"不要被阴云遮挡住好心情\"},{\"date\":\"03\",\"high\":\"高温 35℃\",\"low\":\"低温 "
  171. "24℃\",\"ymd\":\"2023-09-03\",\"week\":\"星期日\",\"sunrise\":\"06:00\",\"sunset\":\"18:50\",\"aqi\":82,\"fx\":\"东北风\","
  172. "\"fl\":\"1级\","
  173. "\"type\":\"晴\",\"notice\":"
  174. "\"愿你拥有比阳光明媚的心情\"},{\"date\":\"04\",\"high\":\"高温 35℃\",\"low\":\"低温 "
  175. "25℃\",\"ymd\":\"2023-09-04\",\"week\":\"星期一\",\"sunrise\":\"06:00\",\"sunset\":\"18:48\",\"aqi\":88,\"fx\":\"南风\","
  176. "\"fl\":\"2级\","
  177. "\"type\":\"晴\",\"notice\":"
  178. "\"愿你拥有比阳光明媚的心情\"},{\"date\":\"05\",\"high\":\"高温 35℃\",\"low\":\"低温 "
  179. "25℃\",\"ymd\":\"2023-09-05\",\"week\":\"星期二\",\"sunrise\":\"06:01\",\"sunset\":\"18:47\",\"aqi\":58,\"fx\":\"南风\","
  180. "\"fl\":\"2级\","
  181. "\"type\":\"阴\",\"notice\":"
  182. "\"不要被阴云遮挡住好心情\"}],\"yesterday\":{\"date\":\"21\",\"high\":\"高温 34℃\",\"low\":\"低温 "
  183. "24℃\",\"ymd\":\"2023-08-21\",\"week\":\" "
  184. "星期一\",\"sunrise\":\"05:50\",\"sunset\":\"19:07\",\"aqi\":60,\"fx\":\"西风\",\"fl\":\"2级\",\"type\":\"小雨\","
  185. "\"notice\":"
  186. "\"雨虽小,注意保暖别感冒\"}}}";
  187. printfJsonCompera(jsonstr);
  188. printf("\r\n--------------------------- 数组占多json数据测试 --------------------------\r\n");
  189. jsonstr = "{\"item1\":{\"arrayInt\":[16,16,16,16,16,16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89,16.89,16.89,16."
  190. "89,16.89,16.89],"
  191. "\"arrayString\":[\"hello\",\"hello\","
  192. "\"hello\",\"hello\",\"hello\",\"hello\",\"hello\",\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,"
  193. "false,null,16,16.89,"
  194. "\"hello\",true,false,null]},\"item2\":{"
  195. "\"arrayInt\":[16,16,16,16,16,16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89,16."
  196. "89],\"arrayString\":["
  197. "\"hello\",\"hello\",\"hello\",\"hello\","
  198. "\"hello\",\"hello\",\"hello\",\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null,16,16.89,"
  199. "\"hello\",true,false,"
  200. "null]},\"item3\":{\"arrayInt\":[16,16,16,"
  201. "16,16,16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89],\"arrayString\":["
  202. "\"hello\",\"hello\","
  203. "\"hello\",\"hello\",\"hello\",\"hello\","
  204. "\"hello\",\"hello\",\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null,16,16.89,\"hello\",true,false,"
  205. "null]},\"item4\":{"
  206. "\"arrayInt\":[16,16,16,16,16,16,16,16,16,16],"
  207. "\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89,16.89],\"arrayString\":[\"hello\",\"hello\","
  208. "\"hello\",\"hello\","
  209. "\"hello\",\"hello\",\"hello\",\"hello\","
  210. "\"hello\",\"hello\"],\"array\":[16,16.89,\"hello\",true,false,null,16,16.89,\"hello\",true,false,null]}}";
  211. printfJsonCompera(jsonstr);
  212. printf("\r\n--------------------------- 小对象json 混合类型内存占用测试 --------------------------\r\n");
  213. jsonstr = "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null}";
  214. printfJsonCompera(jsonstr);
  215. printf("\r\n--------------------------- 小对象json 纯字符串内存占用测试 --------------------------\r\n");
  216. jsonstr = "{\"inter\":\"16\",\"double\":\"16.89\",\"string\":\"hello\",\"boolTrue\":\"true\",\"boolFalse\":\"false\",\"null\":"
  217. "\"null\"}";
  218. printfJsonCompera(jsonstr);
  219. /**
  220. * @brief 反序列化为文本,内存占用没什么特别的优化点,和cjson实现思路差不多,内存占用也就差不多,就不进行对比了
  221. *
  222. */
  223. return RyanJsonTrue;
  224. }