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

Improved the Timer and tagged it as API #7760

Closed
wants to merge 3 commits into from
Closed

Conversation

mnapoli
Copy link
Contributor

@mnapoli mnapoli commented Apr 24, 2015

While working on #7569 (removing calls to non-api stuff) I saw that the Timer was used in some plugins but it's not tagged as @api.

I tagged the Timer as API but before that I cleaned it up a bit:

  • moving it to a Piwik\Timer namespace: it avoids cluttering the root Piwik\ namespace with stuff unrelated to Piwik itself, and it allows to move it to an external component later without any bc break
  • simplifying the API a little bit based on how it was used: a method wasn't used, others where returning formatted number but used as int (e.g. used in math operation), etc.
  • I added unit tests

By the way I now realize that git didn't pick up the "move" operation and instead deleted/recreated the file, that wasn't planned sometimes git can be a bit difficult about that :/

PR targeted at 2.14, don't merge in 2.13?

@mnapoli mnapoli added the Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. label Apr 24, 2015
@mnapoli mnapoli added this to the Piwik 2.14.0 milestone Apr 24, 2015
@mnapoli mnapoli added the Needs Review PRs that need a code review label Apr 24, 2015
@tsteur
Copy link
Member

tsteur commented Apr 30, 2015

In which plugins is the Timer API used? From the sound it doesn't seem like needed as an API?

@mattab
Copy link
Member

mattab commented May 1, 2015

@tsteur FYI you can see the inspection output here http://builds-artifacts.piwik.org/protected/inspections.all/2015-04-04/inspections/all_inspections.out - it's used in VisitorGenerator

@tsteur
Copy link
Member

tsteur commented May 1, 2015

Maybe same applies here as in #7790 (comment)

If possible, I would like to avoid making this a public API (to keep the list of available APIs "short") but if needed I'm ok with it.

@mnapoli
Copy link
Contributor Author

mnapoli commented May 1, 2015

The idea is that this isn't a Piwik API, it's a component. Like the Symfony Stopwatch has nothing to do with the framework, the Timer has nothing to do with Piwik. I've considered replacing it with the Stopwatch component but it wasn't worth it because we use things that aren't available, and the Stopwatch is more complex. I have also searched packagist and considered all other alternatives, but not worth it imo.

That's why I moved it in another namespace: later we can move it into a separate repo. Hope that clears things up.

@tsteur
Copy link
Member

tsteur commented May 1, 2015

It doesn't really make a difference for me whether it is a component or not... What I meant is marking it as @api which means we have to support it and it will appear on developer.piwik.org. I'm afraid we're about to mark too many classes as API (component or not) that are not really needed just because they were used in some internal plugins.

@diosmosis
Copy link
Member

@tsteur @mnapoli How would you feel about adding an @api method to ConsoleCommand like this:

protected function performTimedOperation($successMessage, $callback)

? The method would use Timer internally, and print out the success message w/ the elapsed time. This way, we provide an easy way for commands to spit out the time (which seems a common need for long running commands) w/o exposing the Timer class.

@tsteur
Copy link
Member

tsteur commented May 25, 2015

I'm not sure, it probably depends on the use case.

If someone wants to know the time the command took, one could simply use the Linux command time eg time ./console. Something like this is usually good practice and we should not really reimplement native system features. We could otherwise, if time is not acceptable, maybe rather add an option that is available to all commands and will print the time instead of having that new method. This way it would be available to all commands without having to do anything.

If we want to print it always after command execution for specific commands then such a method could be helpful. Although I'm not sure if I'd prefer to use Timer directly then as we'd otherwise end up adding such methods everywhere (but I reckon it would maybe not happen in this case as we usually don't have long running jobs elsewhere). Do we have an issue where a user / developer asks for seeing the time the command took? If not, I would probably not add it. If yes, I'd be ok with it but don't feel really good about it. Also I'm not a fan of callbacks. I'd try to solve it differently. Eg always save the start time before executing and afterwards, if performTimedOperation is called, calculate the difference.

@diosmosis
Copy link
Member

If someone wants to know the time the command took, one could simply use the Linux command time eg time ./console.

This doesn't help if you're timing individual steps, such as optimizing multiple tables (which allows users to see progress + get an idea of how long the next step will take).

Eg always save the start time before executing and afterwards, if performTimedOperation is called, calculate the difference.

I have no idea what you mean by this, but how would it be easier than using a callback?

@tsteur
Copy link
Member

tsteur commented May 25, 2015

This doesn't help if you're timing individual steps, such as optimizing multiple tables (which allows users to see progress + get an idea of how long the next step will take).

That's why I asked for the use case. I don't see this in the Command class. I'd actually rather make the Timer API public if it is actually needed in a third party plugin (if needed in core plugin then we don't have to expose it). Even better would be maybe to develop (probably such a helper already exists somewhere) a Symfony Console helper but I think it would be hard to document/expose.

I have no idea what you mean by this, but how would it be easier than using a callback?

I don't see a need for a callback here. It makes it rather more complicated to understand, to debug and profile etc. It would be easier to not have a callback since you'd simply write a command as usual:

protected function execute(InputInterface $input, OutputInterface $output)
{
    Filesystem::deleteAllCacheOnUpdate();

    $this->printTimingInformation($output);
}

As you want to time different operations as you just said this would not help you there.

I would personally either use the Timer class directly or add a helper for Symfony Console as the use case seems to me to specific for adding it to Command directly

@diosmosis
Copy link
Member

A helper could be exposed via @api if the helper type has to be referenced by code that uses it. I don't know if there are existing add-ons for this. Honestly, it seems too small a use case to bother with another library.

@mnapoli
Copy link
Contributor Author

mnapoli commented May 26, 2015

I'd rather remove usage of such timer class to replace it with manually using microtime() instead of adding unnecessary complexity. A stopwatch component is very basic and generic need, if we don't want to provide a very simple and basic component like this I don't think it's a good idea to expose and maintain a very restricted helper that applies only to a very specific use case (we'll later be adding the same thing for web, and then for logging, etc etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants