Best Python code snippet using pytest-benchmark
test_sampling.py
Source:test_sampling.py
1# ==================================================================================================2# Copyright 2013 Twitter, Inc.3# --------------------------------------------------------------------------------------------------4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this work except in compliance with the License.6# You may obtain a copy of the License in the LICENSE file, or at:7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15# ==================================================================================================16import os17import pytest18from twitter.common.contextutil import temporary_file19from twitter.common.metrics import Label20from twitter.common.metrics.metrics import Metrics21from twitter.common.metrics.sampler import (22 MetricSampler,23 SamplerBase,24 DiskMetricWriter,25 DiskMetricReader)26from twitter.common.quantity import Amount, Time27from twitter.common.testing.clock import ThreadedClock28def test_sampler_base():29 class TestSampler(SamplerBase):30 def __init__(self, period, clock):31 self.count = 032 SamplerBase.__init__(self, period, clock)33 def iterate(self):34 self.count += 135 test_clock = ThreadedClock()36 sampler = TestSampler(Amount(1, Time.SECONDS), clock=test_clock)37 sampler.start()38 assert test_clock.converge(threads=[sampler])39 test_clock.assert_waiting(sampler, 1)40 test_clock.tick(0.5)41 assert test_clock.converge(threads=[sampler])42 assert sampler.count == 043 test_clock.tick(0.5)44 assert test_clock.converge(threads=[sampler])45 assert sampler.count == 146 test_clock.tick(5)47 assert test_clock.converge(threads=[sampler])48 assert sampler.count == 649 assert not sampler.is_stopped()50 sampler.stop()51 # make sure that stopping the sampler short circuits any sampling52 test_clock.tick(5)53 assert test_clock.converge(threads=[sampler])54 assert sampler.count == 655def test_metric_read_write():56 metrics = Metrics()57 with temporary_file() as fp:58 os.unlink(fp.name)59 writer = DiskMetricWriter(metrics, fp.name)60 reader = DiskMetricReader(fp.name)61 assert reader.sample() == {}62 reader.iterate()63 assert reader.sample() == {}64 writer.iterate()65 assert reader.sample() == {}66 reader.iterate()67 assert reader.sample() == {}68 metrics.register(Label('herp', 'derp'))69 writer.iterate()70 assert reader.sample() == {}71 reader.iterate()72 assert reader.sample() == {'herp': 'derp'}73def test_metric_sample():74 metrics = Metrics()75 sampler = MetricSampler(metrics)76 assert sampler.sample() == {}77 sampler.iterate()78 assert sampler.sample() == {}79 metrics.register(Label('herp', 'derp'))80 assert sampler.sample() == {}81 sampler.iterate()...
test_clock_model.py
Source:test_clock_model.py
1import pytest2from clockzy.lib.models.clock import Clock3from clockzy.lib.db.database_interface import item_exists4from clockzy.lib.test_framework.database import intratime_user_parameters, clock_parameters5from clockzy.lib.db.db_schema import CLOCK_TABLE6from clockzy.lib.handlers.codes import SUCCESS, ITEM_ALREADY_EXISTS7# Pytest tierdown fixture is executed from righ to left position8@pytest.mark.parametrize('user_parameters, clock_parameters', [(intratime_user_parameters, clock_parameters)])9def test_save_clock(add_pre_user, delete_post_user, delete_post_clock):10 test_clock = Clock(**clock_parameters)11 # Add the clock and check that 1 row has been affected (no exception when running)12 assert test_clock.save() == SUCCESS13 clock_parameters['id'] = test_clock.id14 # If we try to add the same CLOCK, check that it can not be inserted15 assert test_clock.save() == ITEM_ALREADY_EXISTS16 # Query and check that the clock exist17 assert item_exists({'id': test_clock.id}, CLOCK_TABLE)18@pytest.mark.parametrize('user_parameters, clock_parameters', [(intratime_user_parameters, clock_parameters)])19def test_update_clock(add_pre_user, add_pre_clock, delete_post_user, delete_post_clock):20 test_clock = Clock(**clock_parameters)21 test_clock.id = add_pre_clock22 clock_parameters['id'] = test_clock.id23 # Update the clock info and check that 1 row has been affected (no exception when running)24 test_clock.action = 'OUT'25 assert test_clock.update() == SUCCESS26 # Query and check that the clock exist27 assert item_exists({'id': test_clock.id, 'action': 'OUT'}, CLOCK_TABLE)28@pytest.mark.parametrize('user_parameters, clock_parameters', [(intratime_user_parameters, clock_parameters)])29def test_delete_clock(add_pre_user, add_pre_clock, delete_post_user):30 test_clock = Clock(**clock_parameters)31 test_clock.id = add_pre_clock32 clock_parameters['id'] = test_clock.id33 # Delete the clock info and check that 1 row has been affected (no exception when running)34 assert test_clock.delete() == SUCCESS35 # Query and check that the clock does not exist...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!