-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseOutput.m
583 lines (497 loc) · 16.4 KB
/
parseOutput.m
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
%% do parsing
clear all; close all;
!rm -f .tmp*
TIMESTAMP_INDEX = 2;
MANUAL_CRAWLING_MODE = false;
BROWSER = 'chrome';
if MANUAL_CRAWLING_MODE
OUTPUT_DIR = 'specific_jobs';
else
OUTPUT_DIR = 'output';
end
files = dir(OUTPUT_DIR);
sites = [];
for i=3:length(files)
sites = [sites {strtok(files(i).name,'-')}];
end
sites = unique(sites);
numSites = length(sites);
allConns = cell(1, numSites);
allConnsM = cell(1, numSites);
allLoadtimes = cell(1, numSites);
allLoadtimesM = cell(1, numSites);
allStaps = cell(1, numSites);
allStapsM = cell(1, numSites);
tic
parfor i=1:numSites
fname = sprintf('%s/%s-desktop-%s-conns.csv.bz2', OUTPUT_DIR, sites{i}, BROWSER);
fprintf('[%d of %d] %s\n', i, length(sites), sites{i})
if ~MANUAL_CRAWLING_MODE
[status, out] = system(sprintf('bzcat %s', fname));
if status ~= 0
fprintf('Skipping %s (incomplete) \n', sites{i});
continue
end
conns = str2num(out);
fname = sprintf('%s/%s-mobile-%s-conns.csv.bz2', OUTPUT_DIR, sites{i}, BROWSER);
[status, out] = system(sprintf('bzcat %s', fname));
if status ~= 0
fprintf('Skipping %s (incomplete) \n', sites{i});
continue
end
connsM = str2num(out);
fname = sprintf('%s/%s-desktop-%s-loadtime.csv.bz2', OUTPUT_DIR, sites{i}, BROWSER);
[status, out] = system(sprintf('bzcat %s', fname));
if status ~= 0
fprintf('Skipping %s (incomplete) \n', sites{i});
continue
end
% load times structure:
% [timeConnect,timeDomLoad, timeDns, timeRedirect, timeResponse]
loadtime = str2num(out);
fname = sprintf('%s/%s-mobile-%s-loadtime.csv.bz2', OUTPUT_DIR, sites{i}, BROWSER);
[status, out] = system(sprintf('bzcat %s', fname));
if status ~= 0
fprintf('Skipping %s (incomplete) \n', sites{i});
continue
end
loadtimeM = str2num(out);
% if dom never loaded, or if page loaded abnormally fast...
% TODO: fix this -- too many false positives
if loadtime(2) < 0 || loadtimeM(2) < 0 || ...
loadtime(1) <= 0 || loadtimeM(1) <= 0
fprintf('Skipping %s (no page load) \n', sites{i});
continue
end
fname = sprintf('%s/%s-mobile-%s-stap.csv.bz2', OUTPUT_DIR, sites{i}, BROWSER);
tmpname = sprintf('.tmp.%s', sites{i});
cmd = sprintf( 'bash masterStapParse.sh %s %s', fname, sites{i});
[status, out] = system(cmd);
if status ~= 0
fprintf('Skipping %s (incomplete) \n', sites{i});
continue
end
stapsM = importdata(tmpname);
delete(tmpname)
end
fname = sprintf('%s/%s-desktop-%s-stap.csv.bz2', OUTPUT_DIR, sites{i}, BROWSER);
tmpname = sprintf('.tmp.%s', sites{i});
cmd = sprintf( 'bash masterStapParse.sh %s %s', fname, sites{i});
[status, out] = system(cmd);
if status ~= 0
fprintf('Skipping %s (incomplete) \n', sites{i});
continue
end
staps = importdata(tmpname);
delete(tmpname)
if ~MANUAL_CRAWLING_MODE && (length(conns) < 250 || length(connsM) < 250 || ...
isempty(staps) || isempty(stapsM) || ...
size(stapsM, 2) ~= 3 || size(stapsM, 2) ~= 3)
fprintf('Skipping %s (odd output)\n', sites{i});
continue
end
startTime = min(staps(:,TIMESTAMP_INDEX));
staps(:,TIMESTAMP_INDEX) = staps(:,TIMESTAMP_INDEX) - startTime;
if ~MANUAL_CRAWLING_MODE
startTime = conns(1,1);
conns(:,1) = conns(:,1) - startTime;
startTime = connsM(1,1);
connsM(:,1) = connsM(:,1) - startTime;
startTime = min(stapsM(:,TIMESTAMP_INDEX));
stapsM(:,TIMESTAMP_INDEX) = stapsM(:,TIMESTAMP_INDEX) - startTime;
% ensure we're actually looking at time
assert(~any(staps(:,TIMESTAMP_INDEX) > 250))
assert(~any(stapsM(:,TIMESTAMP_INDEX) > 250))
else
loadtime = [];
conns = [];
connsM = [];
stapsM = [];
loadtimeM = [];
end
allConns{i} = conns;
allConnsM{i} = connsM;
allLoadtimes{i} = loadtime;
allLoadtimesM{i} = loadtimeM;
allStaps{i} = staps;
allStapsM{i}= stapsM;
end
% remove failed sites
for i=length(sites):-1:1
if isempty(allStaps{i})
sites(i) = [];
allConns(i) = [];
allConnsM(i) = [];
allLoadtimes(i) = [];
allLoadtimesM(i) = [];
allStaps(i) = [];
allStapsM(i) = [];
end
end
failedSites = numSites - length(sites);
numSites = length(sites);
toc
fprintf('Done! %d sites parsed successfully, %d failed.\n', numSites, failedSites)
%% link stap indices
fprintf('Extracting features using feature_names...\n')
load feature_names
stap_feature_names = feature_names;
assert(size(stap_feature_names,2) == 1)
stapTypes = length(stap_feature_names);
numSites = length(allStaps);
stapDataAggregated = cell(numSites, stapTypes);
stapDataAggregatedM = cell(numSites, stapTypes);
for i=1:numSites
staps = allStaps{i};
stapsM = allStapsM{i};
for j=1:stapTypes
relevantStaps = staps(staps(:,1) == j, 2:3);
stapDataAggregated{i,j} = relevantStaps;
if ~MANUAL_CRAWLING_MODE
relevantStapsM = stapsM(stapsM(:,1) == j, 2:3);
stapDataAggregatedM{i,j} = relevantStapsM;
end
end
if round(mod(i, numSites / 20)) == 0
fprintf('%.2f%% [%d of %d] \n', i/numSites*100,i,numSites)
end
end
fprintf('Saving output...\n')
save output
%% plot staps (single site)
siteIndex = length(sites) - 17;
stapID = 119;
relevantStap = stapDataAggregated{siteIndex, stapID};
timestamps = relevantStap(:,1);
feature = relevantStap(:,end);
plot(timestamps,feature)
title(sites{siteIndex});
ylabel(strrep(stap_feature_names(stapID), '_', '\_'))
xlabel('Time (sec)')
axis tight
%% plot everything individually (all sites)
save_figs = true;
use_export_fig = false;
use_hgsave = true;
close all;
mkdir('figs');
tic
for i=1:numSites
sitename = sites(i);
fprintf('[%d of %d] %s\n', i, length(sites), sites{i})
mkdir(sprintf('figs/%s',sitename{:}));
for j=1:stapTypes
featureName = stap_feature_names(j);
relevantStap = stapDataAggregated{i, j};
relevantStapM = stapDataAggregatedM{i, j};
if size(relevantStap,1) <= 5
continue
end
fprintf('--> plotting %s (%d of %d) \n', featureName{:}, j, stapTypes)
% relevantStap-format: [timestamp, feature]
stap_time_series = sortrows(relevantStap);
stap_time_seriesM = sortrows(relevantStapM);
subplot(2,1,1)
plot(stap_time_series(:,1),stap_time_series(:,2), ...
'Linewidth', 1, 'MarkerSize',4, 'Color', [55 126 184]/255)
hold all
plot(stap_time_seriesM(:,1),stap_time_seriesM(:,2), ...
'--','Linewidth', 1, 'MarkerSize',4,'Color',[77 175 74]/255)
box off
ylabel(featureName{:})
title(sprintf('%s -- %s', sitename{:}, cell2mat(strrep(featureName, '_', '\_'))))
legend('Desktop UA' ,'Mobile UA')
subplot(2,1,2)
plot(stap_time_series(:,1),cumsum(stap_time_series(:,2)), ...
'Linewidth', 1, 'MarkerSize',4, 'Color', [55 126 184]/255)
hold all
plot(stap_time_seriesM(:,1),cumsum(stap_time_seriesM(:,2)), ...
'--','Linewidth', 1, 'MarkerSize',4,'Color',[77 175 74]/255)
box off
ylabel('Cumulative sum')
xlabel('Time (seconds)')
legend('Desktop UA' ,'Mobile UA')
if save_figs
set(gcf,'PaperPositionMode','auto')
if use_export_fig
export_fig(sprintf('figs/%s/%d.%s.png', ...
sitename{:}, j, featureName{:}), '-r300')
else
print(gcf,'-dpng','-r300', sprintf('figs/%s/%d.%s.png', ...
sitename{:}, j, featureName{:}))
end
if use_hgsave
hgsave(sprintf('figs/%s/%d.%s.fig', ...
sitename{:}, j, featureName{:}));
end
else
pause
end
clf('reset')
end
end
close all
toc
%% calculate aggregates
DURATION = 150; % seconds
BINS = 30;
binduration = DURATION / BINS;
aggDat = zeros(numSites, stapTypes, BINS);
aggDatM = zeros(numSites, stapTypes, BINS);
for i=1:numSites
fprintf('[%d of %d]\n', i, numSites)
for j=1:stapTypes
relevantStap = stapDataAggregated{i, j};
relevantStapM = stapDataAggregatedM{i, j};
stap_time_series = sortrows(relevantStap);
stap_time_seriesM = sortrows(relevantStapM);
for b=1:BINS
binDat = stap_time_series(binduration * (b-1) <= stap_time_series(:,1) & ...
stap_time_series(:,1) < (binduration * b), 2);
aggDat(i, j, b) = sum(binDat);
binDatM = stap_time_seriesM(binduration * (b-1) <= stap_time_seriesM(:,1) & ...
stap_time_seriesM(:,1) < (binduration * b), 2);
aggDatM(i, j, b) = sum(binDatM);
end
end
end
fprintf('Done!\n')
%% plot aggregate means
% close all
subplot(2,1,1)
means = mean(squeeze(aggDat(:,119,:)/1024));
stds = std(squeeze(aggDat(:,119,:)/1024));
meansM = mean(squeeze(aggDatM(:,119,:)/1024));
stdsM = std(squeeze(aggDatM(:,119,:)/1024));
errorbar((0:BINS-1)*binduration, means,stds, 'linewidth', 1', 'Color', [55 126 184]/255)
hold all
errorbar((0:BINS-1)*binduration, meansM,stdsM,'--', 'linewidth', 1, 'Color', [77 175 74]/255)
box off
axis tight
title('Network Activity (Sent)')
ylabel('KB sent')
xlabel('Time (seconds)')
legend('Desktop UA', 'Mobile UA')
tmp = ylim;
ylim([0 tmp(2)])
subplot(2,1,2)
means = mean(squeeze(aggDat(:,118,:)/1024));
stds = std(squeeze(aggDat(:,118,:)/1024));
meansM = mean(squeeze(aggDatM(:,118,:)/1024));
stdsM = std(squeeze(aggDatM(:,118,:)/1024));
errorbar((0:BINS-1)*binduration, means,stds, 'linewidth', 1', 'Color', [55 126 184]/255)
hold all
errorbar((0:BINS-1)*binduration, meansM,stdsM,'--', 'linewidth', 1, 'Color', [77 175 74]/255)
box off
axis tight
title('Network Activity (Received)')
ylabel('KB received')
xlabel('Time (seconds)')
legend('Desktop UA', 'Mobile UA')
tmp = ylim;
ylim([0 tmp(2)])
%% plot aggregate errorbars
mkdir('figs-aggregate')
close all
save_figs = true;
for j=1:stapTypes
featureName = stap_feature_names(j);
means = mean(squeeze(aggDat(:,j,:)));
stds = std(squeeze(aggDat(:,j,:)));
errorbar((0:BINS-1)*binduration, means,stds, 'linewidth', 1,'Color', [55 126 184]/255)
hold all
meansM = mean(squeeze(aggDatM(:,j,:)));
stdsM = std(squeeze(aggDatM(:,j,:)));
errorbar((0:BINS-1)*binduration, meansM,stdsM,'--', 'linewidth', 1, 'Color',[77 175 74]/255)
ylabel(strcat(strrep(featureName, '_', '\_'), ' syscalls'))
xlabel('Time (seconds)')
legend('Desktop UA', 'Mobile UA')
box off
tmp = ylim;
tmp(1) = 0;
ylim(tmp)
xlim([0 BINS * binduration])
if save_figs
set(gcf,'PaperPositionMode','auto');
print(gcf,'-dpng','-r300', sprintf('figs-aggregate/%d-%s-eb.png', ...
j, featureName{:}))
hgsave(sprintf('figs-aggregate/%d-%s-eb.fig', ...
j, featureName{:}))
else
pause
end
clf('reset')
end
close all
%% plot aggregates boxplots
mkdir('figs-aggregate')
close all
save_figs = true;
for j=1:stapTypes
featureName = stap_feature_names(j);
subplot(2,1,1)
boxplot(squeeze(aggDat(:,j,:)), 'plotstyle','compact','symbol', '')
set(gca,'XTickLabel',{' '})
title(strrep(featureName, '_', '\_'))
axis tight
ylabel('Frequency (desktop)')
% ylim([0 1e4])
subplot(2,1,2)
boxplot(squeeze(aggDatM(:,j,:)), 'plotstyle','compact','symbol', '')
set(gca,'XTickLabel',{' '})
% ylim([0 1e4])
axis tight
ylabel('Frequency (mobile)')
if save_figs
set(gcf,'PaperPositionMode','auto');
print(gcf,'-dpng','-r300', sprintf('figs-aggregate/%d-%s.png', ...
j, featureName{:}))
else
pause
end
clf('reset')
end
close all
%% plot loadtimes
loadtimes = cell2mat(cellfun(@(c) c', allLoadtimes, 'uniformoutput', false));
loadtimesM = cell2mat(cellfun(@(c) c', allLoadtimesM, 'uniformoutput', false));
lnames = ['timeConnect','timeDomLoad', 'timeDns', 'timeRedirect', 'timeResponse'];
%% setup connection matrix
conns = cell2mat(cellfun(@(c) c(1:500,2), allConns, 'uniformoutput', false));
connsM = cell2mat(cellfun(@(c) c(1:500,2), allConnsM, 'uniformoutput', false));
%% plot results
close all
for i=1:numSites
conns = allConns{i};
plot(conns(:,1), conns(:,2))
hold all
end
% legend(sites)
%% boxplots
subplot(2,1,1)
boxplot(conns')
ylabel('Connections')
set(gca,'XTickLabel',{' '})
title('Desktop UA')
subplot(2,1,2)
boxplot(connsM')
set(gca,'XTickLabel',{' '})
ylabel('Connections')
title('Mobile UA')
%% means
plot((1:size(conns,1))/4.5,mean(conns, 2),'linewidth', 1)
hold all
plot((1:size(conns,1))/4.5,mean(connsM, 2), '--', 'linewidth', 1)
legend('Desktop UA', 'Mobile UA')
ylabel('Mean connections')
xlabel('Time (seconds)')
axis tight
%% barplot
bar([mean(conns); mean(connsM)]')
ylabel('Mean connections')
xlabel('Site index')
%% histogram
subplot(2,1,1)
hist([mean(conns)' mean(connsM)'], 20)
legend('Desktop UA', 'Mobile UA')
xlabel('Mean connections')
subplot(2,1,2)
hist([max(conns)' max(connsM)'], 20)
legend('Desktop UA', 'Mobile UA')
xlabel('Peak connections')
%% image-style
subplot(2,1,1)
imagesc(conns, [0 50])
ylabel('Time')
title('Desktop UA')
subplot(2,1,2)
imagesc(connsM, [0 50])
ylabel('Time')
title('Mobile UA')
%% single site
plot((1:600)/4.5, conns(:,15))
hold all
plot((1:600)/4.5, connsM(:,15),'--')
legend('Desktop UA', 'Mobile UA')
ylabel('Number of Connections')
xlabel('Time (seconds)')
%%
%%
%% BROWSER SPECIFIC PLOTTING
%% ASSUMES: firefox, chrome structs
%%
%%
%% plot staps (single site), specific site
close all
site = 'ebay.com';
subplot(1,2,1)
v2struct(firefox)
siteIndex = find(strcmp(sites, site));
assert(siteIndex > 0)
stapID_FF = 118;
relevantStap = stapDataAggregated{siteIndex, stapID_FF};
timestamps = relevantStap(:,1);
feature = relevantStap(:,end);
totalSyscallsFirefox = sum(feature);
plot(timestamps,feature)
title(strcat(site, ' -- firefox'));
ylabel(strrep(stap_feature_names(stapID_FF), '_', '\_'))
xlabel('Time (sec)')
axis tight
subplot(1,2,2)
v2struct(chrome)
siteIndex = find(strcmp(sites, site));
assert(siteIndex > 0)
relevantStap = stapDataAggregated{siteIndex, stapID_FF};
timestamps = relevantStap(:,1);
feature = relevantStap(:,end);
totalSyscallsChrome = sum(feature);
plot(timestamps,feature)
title(strcat(site, ' -- chrome'));
ylabel(strrep(stap_feature_names(stapID_FF), '_', '\_'))
xlabel('Time (sec)')
axis tight
fprintf('-- %s (%s) --\n', stap_feature_names{stapID_FF}, site)
fprintf('Total Chrome network: %.2fMB\n', totalSyscallsChrome / 1024 / 1024)
fprintf('Total Firefox network: %.2fMB\n\n', totalSyscallsFirefox / 1024 / 1024)
%% plot total syscalls
% aggDat = [site,syscall,timestep]
close all
v2struct(chrome)
aggDatOnlySyscalls = aggDat(:,1:115,:);
totalSyscallsChrome = sum(mean((sum(aggDatOnlySyscalls,2)),1)); % mean per site
plot((0:BINS-1)*binduration, squeeze(mean((sum(aggDatOnlySyscalls,2)),1)), ...
'linewidth', 1, 'Color', [69 117 180]/255)
v2struct(firefox)
aggDatOnlySyscalls = aggDat(:,1:115,:);
totalSyscallsFirefox = sum(mean((sum(aggDatOnlySyscalls,2)),1)); % mean per site
hold all
plot((0:BINS-1)*binduration, squeeze(mean((sum(aggDatOnlySyscalls,2)),1)), ...
'--', 'linewidth', 1, 'Color', [215 48 39]/255)
legend('Chrome', 'Firefox')
ylabel('Mean number of system calls')
xlabel('Time (sec)')
box off
fprintf('Total Chrome syscalls: %d\n', totalSyscallsChrome)
fprintf('Total Firefox syscalls: %d\n', totalSyscallsFirefox)
%% number of connections
close all
figure; hold all;
v2struct(chrome)
conns = cell2mat(cellfun(@(c) c(1:500,2), allConns, 'uniformoutput', false));
connsM = cell2mat(cellfun(@(c) c(1:500,2), allConnsM, 'uniformoutput', false));
plot((1:size(conns,1))/4.5,mean(conns, 2),'linewidth', 1)
plot((1:size(connsM,1))/4.5,mean(connsM, 2),'--', 'linewidth', 1)
v2struct(firefox)
conns = cell2mat(cellfun(@(c) c(1:500,2), allConns, 'uniformoutput', false));
connsM = cell2mat(cellfun(@(c) c(1:500,2), allConnsM, 'uniformoutput', false));
hold all
plot((1:size(conns,1))/4.5,mean(conns, 2), 'linewidth', 1)
plot((1:size(connsM,1))/4.5,mean(connsM, 2),'--', 'linewidth', 1)
legend('Chrome', 'Chrome-mobile','Firefox', 'Firefox-mobile')
ylabel('Mean connections')
xlabel('Time (seconds)')
axis tight
%%
set(gcf,'PaperPositionMode','auto')
print(gcf,'-dpng','-r300', 'browser-syscalls.png')