!**************************************************************************************************!
! Test the PROFILER (graphical) macro
!
! @author  Neon Ngo
! @bug 2025, 2033
!**************************************************************************************************!
startmacro D:updateInterval[0.25]
  switch INTERACTIVE interAct GET DEF=TRUE

  if interAct then
    res instructions "The PROFILER GUI with ^{updateInterval} sec updates:\n\n&
                      Memory Usage PLOT on top:\n&
                      RED    - TOTAL_USED\n&
                      GREEN  - TOTAL_COMMITTED\n&
                      BLUE   - HEAP_MEMORY\n&
                      YELLOW - NONHEAP_MEMORY\n\n&
                      Thread Statistics PLOT on bottom:\n&
                      RED    - CURRENT_COUNT\n&
                      GREEN  - PEAK_NUMBER\n&
                      BLUE   - TOTAL_CREATED\n\n&
                      NOTE: You may need to use LEGEND to hide &
                      some lines in order to see others.\n\n&
                      1) Press 'DEMO':\n&
                      lines on both PLOTs should increase\n\n&
                      2) Press 'Stop'on DEMO:\n&
                      Thread CURRENT_COUNT goes down right away &
                      (Java garbage collection may delay others)"

    pipe on
      panel/msgid=MAIN/controls=gc/setup
      gcontrol label  MACRO "MACRO"
        gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
        set gc.MAIN.itemColors {PASS=GREEN,FAIL=RED}
        gcontrol text idtext "INSTRUCTIONS" "^instructions" 24 26 /title
        gcontrol button runDemo "Run command"  "DEMO" /tleft

      profiler/pause=^updateInterval/bg
   pipe off
  endif
endmacro

procedure processMessage m:msg
  if msg.name eqs "MAIN"
    ! tell any PROFILER or DEMO sub-macros to EXIT
    foreach key in reg
      if key eqss "DEMO" OR key eqss "PROFILER" then
        message send id=key name="EXIT"
      endif
    endfor
    assert/text="^reg.main.name: user indicated" msg.data eqs "PASS"
    pipe stop

  elseif msg.name eqs "RUNDEMO"
    demo;sys/bg/wpos=(0,400)

  else
    ! do nothing
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif

return
