-
Notifications
You must be signed in to change notification settings - Fork 479
Throughput sampler #15
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
806248a
Implement an initial ThroughputSampler
LotharSee b9c7fcb
Mock time.time for our sampler tests
LotharSee 7bea5ad
Add long test for Throughput sampler, fix thanks to it
LotharSee 633f229
Add more throughput sampler tests
LotharSee d182979
Upgrade setuptools for tests to fix Python 3.4 CircleCI
LotharSee ae071a1
Speedup CI by not compiling Cassandra extensions
LotharSee 0cc4cbf
Remove a xrange
LotharSee a61b16e
Simplify Tracer.sampler interface, cleanup code
LotharSee 7918a85
Fix throughtput sampler tests to be reliable
LotharSee 973ae1e
Used fixed-size buffer from Throughput sampler
LotharSee 12f91b9
Make ThroughtputSampler threadsafe
LotharSee 1916877
Make properly ThroughtputSampler threadsafe
LotharSee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| from __future__ import division | ||
|
|
||
| import unittest | ||
| import random | ||
| import time | ||
| import threading | ||
|
|
||
| from ddtrace.tracer import Tracer | ||
| from ddtrace.sampler import RateSampler, ThroughputSampler | ||
| from .test_tracer import DummyWriter | ||
| from .util import patch_time | ||
|
|
||
|
|
||
| class RateSamplerTest(unittest.TestCase): | ||
|
|
||
| def test_random_sequence(self): | ||
| writer = DummyWriter() | ||
| sampler = RateSampler(0.5) | ||
| tracer = Tracer(writer=writer, sampler=sampler) | ||
|
|
||
| # Set the seed so that the choice of sampled traces is deterministic, then write tests accordingly | ||
| random.seed(4012) | ||
|
|
||
| # First trace, sampled | ||
| with tracer.trace("foo") as s: | ||
| assert s.sampled | ||
| assert s.weight == 2 | ||
| assert writer.pop() | ||
|
|
||
| # Second trace, not sampled | ||
| with tracer.trace("figh") as s: | ||
| assert not s.sampled | ||
| s2 = tracer.trace("what") | ||
| assert not s2.sampled | ||
| s2.finish() | ||
| with tracer.trace("ever") as s3: | ||
| assert not s3.sampled | ||
| s4 = tracer.trace("!") | ||
| assert not s4.sampled | ||
| s4.finish() | ||
| spans = writer.pop() | ||
| assert not spans, spans | ||
|
|
||
| # Third trace, not sampled | ||
| with tracer.trace("ters") as s: | ||
| assert s.sampled | ||
| assert writer.pop() | ||
|
|
||
|
|
||
| class ThroughputSamplerTest(unittest.TestCase): | ||
| """Test suite for the ThroughputSampler""" | ||
|
|
||
| def test_simple_limit(self): | ||
| writer = DummyWriter() | ||
| tracer = Tracer(writer=writer) | ||
|
|
||
| with patch_time() as fake_time: | ||
| tps = 5 | ||
| tracer.sampler = ThroughputSampler(tps) | ||
|
|
||
| for _ in range(10): | ||
| s = tracer.trace("whatever") | ||
| s.finish() | ||
| traces = writer.pop() | ||
|
|
||
| got = len(traces) | ||
| expected = 10 | ||
|
|
||
| assert got == expected, \ | ||
| "Wrong number of traces sampled, %s instead of %s" % (got, expected) | ||
|
|
||
| # Wait enough to reset | ||
| fake_time.sleep(tracer.sampler.BUFFER_DURATION + 1) | ||
|
|
||
| for _ in range(100): | ||
| s = tracer.trace("whatever") | ||
| s.finish() | ||
| traces = writer.pop() | ||
|
|
||
| got = len(traces) | ||
| expected = tps * tracer.sampler.BUFFER_DURATION | ||
|
|
||
| assert got == expected, \ | ||
| "Wrong number of traces sampled, %s instead of %s" % (got, expected) | ||
|
|
||
| def test_long_run(self): | ||
| writer = DummyWriter() | ||
| tracer = Tracer(writer=writer) | ||
|
|
||
| # Test a big matrix of combinaisons | ||
| # Ensure to have total_time >> BUFFER_DURATION to reduce edge effects | ||
| for tps in [10, 23, 15, 31]: | ||
| for (traces_per_s, total_time) in [(80, 23), (75, 66), (1000, 77)]: | ||
|
|
||
| with patch_time() as fake_time: | ||
| # We do tons of operations in this test, do not let the time slowly shift | ||
| fake_time.set_delta(0) | ||
|
|
||
| tracer.sampler = ThroughputSampler(tps) | ||
|
|
||
| for _ in range(total_time): | ||
| for _ in range(traces_per_s): | ||
| s = tracer.trace("whatever") | ||
| s.finish() | ||
| fake_time.sleep(1) | ||
|
|
||
| traces = writer.pop() | ||
| # The current sampler implementation can introduce an error of up to | ||
| # `tps * BUFFER_DURATION` traces at initialization (since the sampler starts empty) | ||
| got = len(traces) | ||
| expected = tps * total_time | ||
| error_delta = tps * tracer.sampler.BUFFER_DURATION | ||
|
|
||
| assert abs(got - expected) <= error_delta, \ | ||
| "Wrong number of traces sampled, %s instead of %s (error_delta > %s)" % (got, expected, error_delta) | ||
|
|
||
|
|
||
| def test_concurrency(self): | ||
| # Test that the sampler works well when used in different threads | ||
| writer = DummyWriter() | ||
| tracer = Tracer(writer=writer) | ||
|
|
||
| total_time = 10 | ||
| concurrency = 100 | ||
| end_time = time.time() + total_time | ||
|
|
||
| # Let's sample to a multiple of BUFFER_SIZE, so that we can pre-populate the buffer | ||
| tps = 15 * ThroughputSampler.BUFFER_SIZE | ||
| tracer.sampler = ThroughputSampler(tps) | ||
|
|
||
| threads = [] | ||
|
|
||
| def run_simulation(tracer, end_time): | ||
| while time.time() < end_time: | ||
| s = tracer.trace("whatever") | ||
| s.finish() | ||
| # ~1000 traces per s per thread | ||
| time.sleep(0.001) | ||
|
|
||
| for i in range(concurrency): | ||
| thread = threading.Thread(target=run_simulation, args=(tracer, end_time)) | ||
| threads.append(thread) | ||
|
|
||
| for t in threads: | ||
| t.start() | ||
|
|
||
| for t in threads: | ||
| t.join() | ||
|
|
||
| traces = writer.pop() | ||
|
|
||
| got = len(traces) | ||
| expected = tps * total_time | ||
| error_delta = tps * ThroughputSampler.BUFFER_DURATION | ||
|
|
||
| assert abs(got - expected) <= error_delta, \ | ||
| "Wrong number of traces sampled, %s instead of %s (error_delta > %s)" % (got, expected, error_delta) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this make it fast? if so ...<3