Jump to content

GraphX - Graph CPU + RAM Usage Per Process


weaponx
 Share

Recommended Posts

I would like to thank PSaltyDS for his ProcessListProperties script here:

#409276

Description:

The first part of this script will gather CPU usage for a specified process, at a specified frequency, a specified number of times (if that makes any sense). All of this data gets pumped into a 2 dimensional array.

The second part of this script plots the data from the array into a graph. And it's pretty sweet.

v1

Prime95 (4 cores):

Posted Image

In the above picture I loaded up Prime95 but waited about 10 seconds to start the benchmark, you can see it peaks at 100% for the duration.

v2

Randomly generated data:

Posted Image

Actual data from explorer.exe:

Posted Image

And here is the code:

GraphX.au3

GraphXv2.au3

Edited by weaponx
Link to comment
Share on other sites

Congrats. Very nice code.

Suggestions:

could you show in the graph the RAM usage too (at least the average ram usage counter)?

could you write a textual log with ram and cpu usage detected? I mean something like this:

timestamp cpu usage ram usage

... ... ...

... ... ...

---------------------------------------

Average ram usage: nnn

Average cpu usage: nnn

Moreover, you could improve the GUI to create a professional process monitoring tool: list of active processes, selection of a process, configuration of the polling time and of the test duration,...

Many thanks again for sharing your code and for your help

Peppe

Edited by gcriaco
Link to comment
Share on other sites

Thanks for checking it out. The next version should be able to plot the data in real time and it will be sweet. Also my GraphX() function is not far off from becoming a full-fledged graph UDF. That way people can insert graphs using native drawing functions instead of embedding Excel OCX.

@gcriaco - Your request should be easy. I will put an export button in there somewhere. All of the data is already in an array.

I will post more as soon as it's done.

Link to comment
Share on other sites

Nice script! The graphs looks great!

Could you find a way of telling the user what's happening without making a new TrayTip() every half a second?

Hmm...you could just have one at the end to signal completion.

Maybe I can have The Count from Sesame Street read off the remaining time.

Link to comment
Share on other sites

Hmm...you could just have one at the end to signal completion.

Maybe I can have The Count from Sesame Street read off the remaining time.

Nice script! Looking forward to the Sesame St. skin when you get it done... (green felt?)

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 7 months later...

hey man nice code can i ask for a simple code to make a graph just like urs

i got lost in arrays and couldn't get it.

just simple graph getting their values from text files,Thanks

Link to comment
Share on other sites

hey man nice code can i ask for a simple code to make a graph just like urs

i got lost in arrays and couldn't get it.

just simple graph getting their values from text files,Thanks

This is a preview of a udf i'm creating:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.1
 Author:        WeaponX

 Script Function:
    Steam style graph generator

#ce ----------------------------------------------------------------------------
#include <GUIConstants.au3>
#include <StaticConstants.au3>
;#include <Array.au3>

;Create Main window
GUICreate("Graph X", 600, 600, -1)
GUISetState (@SW_SHOW)
GUISetBkColor (0x424242)   

;Left, Top, Width, Height
$graph1 = _GraphXCreate(100,30,400,200)

;Left, Top, Width, Height
$graph2 = _GraphXCreate(100,300,400,200, 0x000000)

;Generate 60 random integers 0-1000 for 2 seperate lines
$randomData = _GraphXRandomData(0, 1000, 60, 2)

_GraphXUpdate($graph1,$randomData)
_GraphXUpdate($graph2,_GraphXRandomData(50, 100, 20, 4))

Do
    $msg = GUIGetMsg()
Until $msg = $GUI_EVENT_CLOSE
   
