plot.py
2.88 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python
import os
import json
from collections import OrderedDict
from dateutil import parser as dtparser
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt, rc
import numpy as np
DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
FIG_NAME = 'MonitorPowerConsumption.pdf'
monitor_data = dict()
normal_data = dict()
packet_count = 0
for dirpath, dirname, filenames in os.walk(os.path.join(DATA_DIR, 'monitor25')):
for f in filenames:
with open(os.path.join(dirpath, f), 'r') as f:
for line in f:
l = json.loads(line)
if 'action' not in l:
continue
if l['action'] == 'android.intent.action.BATTERY_CHANGED':
monitor_data[dtparser.parse(l['timestamp'])] = l
elif l['action'] == 'edu.buffalo.cse.pocketsniffer.tasks.SnifTask.ProgressUpdate':
packet_count += l['totalPackets']
for t in sorted(monitor_data.keys()):
if monitor_data[t]['level'] == 1.0:
monitor_start = t
break
print("%d battery updates found for monitor mode." % (len(monitor_data)))
for dirpath, dirname, filenames in os.walk(os.path.join(DATA_DIR, 'normal')):
for f in filenames:
with open(os.path.join(dirpath, f), 'r') as f:
for line in f:
l = json.loads(line)
if 'action' not in l:
continue
if l['action'] == 'android.intent.action.BATTERY_CHANGED':
normal_data[dtparser.parse(l['timestamp'])] = l
normal_start = min(normal_data.keys())
print("%d battery updates found for normal mode." % (len(normal_data)))
monitor_levels = OrderedDict()
for t in sorted(monitor_data.keys()):
if t < monitor_start:
continue
monitor_levels[(t-monitor_start).total_seconds()] = monitor_data[t]['level']
normal_levels = OrderedDict()
for t in sorted(normal_data.keys()):
normal_levels[(t-normal_start).total_seconds()] = normal_data[t]['level']
rc('font', **{'family':'serif', 'serif':['Palatino'], 'size':'9'})
rc('text', usetex=True)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(normal_levels.keys(), normal_levels.values(), '-g*', markersize=3, markeredgewidth=0, linewidth=0.5, label='\\textbf{Baseline}')
ax.plot(monitor_levels.keys(), monitor_levels.values(), '-ro', markersize=2, markeredgewidth=0, linewidth=0.5, label='\\textbf{Monitor Mode (%s pkts)}' % ("{:,}".format(packet_count)))
ax.set_xlabel('\\textbf{Elapsed Time} (hour)')
ax.set_ylabel('\\textbf{Battery Level}')
limit = max([max(monitor_levels.keys()), max(normal_levels.keys())])
limit = ((limit / 3600) + 1) * 3600 + 1
ax.set_xticks(np.arange(0, limit, 3600))
ax.set_xticklabels([str(int(i/3600)) for i in np.arange(0, limit, 3600)])
for axis in ['top', 'bottom', 'left', 'right']:
ax.spines[axis].set_linewidth(0.5)
ax.grid(True)
ax.legend(loc='upper right', markerscale=1, numpoints=1, scatterpoints=1, fontsize='8')
fig.set_size_inches(4, 3)
fig.savefig(FIG_NAME, bbox_inches='tight')