| 1 | ! ============================================================================== |
| 2 | ! Test program for Phase 4 memory dashboard |
| 3 | ! ============================================================================== |
| 4 | program test_dashboard |
| 5 | use string_pool |
| 6 | use memory_dashboard |
| 7 | use iso_fortran_env, only: output_unit |
| 8 | implicit none |
| 9 | |
| 10 | type(string_ref) :: refs(100) |
| 11 | integer :: i, j |
| 12 | character(len=256) :: test_str |
| 13 | logical :: test_passed |
| 14 | |
| 15 | test_passed = .true. |
| 16 | |
| 17 | print *, "=== Phase 4 Memory Dashboard Test Suite ===" |
| 18 | print *, "Testing real-time memory statistics and visualization" |
| 19 | print * |
| 20 | |
| 21 | ! Initialize the pool and dashboard |
| 22 | call pool_init() |
| 23 | call dashboard_init(verbose=.true.) |
| 24 | |
| 25 | ! Test 1: Simulate readline module allocations |
| 26 | print *, "Test 1: Simulating readline module activity..." |
| 27 | do i = 1, 20 |
| 28 | refs(i) = pool_get_string(64) |
| 29 | call dashboard_track_allocation(MOD_READLINE, 64, 1) |
| 30 | write(test_str, '(a,i0)') "readline_buffer_", i |
| 31 | call pool_copy_to_ref(refs(i), test_str) |
| 32 | end do |
| 33 | print *, " Created 20 readline buffers" |
| 34 | |
| 35 | ! Test 2: Simulate completion module allocations |
| 36 | print *, "Test 2: Simulating completion module activity..." |
| 37 | do i = 21, 40 |
| 38 | refs(i) = pool_get_string(256) |
| 39 | call dashboard_track_allocation(MOD_COMPLETION, 256, 2) |
| 40 | write(test_str, '(a,i0)') "completion_candidate_", i |
| 41 | call pool_copy_to_ref(refs(i), test_str) |
| 42 | end do |
| 43 | print *, " Created 20 completion candidates" |
| 44 | |
| 45 | ! Test 3: Simulate parser module with mixed sizes |
| 46 | print *, "Test 3: Simulating parser module activity..." |
| 47 | do i = 41, 60 |
| 48 | if (mod(i, 2) == 0) then |
| 49 | refs(i) = pool_get_string(1024) |
| 50 | call dashboard_track_allocation(MOD_PARSER, 1024, 3) |
| 51 | write(test_str, '(a,i0)') "parser_ast_node_", i |
| 52 | else |
| 53 | refs(i) = pool_get_string(256) |
| 54 | call dashboard_track_allocation(MOD_PARSER, 256, 2) |
| 55 | write(test_str, '(a,i0)') "parser_token_", i |
| 56 | end if |
| 57 | call pool_copy_to_ref(refs(i), test_str) |
| 58 | end do |
| 59 | print *, " Created 20 parser elements (mixed sizes)" |
| 60 | |
| 61 | ! Display initial dashboard |
| 62 | print * |
| 63 | print *, "=== Initial Dashboard Display ===" |
| 64 | call dashboard_display(detailed=.false.) |
| 65 | |
| 66 | ! Test 4: Simulate some deallocations |
| 67 | print *, "Test 4: Simulating deallocations..." |
| 68 | do i = 1, 10 |
| 69 | call pool_release_string(refs(i)) |
| 70 | call dashboard_track_deallocation(MOD_READLINE, 64, 1) |
| 71 | end do |
| 72 | do i = 21, 30 |
| 73 | call pool_release_string(refs(i)) |
| 74 | call dashboard_track_deallocation(MOD_COMPLETION, 256, 2) |
| 75 | end do |
| 76 | print *, " Released 10 readline buffers and 10 completion candidates" |
| 77 | |
| 78 | ! Test 5: Simulate executor module with large allocations |
| 79 | print *, "Test 5: Simulating executor module with large buffers..." |
| 80 | do i = 61, 70 |
| 81 | refs(i) = pool_get_string(4096) |
| 82 | call dashboard_track_allocation(MOD_EXECUTOR, 4096, 4) |
| 83 | write(test_str, '(a,i0)') "executor_command_output_", i |
| 84 | call pool_copy_to_ref(refs(i), test_str) |
| 85 | end do |
| 86 | print *, " Created 10 large executor buffers" |
| 87 | |
| 88 | ! Display detailed dashboard |
| 89 | print * |
| 90 | print *, "=== Detailed Dashboard Display ===" |
| 91 | call dashboard_display(detailed=.true.) |
| 92 | |
| 93 | ! Test 6: Check module statistics |
| 94 | print *, "Test 6: Verifying module statistics..." |
| 95 | block |
| 96 | type(module_stats) :: rl_stats, comp_stats |
| 97 | |
| 98 | rl_stats = dashboard_get_module_stats(MOD_READLINE) |
| 99 | comp_stats = dashboard_get_module_stats(MOD_COMPLETION) |
| 100 | |
| 101 | if (rl_stats%total_allocations == 20 .and. rl_stats%total_deallocations == 10) then |
| 102 | print *, " PASSED: Readline stats correct" |
| 103 | else |
| 104 | print *, " FAILED: Readline stats incorrect" |
| 105 | test_passed = .false. |
| 106 | end if |
| 107 | |
| 108 | if (comp_stats%total_allocations == 20 .and. comp_stats%total_deallocations == 10) then |
| 109 | print *, " PASSED: Completion stats correct" |
| 110 | else |
| 111 | print *, " FAILED: Completion stats incorrect" |
| 112 | test_passed = .false. |
| 113 | end if |
| 114 | end block |
| 115 | |
| 116 | ! Test 7: Stress test with rapid allocations/deallocations |
| 117 | print *, "Test 7: Stress testing with rapid operations..." |
| 118 | do j = 1, 5 |
| 119 | ! Allocate batch |
| 120 | do i = 71, 100 |
| 121 | refs(i) = pool_get_string(128) |
| 122 | call dashboard_track_allocation(MOD_HISTORY, 128, 2) |
| 123 | end do |
| 124 | ! Deallocate batch |
| 125 | do i = 71, 100 |
| 126 | call pool_release_string(refs(i)) |
| 127 | call dashboard_track_deallocation(MOD_HISTORY, 128, 2) |
| 128 | end do |
| 129 | end do |
| 130 | print *, " Completed 5 cycles of 30 allocations/deallocations" |
| 131 | |
| 132 | ! Export statistics to CSV |
| 133 | print * |
| 134 | print *, "Test 8: Exporting statistics to CSV..." |
| 135 | call dashboard_export_csv("memory_stats.csv") |
| 136 | |
| 137 | ! Display summary |
| 138 | print * |
| 139 | print *, "=== Final Summary ===" |
| 140 | call dashboard_summary() |
| 141 | |
| 142 | ! Final dashboard display |
| 143 | print *, "=== Final Dashboard State ===" |
| 144 | call dashboard_display(detailed=.false.) |
| 145 | |
| 146 | ! Cleanup remaining allocations |
| 147 | do i = 11, 20 |
| 148 | if (refs(i)%pool_index /= 0) call pool_release_string(refs(i)) |
| 149 | end do |
| 150 | do i = 31, 70 |
| 151 | if (refs(i)%pool_index /= 0) call pool_release_string(refs(i)) |
| 152 | end do |
| 153 | |
| 154 | ! Clean up |
| 155 | call dashboard_cleanup() |
| 156 | call pool_cleanup() |
| 157 | |
| 158 | ! Test results |
| 159 | print * |
| 160 | if (test_passed) then |
| 161 | print *, "=== ALL TESTS PASSED ===" |
| 162 | print *, "Phase 4 Dashboard implementation successful!" |
| 163 | print *, "" |
| 164 | print *, "Key achievements:" |
| 165 | print *, " ✓ Per-module memory tracking" |
| 166 | print *, " ✓ Real-time statistics display" |
| 167 | print *, " ✓ Bucket distribution analysis" |
| 168 | print *, " ✓ CSV export capability" |
| 169 | print *, " ✓ Visual progress bars and formatting" |
| 170 | print *, "" |
| 171 | print *, "The dashboard provides essential visibility for Phase 6 integration!" |
| 172 | else |
| 173 | print *, "=== SOME TESTS FAILED ===" |
| 174 | print *, "Please review the implementation" |
| 175 | end if |
| 176 | |
| 177 | end program test_dashboard |