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
13 changes: 11 additions & 2 deletions src/pymongo_migrate/mongo_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).

rooterkyberian marked this conversation as resolved.
Show resolved Hide resolved
start = time.clock()
reahaas marked this conversation as resolved.
Show resolved Hide resolved
func(argument)
elapsed = time.clock() - start
return elapsed


@dataclass
class MongoMigrate:
client: pymongo.MongoClient
Expand Down Expand Up @@ -123,7 +130,8 @@ def upgrade(self, migration_name: Optional[str] = None):
)
continue
self.logger.info("Running upgrade migration %r", migration.name)
migration.upgrade(self.db)
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.

migration_state.applied = dt()
self.set_state(migration_state)
if migration.name == migration_name:
Expand All @@ -148,7 +156,8 @@ def downgrade(self, migration_name: Optional[str]):
)
continue
self.logger.info("Running downgrade migration %r", migration.name)
migration.downgrade(self.db)
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.

migration_state.applied = None
self.set_state(migration_state)

Expand Down