Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
ref #344
  • Loading branch information
jklmnn committed Jul 27, 2020
1 parent ff21da1 commit a8f8f6e
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env -S python3

import sys
from pathlib import Path
from time import perf_counter
from typing import Generator

from tqdm import tqdm # type: ignore

from rflx.pyrflx import PyRFLX


class Benchmark:
def __init__(self, specdir: Path) -> None:
print("Loading...")
start = perf_counter()
self.__pyrflx = PyRFLX([str(specdir / "ipv4.rflx"), str(specdir / "icmp.rflx")])
self.__ipv4 = self.__pyrflx["IPv4"]
self.__icmp = self.__pyrflx["ICMP"]
print(f"Loaded in {perf_counter() - start} seconds")

def generate(self) -> Generator[bytes, None, None]:
msg = self.__icmp["Message"]
pkt = self.__ipv4["Packet"]
for ident in range(0, 2 ** 16):
msg.set("Tag", "Echo_Request")
msg.set("Code_Zero", 0)
msg.set("Checksum", 0)
msg.set("Identifier", 0)
msg.set("Sequence_Number", ident)
msg.set("Data", bytes(8))

pkt.set("Version", 4)
pkt.set("IHL", 5)
pkt.set("DSCP", 0)
pkt.set("ECN", 0)
pkt.set("Total_Length", 20 + len(msg.bytestring))
pkt.set("Identification", 1)
pkt.set("Flag_R", "False")
pkt.set("Flag_DF", "False")
pkt.set("Flag_MF", "False")
pkt.set("Fragment_Offset", 0)
pkt.set("TTL", 64)
pkt.set("Protocol", "PROTOCOL_ICMP")
pkt.set("Header_Checksum", 0)
pkt.set("Source", 0)
pkt.set("Destination", 0)
pkt.set("Options", [])
pkt.set("Payload", msg.bytestring)
yield pkt.bytestring

def run(self) -> None:
i = 0
start = perf_counter()
for _ in tqdm(self.generate()):
if i % 500 == 0:
if perf_counter() - start > 1:
print("Performance < 500it/s, stopping")
sys.exit(1)
start = perf_counter()
i += 1


if __name__ == "__main__":
benchmark = Benchmark(Path(sys.argv[1]))
benchmark.run()

0 comments on commit a8f8f6e

Please sign in to comment.