;////////////////////////////////
;// CREATE GRAPH
;////////////////////////////////
Func _GraphXCreate($graphX, $graphY, $graphWidth = 400, $graphHeight = 100, $graphBGColor = 0x242529, $graphBorderColor = 0x000000, $graphLabelColor = 0x787878)
    
    ;Vertical/horizontal gridline interval
    Local $graphGridXInterval = 8, $graphGridYInterval = 4    
    
    ;Define GraphX handle array (this will be returned by this function and used to update the graph)
    ;[0] = Array containing AutoIt handles to all GUI controls used
    ;[1-Ubound] = Graph parameters
    Local $graphHWND[10] =[0, $graphX, $graphY, $graphWidth, $graphHeight, $graphBGColor, $graphBorderColor, $graphLabelColor, $graphGridXInterval, $graphGridYInterval]
    
    ;Track ALL GUI controls created by this function so they can be accessed/deleted later
    ;[0] = Graphic Control (grid)
    ;[1] = Graphic Control (graph)
    ;[2-Ubound] = Labels
     Local $handlesArray[1 + 1 + $graphGridXInterval + 1 + $graphGridYInterval + 1]

    ;////////////////////////////////
    ;// DRAW LABELS
    ;////////////////////////////////
    
    ;This is used when storing the label handles into the tracking array
    $lastElement = 1
    
    ;VERTICAL AXIS
    
    ;Align vertical axis labels along their left boundary, right justify the text 
    Local $VlabelWidth = 60, $VlabelHeight = 12, $VlabelOffset = 7
    
    ;Label left = Graph left - label width - offset between label and graph
    $VlabelLeft = $graphX - $VlabelWidth - $VlabelOffset
    
    ;Draw a label at every gridline along vertical axis
    For $X = 0 to $graphGridYInterval
        
        $VlabelTop = ((($graphHeight / $graphGridYInterval) * $X) + $graphY) - ($VlabelHeight/2)
        
        $lastElement += 1
        
        $handlesArray[$lastElement] = GUICtrlCreateLabel ("",  $VlabelLeft, $VlabelTop, $VlabelWidth, $VlabelHeight, $SS_RIGHT)
        GUICtrlSetColor ($handlesArray[$lastElement], $graphLabelColor)
        ;GUICtrlSetBkColor ( -1, 0xFFFFFF) ;For debugging
        GUICtrlSetFont ($handlesArray[$lastElement],8, 600)
    Next
    
    ;HORIZONTAL AXIS
    
    ;Align horizontal axis labels along their top boundary, center justify the text 
    Local $HlabelWidth = 30, $HlabelHeight = 12, $HlabelOffset = 4
    
    ;Label top = Graph top + graph height + offset between label and graph
    $HlabelTop = $graphY + $graphHeight + $HlabelOffset

    ;Draw a label at every gridline along horizontal axis
    For $X = 0 to $graphGridXInterval
        
        $HlabelLeft = ((($graphWidth / $graphGridXInterval) * $X) + $graphX) - ($HlabelWidth/2)
        
        $lastElement += 1
        
        $handlesArray[$lastElement] = GUICtrlCreateLabel ("",  $HlabelLeft, $HlabelTop, $HlabelWidth, $HlabelHeight, $SS_CENTER)
        GUICtrlSetColor ($handlesArray[$lastElement], $graphLabelColor)
        ;GUICtrlSetBkColor ( -1, 0xFFFFFF) ;For debugging
        GUICtrlSetFont ($handlesArray[$lastElement],8, 600)
    Next

    ;////////////////////////////////
    ;// DRAW GRIDLINES
    ;////////////////////////////////
    
    $graphGrid = GuiCtrlCreateGraphic($graphX, $graphY, $graphWidth,$graphHeight)
    GUICtrlSetBkColor($graphGrid,$graphBGColor)
    GUICtrlSetColor ($graphGrid, $graphBorderColor)    
    
    ;Draw vertical grid lines at specified interval (skip first and last): Y
    For $X = 1 to $graphGridYInterval - 1
        GUICtrlSetGraphic($graphGrid,$GUI_GR_MOVE, 0,$graphHeight * ($X / $graphGridYInterval))
        GUICtrlSetGraphic($graphGrid,$GUI_GR_LINE, $graphWidth, $graphHeight * ($X / $graphGridYInterval))
    Next
   
    ;Draw horizontal grid lines at specified interval (skip first and last): X
    For $X = 1 to $graphGridXInterval - 1
        GUICtrlSetGraphic($graphGrid,$GUI_GR_MOVE, $graphWidth * ($X / $graphGridXInterval),0)
        GUICtrlSetGraphic($graphGrid,$GUI_GR_LINE, $graphWidth * ($X / $graphGridXInterval), $graphHeight)
    Next
    
    ;Copy pointer to graphic object into custom handle
    $handlesArray[0] = $graphGrid
    
    ;Copy handles sub-array to main array
    $graphHWND[0] = $handlesArray
    
    Return $graphHWND
EndFunc

