the-events-calendar / the-events-calendar-category-colors

Plugin add-on for The Events Calendar to colorize categories
https://wordpress.org/plugins/the-events-calendar-category-colors
45 stars 19 forks source link

Plugin causes performance issues #152

Open marcingminski opened 1 year ago

marcingminski commented 1 year ago

Plugin constantly triggers the PHP slow log:

[03-Sep-2023 12:52:19]  [pool www] pid 16838
script_filename = /var/www/html/wordpress/index.php
[0x00007fa1b3813cf0] glob() /var/www/html/wordpress/wp-content/plugins/the-events-calendar-category-colors/src/Category_Colors/Frontend.php:189
[0x00007fa1b3813c00] generate_css() /var/www/html/wordpress/wp-content/plugins/the-events-calendar-category-colors/src/Category_Colors/Frontend.php:159

The line 189 being:

foreach ( glob( "{$css_dir}/teccc*.css" ) as $file ) {

line 189 looks like its part of a function that should have been removed:

        public function generate_css() {
                // TODO: remove after a couple of updates.
                $css_dir = apply_filters( 'teccc_uploads_dir', wp_upload_dir()['basedir'] );
                $css_dir = untrailingslashit( $css_dir );
                foreach ( glob( "{$css_dir}/teccc*.css" ) as $file ) {
                        if ( file_exists( $file ) ) {
                                unlink( $file );
                        }
                }

and line 159 being:

wp_add_inline_style( 'teccc-nofile-stylesheet', $this->generate_css() );

I understand this is because the plugin creates inline CSS every time the page loads. Is there a way to generate static CSS in the file and embed it in HEAD?

afragen commented 1 year ago

I've gone back and forth with creating inline vs static CSS and the upside is inline CSS is less problematic on a wider variety of servers, including WordPress VIP hosts.

marcingminski commented 1 year ago

How about having both options so people can chose what works best for them?

afragen commented 1 year ago

FWIW generate_css() is cached for about 4 weeks. It shouldn't cause any slowdown after generated.

marcingminski commented 1 year ago

Thanks. I get lots of these in the php slow log. Perhaps they aren’t being cached then? Any way to debug this?

afragen commented 1 year ago

I'm not familiar with a PHP Slow log. Where is it generated from?

marcingminski commented 1 year ago

PHP itself: request_slowlog_timeout https://www.php.net/manual/en/install.fpm.configuration.php

afragen commented 1 year ago

When you say slow, what are the actual times running the function? once, ten times, etc.

kadamwhite commented 1 year ago

It's a separate issue but I'm also observing that the recent changes to this plugin are causing an update_option call on every page view, which shouldn't be necessary -- I'm looking at this line: https://github.com/the-events-calendar/the-events-calendar-category-colors/blob/develop/src/Category_Colors/Main.php#L247

(get_category_terms is called by load_categories, which is hooked to init, which runs every time WP spins up to serve a request)

It's not a significant performance impact on its own, but I'd think we shouldn't have to update any content on a normal frontend page view.

@afragen I can make a new issue for this if you'd prefer to keep this isolated to the originally described issue

afragen commented 1 year ago

Might be best to create a new issue.

kadamwhite commented 1 year ago

@afragen Great, moved that piece to https://github.com/the-events-calendar/the-events-calendar-category-colors/issues/153

marcingminski commented 1 year ago

When you say slow, what are the actual times running the function? once, ten times, etc.

sorry missed this - constantly over 2 seconds execution time. I commented out the above lines and the issue (mostly) went away.