| 1 | /* lognestmonster Copyright (c) 2020 Joshua 'joshuas3' Stockin |
| 2 | * <https://joshstock.in> |
| 3 | * <https://github.com/JoshuaS3/lognestmonster> |
| 4 | * |
| 5 | * This software is licensed and distributed under the terms of the MIT License. |
| 6 | * See the MIT License in the LICENSE file of this project's root folder. |
| 7 | * |
| 8 | * This comment block and its contents, including this disclaimer, MUST be |
| 9 | * preserved in all copies or distributions of this software's source. |
| 10 | */ |
| 11 |
|
| 12 | // header_memory.c |
| 13 | // Test memory usage on lognestmonster library to prevent leaks |
| 14 |
|
| 15 | #include <stdio.h> |
| 16 | #include <sys/resource.h> |
| 17 |
|
| 18 | #define LNM_INIT |
| 19 | #define LNM_ALL |
| 20 | #define LNM_DEBUG |
| 21 | #include "lognestmonster.h" |
| 22 |
|
| 23 | int main() { |
| 24 | long t1 = lnm_getus(); |
| 25 |
|
| 26 | struct rusage r_usage; |
| 27 | getrusage(RUSAGE_SELF, &r_usage); |
| 28 | printf("Memory usage before: %li\n", r_usage.ru_maxrss); |
| 29 |
|
| 30 | lnmItem lastEvent = NULL; |
| 31 | for (int iter = 0; iter < 1000; iter++) { |
| 32 | uint64_t time = lnm_getus(); |
| 33 | int type = time % 5 == 0 ? LNM_EVENT : LNM_STATEMENT; |
| 34 | if (type == LNM_STATEMENT) { |
| 35 | char message[25]; |
| 36 | snprintf(message, 25, "New statement #%i", iter); |
| 37 | if (lastEvent == NULL) { |
| 38 | lnmStatement(lnmInfo, message); |
| 39 | } else { |
| 40 | lnmEventPushS(lastEvent, lnmInfo, message); |
| 41 | } |
| 42 | } else if (type == LNM_EVENT) { |
| 43 | char tag[20]; |
| 44 | snprintf(tag, 20, "New event #%i", iter); |
| 45 | if (lastEvent == NULL || time % 7 == 0) { |
| 46 | lastEvent = lnmEvent(tag); |
| 47 | } else { |
| 48 | lnmItem new_event = lnmEvent(tag); |
| 49 | lnmEventPush(lastEvent, new_event); |
| 50 | lastEvent = new_event; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 |
|
| 55 | lnm_debug_parse_registry(); |
| 56 |
|
| 57 | getrusage(RUSAGE_SELF, &r_usage); |
| 58 | printf("Memory usage during test: %li\n", r_usage.ru_maxrss); |
| 59 |
|
| 60 | lnm_registry_free(); |
| 61 | lnm_debug_parse_registry(); |
| 62 |
|
| 63 | getrusage(RUSAGE_SELF, &r_usage); |
| 64 | printf("Memory usage after: %li\n", r_usage.ru_maxrss); |
| 65 |
|
| 66 | printf("time elapsed (us): %lu\n", lnm_getus() - t1); |
| 67 | return 0; |
| 68 | } |
| 69 |
|