;////////////////////////////////
;// UPDATE GRAPH
;////////////////////////////////
Func _GraphXUpdate(ByRef $graphHWND, $graphData)
    ;Extract variables from graph "handle" (maybe a scripting dictionary will replace this, or a 2 dimensional array using variable names in the first dimension + assign)
    Local $handlesArray = $graphHWND[0]
    Local $graphX = $graphHWND[1], $graphY = $graphHWND[2] ;X,Y
    Local $graphWidth = $graphHWND[3], $graphHeight = $graphHWND[4] ;Width, Height
    Local $graphBGColor = $graphHWND[5], $graphBorderColor = $graphHWND[6], $graphLabelColor = $graphHWND[7] ;Colors
    Local $graphGridXInterval = $graphHWND[8], $graphGridYInterval = $graphHWND[9]
    
    ;Predefined line colors (for up to 5 lines)
    Local $graphColors[5] = [0xc98d35, 0x686868, 0x880101, 0x0447a2, 0x44672c]

    ;These need moved...maybe
    Local $graphMinX = 0, $graphMaxX = Ubound($graphData)
   
    Local $graphMinY = 0, $graphMaxY = 0
        
    ;Determine highest Y value from first occurence in the second dimension
    For $X = 0 to $graphMaxX - 1
        If $graphData[$X][0] > $graphMaxY Then $graphMaxY = $graphData[$X][0]
    Next
        
    #cs
    $graphMinY = $graphMaxY
        
    ;Determine lowest Y value from first occurence in the second dimension
    For $X = 0 to $graphMaxX - 1
        If $graphData[$X][0] < $graphMinY Then $graphMinY = $graphData[$X][0]
    Next
    #CE
        
    ;Delete exisitng graph for redraw
    GUICtrlDelete ($handlesArray[1])
    
    ;Create the graphic "sandbox" area, this will be drawn on top of the gridline graphic control
    $graphicSandbox = GuiCtrlCreateGraphic($graphX, $graphY, $graphWidth,$graphHeight)
   
    ;////////////////////////////////
    ;// UPDATE LABELS
    ;////////////////////////////////
    
    ;This is used when storing the label handles into the tracking array
    $lastElement = 1
    
    ;VERTICAL AXIS
    For $X = 0 to $graphGridYInterval
        
        $VLabelValue = $graphMaxY - (($graphMaxY / $graphGridYInterval) * $X)
        
        $lastElement += 1
        
        GUICtrlSetData($handlesArray[$lastElement],$VLabelValue)
    Next
    
    ;HORIZONTAL AXIS
    For $X = 0 to $graphGridXInterval
        
        $HLabelValue = ($graphMaxX / $graphGridXInterval) * $X
        
        $lastElement += 1
        
        GUICtrlSetData($handlesArray[$lastElement],$HLabelValue)
    Next
    
    ;////////////////////////////////
    ;// DRAW LINE GRAPH
    ;////////////////////////////////

    ;Loop through all rows in the array
    For $X = 0 to $graphMaxX - 1
        
        ;Loop through all occurences in the second dimension
        For $Y = 0 to Ubound($graphData, 2) - 1
                
            ;Set line thickness
            ;GUICtrlSetGraphic($graphicSandbox,$GUI_GR_PENSIZE, 1)

            ;Set line color
            GUICtrlSetGraphic($graphicSandbox,$GUI_GR_COLOR, $graphColors[$Y])

            ;////////////////////////////////
            ;// START POINT
            ;////////////////////////////////

            ;At beginning of graph, set X,Y to 0,0 (We use $graphHeight since the Y values are reflected
            If $X = 0 Then 
                $LastX = 0
                $LastY = $graphHeight
            Else
                ;Convert input value into proportion that will fit the limits of the graphic control
                $LastX = ($graphWidth / $graphMaxX) * ($X)
                ;Reflect Y value
                $LastY = $graphHeight - (($graphHeight / $graphMaxY) * $graphData[$X-1][$Y])
            EndIf

            ;Truncate Y values that extend beyond the limits of the graph
            If $LastY > $graphHeight Then $LastY = $graphHeight
            If $LastY < 0 Then $LastY = 0
            
            ;Truncate X values that extend beyond the limits of the graph
            If $LastX > $graphWidth Then $LastX = $graphWidth
            If $LastX < 0 Then $LastX = 0

            ;Define starting point
            GUICtrlSetGraphic($graphicSandbox,$GUI_GR_MOVE, $LastX,$LastY)

            ;////////////////////////////////
            ;// END POINT
            ;////////////////////////////////

            ;Convert input value (relative %) into absolute pixel value
            $tempX = ($graphWidth / $graphMaxX) * ($X+1)
           
            ;Reflect Y value
            $tempY = $graphHeight - (($graphHeight / $graphMaxY) * $graphData[$X][$Y])
            
            ;Truncate Y values that extend beyond the limits of the graph
            If $tempY > $graphHeight Then $tempY = $graphHeight
            If $tempY < 0 Then $tempY = 0
            
            ;Truncate X values that extend beyond the limits of the graph
            If $tempX > $graphWidth Then $tempX = $graphWidth
            If $tempX < 0 Then $tempX = 0
            
            GUICtrlSetGraphic($graphicSandbox,$GUI_GR_LINE,$tempX,$tempY)
            GUICtrlSetGraphic($graphicSandbox,$GUI_GR_REFRESH)
            
            ;ConsoleWrite($Y & ": " & @CRLF & "Previous: " & $LastX & "," & $LastY & @CRLF & "Current:  " & $tempX & "," & $tempY & @CRLF)
        Next
    Next
    
    ;Copy pointer to graphic object into custom handle
    $handlesArray[1] = $graphicSandbox
    
    ;Copy handles sub-array to main array
    $graphHWND[0] = $handlesArray
EndFunc

;////////////////////////////////
;// DESTROY GRAPH
;////////////////////////////////
Func _GraphXRemove(ByRef $graphHWND)
    Local $handlesArray = $graphHWND[0]
    
    For $X = 0 to Ubound($handlesArray) - 1
        GUICtrlDelete($handlesArray[$X])
    Next
EndFunc

;randomMin = Lowest Y value
;randomMax = Highest Y value
;randomResolution = X axis resolution
;randomNumLines = Number of line graphs to generate
Func _GraphXRandomData($randomMin, $randomMax, $randomResolution, $randomNumLines = 1)
    Local $randomArray[$randomResolution][$randomNumLines]
    
    For $X = 0 to $randomResolution - 1
        For $Y = 0 to $randomNumLines - 1
            $randomArray[$X][$Y] = Random($randomMin,$randomMax,1)
        Next
    Next
    
    Return $randomArray
EndFunc
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...