/
recorder.py
60 lines (47 loc) · 2.42 KB
/
recorder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding: utf-8
"""
This example demonstrates the use of the Recorder Block. It does not require
any hardware to run, but necessitates the psutil module to be installed.
The Recorder Block saves the data it receives to a .csv file created at the
desired location. It can only record data from one Block, so multiple Recorders
must be used for recording data from multiple Blocks.
Here, a Recorder Block is used for saving the data recorded by an IOBlock
driving a FakeInOut that measures the RAM usage of the computer. Note that in
addition, A StopButton Block allows stopping the script properly without using
CTRL+C by clicking on a button.
After starting the script, a new file is created at demo_recorder/data.csv.
Nothing visual should happen. To end this demo, click on the stop button that
appears. You can also hit CTRL+C, but it is not a clean way to stop Crappy.
After stopping it, you can check that the data was recorded by opening the
created .csv file. Try to open and close memory-intensive applications (like
web browsers) during the test to see important RAM variations in the data.
"""
import crappy
if __name__ == '__main__':
# This IOBlock acquires the data for the Recorder Block to save
# It acquires data from the FakeInOut InOut, that reads the current RAM usage
# of the computer
mem = crappy.blocks.IOBlock(
'FakeInOut', # The name of the InOut to read data from
labels=('t(s)', 'memory(%)'), # The names of the labels to send to
# downstream Blocks
freq=30, # Lowering the default frequency because it's just a demo
# Sticking to default for the other arguments
)
# This Recorder Block records the data it receives from the IOBlock
# The data is recorded as text in a newly created .csv file
rec = crappy.blocks.Recorder(
'demo_recorder/data.csv', # The path to the file where the data will be
# saved. It does not need to already exist
labels=('t(s)', 'memory(%)'), # The names of the labels to record
freq=20, # No need to run at a higher frequency than the label to record
# Sticking to default for the other arguments
)
# This Block allows the user to properly exit the script
stop = crappy.blocks.StopButton(
# No specific argument to give for this Block
)
# Linking the Block so that the information is correctly sent and received
crappy.link(mem, rec)
# Mandatory line for starting the test, this call is blocking
crappy.start()