简体   繁体   English

如何使用PyTest / tox测试速度?

[英]How do I test for speed with PyTest / tox?

For testing machine learning algorithms / repositories, I see three things that matter: 对于测试机器学习算法/存储库,我认为很重要的三件事:

  1. Does it crash 它会崩溃吗
  2. Does it have a minimum test accuracy 是否具有最低的测试精度
  3. Is it fast enough 够快吗

While (1) and maybe (2) is standard unit testing, I'm not too sure how to deal with (3). 虽然(1)也许是(2)是标准单元测试,但我不太确定如何处理(3)。 Can I test this with pytest / tox? 我可以用pytest / tox测试吗?

I found pytest-benchmark , but how would I do this for example for lidtk ? 我找到了pytest-benchmark ,但是我将如何针对例如lidtk呢?

In pseudo-code, I want to do the following: 用伪代码,我要执行以下操作:

def classifier_predict(input_features):
    # do something smart, but maybe too time-consuming
    return result

def input_generator():
    # Generate something random which classifier_predict
    # can work on - don't measure this time!
    return input_features

class Agents(unittest.TestCase):
    def test_classifier_predict():
        self.assertMaxTime(classifier_predict,
                           input_generator,
                           max_time_in_ms=100)

Handcraft-solution 手工的解决方案

Here is the pseudo-code of a rather hand-crafted solution: 这是一个手工制作的解决方案的伪代码:

def classifier_predict(input_features):
    # do something smart, but maybe too time-consuming
    return result

def input_generator():
    # Generate something random which classifier_predict
    # can work on - don't measure this time!
    return input_features

class Agents(unittest.TestCase):
    def test_classifier_predict():
        nb_tests = 1000
        total_time = 0.0
        for _ in range(nb_tests):
            input_ = input_generator()
            t0 = time.time()
            classifier_predict(input_)
            t1 = time.time()
            total_time += t1 - t0
        self.assertLessEqual(total_time / nb_tests, 100)

Drawbacks 缺点

  • No nice graphs (like pytest-benchmark seems to generate) 没有好的图形(例如pytest-benchmark似乎会生成)
  • In general, hard limits might be difficult due to different hardware and also due to different external work load 通常,由于硬件不同以及外部工作负荷不同,硬限制可能很困难

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM