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

Add _measure_time time function #32

Merged

Conversation

reahaas
Copy link
Contributor

@reahaas reahaas commented Oct 5, 2020

No description provided.

@@ -32,6 +32,13 @@ def _deserialize(data, cls):
return cls(**data)


def _measure_time(func, argument):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A function that get the function to execute and an argument, call the function with the given argument and return the time of executions.

Copy link
Contributor

Choose a reason for hiding this comment

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

make it a contextmanager - it will be much easier to use

two alternative implementations:

from typing import Optional


class MeasureTime:
    def __init__(self):
        self.start = None
        self.stop = None

    @staticmethod
    def time() -> float:
        return time.time()

    @property
    def elapsed(self) -> Optional[float]:
        if self.start is None:
            return None
        return (self.stop or self.time()) - self.start

    def __enter__(self):
        self.start = self.time()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop = self.time()

mt = MeasureTime()
with mt:
    func(args)
print(f"func call lasted {mt.elapsed}s")
import contextlib

@contextlib.contextmanager
def measure_time():
    start = time.time()
    def elapsed():
        return time.time() - start

    yield elapsed

with measure_time() as elapsed:
    func(args)
    print(f"func call lasted {elapsed()}s")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks,
That great, I chose the second implementation you proposed, it nicer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After some struggling with the second approach, moved to the first one (define a class).

Comment on lines 133 to 134
exec_time = _measure_time(migration.upgrade, self.db)
self.logger.info("Execution time of %r: %s", migration.name, exec_time)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Log the execution time.

Comment on lines 159 to 160
exec_time = _measure_time(migration.downgrade, self.db)
self.logger.info("Execution time of %r: %s", migration.name, exec_time
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Log the execution time.

Fix missing parentheses
Copy link
Contributor

@rooterkyberian rooterkyberian left a comment

Choose a reason for hiding this comment

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

please make sure make check passes on your machine
from what I have seen the automated formatter of the code was not run

@@ -32,6 +32,13 @@ def _deserialize(data, cls):
return cls(**data)


def _measure_time(func, argument):
Copy link
Contributor

Choose a reason for hiding this comment

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

make it a contextmanager - it will be much easier to use

two alternative implementations:

from typing import Optional


class MeasureTime:
    def __init__(self):
        self.start = None
        self.stop = None

    @staticmethod
    def time() -> float:
        return time.time()

    @property
    def elapsed(self) -> Optional[float]:
        if self.start is None:
            return None
        return (self.stop or self.time()) - self.start

    def __enter__(self):
        self.start = self.time()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop = self.time()

mt = MeasureTime()
with mt:
    func(args)
print(f"func call lasted {mt.elapsed}s")
import contextlib

@contextlib.contextmanager
def measure_time():
    start = time.time()
    def elapsed():
        return time.time() - start

    yield elapsed

with measure_time() as elapsed:
    func(args)
    print(f"func call lasted {elapsed()}s")

src/pymongo_migrate/mongo_migrate.py Outdated Show resolved Hide resolved
src/pymongo_migrate/mongo_migrate.py Outdated Show resolved Hide resolved
Change the `measure_time` function to work as a contextmanager.
migration.upgrade(self.db)
with measure_time as elapsed:
migration.upgrade(self.db)
self.logger.info(f"Execution time of {migration.name}: {elapsed()}s")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used here an f-string to pass content to the logger, although the rest of the file usage %r style. Is it ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

know I think about it - no, please use %-format style

my reason for sticking with %-format style for logging is the way tools like sentry handle error log aggregation - as it integrates with logging on Python level it can easily aggregate stuff using the same format string. If you use f"" formatting each message looks like a different one to it and it cannot aggregate.

So yea, %-format is not as nice, but preventing duplicates in Sentry is much bigger concern to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

src/pymongo_migrate/mongo_migrate.py Outdated Show resolved Hide resolved
@rooterkyberian
Copy link
Contributor

love it
I'm going to squash commits to have a cleaner history or you can do it yourself if you want. I will merge this PR after I'm done with my work.

btw https://hacktoberfest.digitalocean.com/ is on and you are off to a good start ;)

@reahaas
Copy link
Contributor Author

reahaas commented Oct 6, 2020

you are off to a good start

Thanks, I just joined the challenge 🥇

@rooterkyberian rooterkyberian merged commit 7f5bd91 into stxnext:master Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants