Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

287 lines
9.6KB

  1. #!/usr/bin/env python3
  2. import math
  3. import multiprocessing
  4. import os
  5. from argparse import ArgumentParser
  6. import matplotlib
  7. import numpy as np
  8. import pandas as pd
  9. import matplotlib.pyplot as plt
  10. import seaborn as sns
  11. sns.set()
  12. #sns.set(font_scale=1.5)
  13. tex_fonts = {
  14. "pgf.texsystem": "lualatex",
  15. # "legend.fontsize": "x-large",
  16. # "figure.figsize": (15, 5),
  17. "axes.labelsize": 15, # "small",
  18. # "axes.titlesize": "x-large",
  19. "xtick.labelsize": 15, # "small",
  20. "ytick.labelsize": 15, # "small",
  21. "legend.fontsize": 15,
  22. "axes.formatter.use_mathtext": True,
  23. "mathtext.fontset": "dejavusans",
  24. }
  25. # plt.rcParams.update(tex_fonts)
  26. def convert_cellid(value):
  27. if isinstance(value, str):
  28. try:
  29. r = int(value.split(" ")[-1].replace("(", "").replace(")", ""))
  30. return r
  31. except Exception as e:
  32. return -1
  33. else:
  34. return int(-1)
  35. if __name__ == "__main__":
  36. parser = ArgumentParser()
  37. parser.add_argument("-s", "--serial_file", required=True, help="Serial csv file.")
  38. parser.add_argument(
  39. "-p", "--pcap_csv_folder", required=True, help="PCAP csv folder."
  40. )
  41. parser.add_argument("--save", required=True, help="Location to save pdf file.")
  42. parser.add_argument("--fancy", action="store_true", help="Create fancy plot.")
  43. parser.add_argument(
  44. "-i",
  45. "--interval",
  46. default=10,
  47. type=int,
  48. help="Time interval for rolling window.",
  49. )
  50. args = parser.parse_args()
  51. pcap_csv_list = list()
  52. for filename in os.listdir(args.pcap_csv_folder):
  53. if filename.endswith(".csv") and "tcp" in filename:
  54. pcap_csv_list.append(filename)
  55. counter = 1
  56. if len(pcap_csv_list) == 0:
  57. print("No CSV files found.")
  58. pcap_csv_list.sort(key=lambda x: int(x.split("_")[-1].replace(".csv", "")))
  59. for csv in pcap_csv_list:
  60. print(
  61. "\rProcessing {} out of {} CSVs.\t({}%)\t".format(
  62. counter, len(pcap_csv_list), math.floor(counter / len(pcap_csv_list))
  63. )
  64. )
  65. # try:
  66. transmission_df = pd.read_csv(
  67. "{}{}".format(args.pcap_csv_folder, csv),
  68. dtype=dict(is_retranmission=bool, is_dup_ack=bool),
  69. )
  70. transmission_df = transmission_df.set_index("datetime")
  71. transmission_df.index = pd.to_datetime(transmission_df.index)
  72. transmission_df = transmission_df.sort_index()
  73. # srtt to [s]
  74. transmission_df["srtt"] = transmission_df["srtt"].apply(lambda x: x / 10 ** 6)
  75. # key for columns and level for index
  76. transmission_df["goodput"] = (
  77. transmission_df["payload_size"]
  78. .groupby(pd.Grouper(level="datetime", freq="{}s".format(args.interval)))
  79. .transform("sum")
  80. )
  81. transmission_df["goodput"] = transmission_df["goodput"].apply(
  82. lambda x: ((x * 8) / args.interval) / 10 ** 6
  83. )
  84. transmission_df["goodput_rolling"] = (
  85. transmission_df["payload_size"].rolling("{}s".format(args.interval)).sum()
  86. )
  87. transmission_df["goodput_rolling"] = transmission_df["goodput_rolling"].apply(
  88. lambda x: ((x * 8) / args.interval) / 10 ** 6
  89. )
  90. # set meta values and remove all not needed columns
  91. cc_algo = transmission_df["congestion_control"].iloc[0]
  92. cc_algo = cc_algo.upper()
  93. transmission_direction = transmission_df["direction"].iloc[0]
  94. # transmission_df = transmission_df.filter(["goodput", "datetime", "ack_rtt", "goodput_rolling", "snd_cwnd"])
  95. # read serial csv
  96. serial_df = pd.read_csv(
  97. args.serial_file, converters={"Cell_ID": convert_cellid}
  98. )
  99. serial_df = serial_df.set_index("datetime")
  100. serial_df.index = pd.to_datetime(serial_df.index)
  101. serial_df.sort_index()
  102. transmission_df = pd.merge_asof(
  103. transmission_df,
  104. serial_df,
  105. tolerance=pd.Timedelta("1milliseconds"),
  106. right_index=True,
  107. left_index=True,
  108. )
  109. transmission_df.index = transmission_df["arrival_time"]
  110. # filter active state
  111. for i in range(1, 5):
  112. transmission_df["LTE_SCC{}_effective_bw".format(i)] = transmission_df[
  113. "LTE_SCC{}_bw".format(i)
  114. ]
  115. mask = transmission_df["LTE_SCC{}_state".format(i)].isin(["ACTIVE"])
  116. transmission_df["LTE_SCC{}_effective_bw".format(i)] = transmission_df[
  117. "LTE_SCC{}_effective_bw".format(i)
  118. ].where(mask, other=0)
  119. # filter if sc is usesd for uplink
  120. for i in range(1, 5):
  121. mask = transmission_df["LTE_SCC{}_UL_Configured".format(i)].isin([False])
  122. transmission_df["LTE_SCC{}_effective_bw".format(i)] = transmission_df[
  123. "LTE_SCC{}_effective_bw".format(i)
  124. ].where(mask, other=0)
  125. # sum all effective bandwidth for 5G and 4G
  126. transmission_df["SCC1_NR5G_effective_bw"] = transmission_df[
  127. "SCC1_NR5G_bw"
  128. ].fillna(0)
  129. transmission_df["lte_effective_bw_sum"] = (
  130. transmission_df["LTE_SCC1_effective_bw"].fillna(0)
  131. + transmission_df["LTE_SCC2_effective_bw"].fillna(0)
  132. + transmission_df["LTE_SCC3_effective_bw"].fillna(0)
  133. + transmission_df["LTE_SCC4_effective_bw"].fillna(0)
  134. + transmission_df["LTE_bw"].fillna(0))
  135. transmission_df["nr_effective_bw_sum"] = transmission_df["SCC1_NR5G_effective_bw"]
  136. transmission_df["effective_bw_sum"] = transmission_df["nr_effective_bw_sum"] + transmission_df[
  137. "lte_effective_bw_sum"]
  138. # transmission timeline
  139. scaley = 1.5
  140. scalex = 1.0
  141. fig, ax = plt.subplots(2, 1, figsize=[6.4 * scaley, 4.8 * scalex])
  142. fig.subplots_adjust(right=0.75)
  143. if not args.fancy:
  144. plt.title("{} with {}".format(transmission_direction, cc_algo))
  145. fig.suptitle("{} with {}".format(transmission_direction, cc_algo))
  146. ax0 = ax[0]
  147. ax1 = ax0.twinx()
  148. ax2 = ax0.twinx()
  149. # ax2.spines.right.set_position(("axes", 1.22))
  150. ax00 = ax[1]
  151. snd_plot = ax0.plot(
  152. transmission_df["snd_cwnd"].dropna(),
  153. color="lime",
  154. linestyle="dashed",
  155. label="cwnd",
  156. )
  157. srtt_plot = ax1.plot(
  158. transmission_df["srtt"].dropna(),
  159. color="red",
  160. linestyle="dashdot",
  161. label="sRTT",
  162. )
  163. goodput_plot = ax2.plot(
  164. transmission_df["goodput_rolling"],
  165. color="blue",
  166. linestyle="solid",
  167. label="goodput",
  168. )
  169. # sum all effective bandwidth for 5G and 4G
  170. transmission_df["SCC1_NR5G_effective_bw"] = transmission_df["SCC1_NR5G_bw"].fillna(0)
  171. transmission_df["effective_bw_sum"] = (
  172. transmission_df["SCC1_NR5G_effective_bw"]
  173. + transmission_df["LTE_SCC1_effective_bw"]
  174. + transmission_df["LTE_SCC2_effective_bw"]
  175. + transmission_df["LTE_SCC3_effective_bw"]
  176. + transmission_df["LTE_SCC4_effective_bw"]
  177. + transmission_df["LTE_bw"]
  178. )
  179. bw_cols = [
  180. "SCC1_NR5G_effective_bw",
  181. "LTE_bw",
  182. "LTE_SCC1_effective_bw",
  183. "LTE_SCC2_effective_bw",
  184. "LTE_SCC3_effective_bw",
  185. "LTE_SCC4_effective_bw",
  186. ]
  187. transmission_df.to_csv("{}{}_plot.csv".format(args.save, csv.replace(".csv", "")))
  188. exit()
  189. ax_stacked = transmission_df[bw_cols].plot.area(stacked=True, linewidth=0, ax=ax00)
  190. ax00.set_ylabel("bandwidth [MHz]")
  191. #ax.set_xlabel("time [minutes]")
  192. #ax00.set_xlim([0, transmission_df.index[-1]])
  193. ax00.xaxis.grid(False)
  194. ax2.spines.right.set_position(("axes", 1.1))
  195. ax0.set_ylim(0, 5000)
  196. ax1.set_ylim(0, 0.3)
  197. ax2.set_ylim(0, 600)
  198. #ax00.set_ylim(-25, 0)
  199. ax00.set_xlabel("arrival time [s]")
  200. ax2.set_ylabel("Goodput [mbps]")
  201. #ax00.set_ylabel("LTE/NR RSRQ [dB]")
  202. # ax02.set_ylabel("LTE RSRQ [dB]")
  203. ax1.set_ylabel("sRTT [s]")
  204. ax0.set_ylabel("cwnd [MSS]")
  205. if args.fancy:
  206. legend_frame = False
  207. ax0.set_xlim([0, transmission_df.index[-1]])
  208. ax00.set_xlim([0, transmission_df.index[-1]])
  209. # added these three lines
  210. lns_ax0 = snd_plot + srtt_plot + goodput_plot
  211. labs_ax0 = [l.get_label() for l in lns_ax0]
  212. ax2.legend(lns_ax0, labs_ax0, ncols=9, fontsize=9, loc="upper right", frameon=legend_frame)
  213. #ax0.set_zorder(100)
  214. #lns_ax00 = [ax_stacked]
  215. #labs_ax00 = ["5G bandwidth", "4G bandwidth"]
  216. #ax00.legend(lns_ax00, labs_ax00, ncols=3, fontsize=9, loc="upper center", frameon=legend_frame)
  217. L = ax00.legend(ncols=3, fontsize=9, frameon=False)
  218. L.get_texts()[0].set_text("5G main")
  219. L.get_texts()[1].set_text("4G main")
  220. L.get_texts()[2].set_text("4G SCC 1")
  221. L.get_texts()[3].set_text("4G SCC 2")
  222. L.get_texts()[4].set_text("4G SCC 3")
  223. L.get_texts()[5].set_text("4G SCC 4")
  224. #ax00.set_zorder(100)
  225. plt.savefig("{}{}_plot.eps".format(args.save, csv.replace(".csv", "")), bbox_inches="tight")
  226. else:
  227. fig.legend(loc="lower right")
  228. plt.savefig("{}{}_plot.pdf".format(args.save, csv.replace(".csv", "")), bbox_inches="tight")
  229. # except Exception as e:
  230. # print("Error processing file: {}".format(csv))
  231. # print(str(e))
  232. counter += 1
  233. plt.close(fig)
  234. plt.clf()