Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start work on a benchmarking script that compares program executions … #160

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions targets/nikss/test/perf/simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from ptf import testutils as ptfutils # type: ignore

from backends.p4tools.modules.flay.targets.nikss.ebpf_ptf_test import P4EbpfTest
from backends.p4tools.modules.flay.targets.nikss.test.ebpf_ptf_test import (
TEST_PIPELINE_ID,
P4EbpfTest,
)

PORT0 = 0
PORT1 = 1
Expand All @@ -16,5 +19,6 @@ class SimpleTest(P4EbpfTest):
def runTest(self) -> None:
# check no connectivity if switching rules are not installed
pkt = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
rcvd_pkt = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00"
ptfutils.send_packet(self, PORT0, pkt)
ptfutils.verify_packet(self, pkt, PORT1)
ptfutils.verify_packet(self, rcvd_pkt, PORT1)
134 changes: 134 additions & 0 deletions targets/nikss/test/run-flay-ebpf-ptf-benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/usr/bin/env python3

import argparse
import logging
import os
import sys
import tempfile
from pathlib import Path
from typing import Any, Optional

import run_ebpf_ptf_test as ebpf_ptf_test

PARSER = argparse.ArgumentParser()
PARSER.add_argument(
"rootdir",
help="The root directory of the compiler source tree."
"This is used to import P4C's Python libraries",
)
PARSER.add_argument("p4_file", help="the p4 file to process")
PARSER.add_argument(
"-tf",
"--testfile",
dest="testfile",
help="The path for the ptf py file for this test.",
)
PARSER.add_argument(
"-td",
"--testdir",
dest="testdir",
help="The location of the test directory.",
)
PARSER.add_argument(
"-b",
"--nocleanup",
action="store_true",
dest="nocleanup",
help="Do not remove temporary results for failing tests.",
)
PARSER.add_argument(
"-n",
"--num-ifaces",
default=6,
dest="num_ifaces",
help="How many virtual interfaces to create.",
)
PARSER.add_argument(
"-ll",
"--log_level",
dest="log_level",
default="WARNING",
choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"],
help="The log level to choose.",
)
PARSER.add_argument(
"-fb", "--flay_binary", dest="flay_binary", default="flay", help="The path to the flay binary."
)
PARSER.add_argument(
"--config-update-pattern",
dest="config_update_pattern",
default=".*",
help="The config update pattern.",
)

# Parse options and process argv
ARGS, ARGV = PARSER.parse_known_args()

# Append the root directory to the import path.
FILE_DIR = Path(__file__).resolve().parent
ROOT_DIR = Path(ARGS.rootdir).absolute()
sys.path.append(str(ROOT_DIR))

from tools import testutils # pylint: disable=wrong-import-position


class Options(ebpf_ptf_test.Options):
"""Options for this testing script. Usually correspond to command line inputs."""

def __init__(self) -> None:
super().__init__()


def create_options(test_args: Any) -> Optional[Options]:
"""Parse the input arguments and create a processed options object."""
options = Options()
result = testutils.check_if_file(test_args.p4_file)
if not result:
return None
options.p4_file = result
testfile = test_args.testfile
if not testfile:
testutils.log.info("No test file provided. Checking for file in folder.")
testfile = options.p4_file.with_suffix(".py")
result = testutils.check_if_file(testfile)
if not result:
return None
options.testfile = Path(result)
testdir = test_args.testdir
if not testdir:
testutils.log.info("No test directory provided. Generating temporary folder.")
testdir = tempfile.mkdtemp(dir=Path(".").absolute())
# Generous permissions because the program is usually edited by sudo.
os.chmod(testdir, 0o755)
options.testdir = Path(testdir)
options.rootdir = Path(test_args.rootdir)
options.num_ifaces = test_args.num_ifaces

# Configure logging.
logging.basicConfig(
filename=options.testdir.joinpath("test.log"),
format="%(levelname)s: %(message)s",
level=getattr(logging, test_args.log_level),
filemode="w",
)
stderr_log = logging.StreamHandler()
stderr_log.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logging.getLogger().addHandler(stderr_log)
return options


if __name__ == "__main__":
test_options = create_options(ARGS)
if not test_options:
sys.exit(testutils.FAILURE)

if not testutils.check_root():
testutils.log.error("This script requires root privileges; Exiting.")
sys.exit(1)

# Run the test with the extracted options
test_result = ebpf_ptf_test.run_test(test_options)
if not (ARGS.nocleanup or test_result != testutils.SUCCESS):
testutils.log.info("Removing temporary test directory.")
testutils.del_dir(test_options.testdir)
sys.exit(test_result)
Loading