-
Notifications
You must be signed in to change notification settings - Fork 0
/
ETL.py
209 lines (135 loc) · 5.35 KB
/
ETL.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
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
#!/usr/bin/env python
# coding: utf-8
# In[22]:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import findspark
findspark.init()
from pyspark.sql import SparkSession
spark = SparkSession.builder .master('local') .appName('ETL') .config('spark.executor.memory', '5gb') .config("spark.cores.max", "6") .getOrCreate()
sc = spark.sparkContext
from pyspark.sql.window import Window
from pyspark.sql import functions as F
# In[23]:
from pyspark.sql.types import StructType, StructField, IntegerType, TimestampType, BooleanType, StringType
schema = StructType([
StructField("timestamp", TimestampType(), True),
StructField("stream", IntegerType(), True),
StructField("sent", BooleanType(), True),
StructField("seqnum", IntegerType(), True),
StructField("ack", BooleanType(), True),
StructField("acknum", IntegerType(), True)
])
# In[24]:
def read_npz_to_df(filename):
data = np.load(filename+'.npz')['packets']
pd_df = pd.DataFrame(data=data)
pd_df.drop(columns=['src', 'dst', 'src_port', 'dst_port'], inplace=True)
pd_df.to_csv(filename+'.csv', index=False)
df = spark.read.option("header", "true").csv(filename+'.csv')
return df
def find_ack_for_seq(df):
df.createOrReplaceTempView("df")
df_joined = spark.sql("""
select df1.stream, df1.timestamp, df1.sent, df1.seqnum, min(df2.timestamp) as acktimestamp
from df df1 inner join df df2
on df1.seqnum<df2.acknum
and df1.sent!=df2.sent
and df1.stream=df2.stream
group by df1.stream, df1.timestamp, df1.seqnum, df1.sent
order by df1.stream
""")
return df_joined
def shift_windows(df, streamno):
shift_window = Window.partitionBy().orderBy('timestamp')
#calculate sequence length
df.createOrReplaceTempView("df")
df_shifted = spark.sql("select * from df where stream={} and sent=True".format(streamno))
df_shifted = df_shifted.withColumn('seqnum_1', F.lag(df_shifted.seqnum).over(shift_window))
df_shifted = df_shifted.withColumn('seqlength', F.when(F.isnull(df_shifted.seqnum - df_shifted.seqnum_1), 0)
.otherwise(df_shifted.seqnum - df_shifted.seqnum_1))
#calculate g_in
df_shifted = df_shifted.withColumn('timestamp_1', F.lag(df_shifted.timestamp).over(shift_window))
df_shifted = df_shifted.withColumn('gin', F.when(F.isnull(df_shifted.timestamp - df_shifted.timestamp_1), 0)
.otherwise(df_shifted.timestamp - df_shifted.timestamp_1))
#calculate g_ack
df_shifted = df_shifted.withColumn('acktimestamp_1', F.lag(df_shifted.acktimestamp).over(shift_window))
df_shifted = df_shifted.withColumn('gack', F.when(F.isnull(df_shifted.acktimestamp - df_shifted.acktimestamp_1), 0)
.otherwise(df_shifted.acktimestamp - df_shifted.acktimestamp_1))
return df_shifted
def sanity_check(df):
df.createOrReplaceTempView("df_shifted")
df_sanity = spark.sql("""
select timestamp, timestamp_1, seqnum, seqnum_1, seqlength, gin
from df_shifted
where df_shifted.seqlength<0
and sent=True
and stream=0
order by timestamp asc
""")
if df_sanity.count()==0:
return True
else:
return False
def get_stream_arrays(df, streamno):
df.createOrReplaceTempView("df_stream")
stream = spark.sql("""
select * from df_stream
where df_stream.stream={}
""".format(streamno))\
.rdd.map(lambda row: (row.timestamp, row.seqlength, row.gin, row.gack))
ts_array = np.array(stream.map(lambda ts_l: ts_l[0]).collect())
l_array = np.array(stream.map(lambda ts_l: ts_l[1]).collect())
gin_array = np.array(stream.map(lambda ts_l: ts_l[2]).collect())
gack_array = np.array(stream.map(lambda ts_l: ts_l[3]).collect())
return ts_array, l_array, gin_array, gack_array
# In[32]:
import glob, os
os.chdir("dumpfiles")
for file in glob.glob("*.npz"):
print(file)
os.chdir('../')
# In[42]:
filename = 'dumpfiles/dumpfile_3792_4_474.npz'
filename = filename.split('.npz')[0]
print(filename)
streamno = 0
df = read_npz_to_df(filename)
df_joined = find_ack_for_seq(df)
df_shifted = shift_windows(df_joined, streamno)
if sanity_check(df_shifted):
ts_array, l_array, gin_array, gack_array = get_stream_arrays(df_shifted, streamno)
# In[43]:
df.filter(df.stream==0).show()
# In[44]:
sns.distplot(l_array)
print(l_array.mean(), l_array.std(), l_array.min(), l_array.max(), np.median(l_array))
# In[45]:
sns.distplot(gin_array)
# In[46]:
sns.distplot(gack_array)
# In[47]:
plt.figure(figsize=(20,5))
plt.scatter(gin_array, gack_array, c=l_array)
plt.yscale('log')
plt.xscale('log')
plt.colorbar()
# In[48]:
plt.figure(figsize=(20,5))
plt.scatter(gin_array, l_array, c=gack_array)
#plt.yscale('log')
#plt.xscale('log')
# In[49]:
plt.figure(figsize=(20,5))
plt.plot(gack_array/gin_array, l_array/gin_array, 'o')
#plt.yscale('log')
#plt.xscale('log')
# In[50]:
df_shifted.select(['gin', 'gack']).show()
#df_shifted.select('gin').rdd.flatMap(list).collect()
# In[ ]:
# In[ ]:
# In[ ]: