Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-645] Add flakiness checker #11572

Merged
merged 10 commits into from
Jul 24, 2018
106 changes: 106 additions & 0 deletions tools/flakiness_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

""" Checks a given test for flakiness
Takes the file name and function name of a test, as well as, optionally,
the number of trials to run and the random seed to use
"""

import subprocess
import sys
import os
import random
import argparse
import re


DEFAULT_NUM_TRIALS = 10000
DEFAULT_VERBOSITY = 2

def run_test_trials(args):
test_path = args.test_path + ":" + args.test_name
print("testing: " + test_path)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use logging instead of print. This allows you to make log messages without having to concatenate strings

new_env = os.environ.copy()
new_env["MXNET_TEST_COUNT"] = str(args.num_trials)

if args.seed is None:
print("Using random seed")
else:
new_env["MXNET_TEST_SEED"] = str(args.seed)

verbosity = "--verbosity=" + str(args.verbosity)

code = subprocess.call(["nosetests", verbosity, test_path],
env = new_env)

print("nosetests completed with return code " + str(code))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need to unset the environment variables after the run? Zero effect on the user's environment variables is preferred.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need, the user environment variables are local to the spawned process. The actual environment is not modified.


def find_test_path(test_file):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some documentation? From the name alone it was not clear to me what this exactly does. Also, how is it going to handle duplicates and the fact that we import tests into other testfiles, e.g. test_operator_gpu?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you take a closer look at the code, you'll see that the file name and test name are both provided by the user. So if you would like to run the gpu version of test_sigmoid then you'll do <path-to-test_operator_gpu.py>:test_sigmoid or test_operator_gpu.test_sigmoid. The problem you're asking is probably "if we modified some tests in test_operator.py, how do we detect that we also need to run the gpu version of it?", which is designed to be handled by other components in the project (this is stated in the design doc too).

"""Searches for the test file and returns the path if found
As a default, the currend working directory is the top of the search.
If a directory was provided as part of the argument, the directory will be
joined with cwd unless it was an absolute path, in which case, the
absolute path will be used instead.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if the behaviour described in this doc is actually resembled by the code. I'm a bit concerned about the absolute path because you always join with cwd. Also you should only append the .py if it isn't present yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path.join will only use the last absolute path provided, so if the user provides an absolute path it will override cwd, I've tested this and it does work this way. Currently, I'm using re.split to process the command line argument with test file and test name, and I'm removing .py from the case when it's present.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Very nice catch and approach!

test_file += ".py"
test_path = os.path.split(test_file)
top = os.path.join(os.getcwd(), test_path[0])

for (path, dirs, files) in os.walk(top):
if test_path[1] in files:
return os.path.join(path, test_path[1])
raise FileNotFoundError("Could not find " + test_path[1] +
"in directory: " + top)

class NameAction(argparse.Action):
"""Parses command line argument to get test file and test name"""
def __call__(self, parser, namespace, values, option_string=None):
name = re.split("\.py:|\.", values)
setattr(namespace, "test_path", find_test_path(name[0]))
setattr(namespace, "test_name", name[1])

def parse_args():
parser = argparse.ArgumentParser(description="Check test for flakiness")

parser.add_argument("test", action=NameAction,
help="file name and and function name of test, "
"provided in the format: <file-name>:<test-name> "
"or <directory>/<file>:<test-name>")

parser.add_argument("-n", "--num-trials", metavar="N",
default=DEFAULT_NUM_TRIALS, type=int,
help="number of test trials, passed as "
"MXNET_TEST_COUNT, defaults to 500")

parser.add_argument("-s", "--seed", type=int,
help="test seed, passed as MXNET_TEST_SEED, "
"defaults to random seed")

parser.add_argument("-v", "--verbosity",
default=DEFAULT_VERBOSITY, type=int,
help="logging level, passed to nosetests")


args = parser.parse_args()
return args


if __name__ == "__main__":
args = parse_args()

run_test_trials(args)