Skip to content

fix(perf): Improve PDF generation performance - #112

Open
MyvTsv wants to merge 1 commit into
11.0/bugfixesfrom
perf_optimization
Open

MyvTsv wants to merge 1 commit into
11.0/bugfixesfrom
perf_optimization

Conversation

@MyvTsv

@MyvTsv MyvTsv commented Nov 16, 2026

Copy link
Copy Markdown
  • I have performed a self-review of my code.
  • I have added tests (when available) that prove my fix is effective or that my feature works.
  • I have updated the CHANGELOG with a short functional description of the fix or new feature.
  • This change requires a documentation update.

Description

Performance pass on the pdf plugin, measured on a seeded e2e database (2,000 tickets, 2,000 computers, 20,000 followups, 10,000 tasks, 50,000 installed software versions, 10,000 network ports, 100,000 history lines). Hot ticket with 500 followups, 250 tasks, 50 costs and 50 validations; hot computer with 1,250 installed software versions, 100 license assignments and 250 network ports.

Scenario Before After
Ticket PDF generation, all 23 sections (83 pages): SQL queries 2,526 102 (-96 %)
Ticket PDF generation: server time 1,811 ms 892 ms (-51 %)
Computer PDF generation, all 24 sections (156 pages): SQL queries 3,462 1,961 (-43 %)
Computer PDF generation: server time 3,001 ms 2,058 ms (-31 %)
Concurrent request during the computer export: session lock wait 2,939 ms 1 ms
Queries added by the plugin on every HTTP request 6 4 (core plugin loading only)

SQL time of the export: ticket 154 → 32 ms, computer 415 → 104 ms. Fully loaded: ticket export 2,550 → 1,580 ms, computer export 3,520 → 2,565 ms; both gains are significant over 5 runs. On the item forms and the "PDF export" tab, the plugin cost was already within the noise and stays so.

Changes

  • Row rendering (PluginPdfSimplePDF::displayInternal()): each row used to be rendered twice by TCPDF, a dry run in a transaction to compute its height and then the real write. That accounted for 85 to 95 % of the export CPU. One-column rows are now written once. In multi-column rows, cells holding one line of plain text are measured with getStringHeight(), and only the other cells (markup, wrapping, empty) still go through the dry run.
  • Session: front/export.php and front/export.massive.php call Session::writeClose() once the rights are checked, just before the generation. A long or massive export no longer freezes the user's other tabs.
  • Ticket sections (followups, tasks, validations, solutions, costs of tickets, problems and changes): user names and dropdown names (request types, groups, task categories) are memoized for the request through PluginPdfCommon::getUserNameCached() and getDropdownNameCached(). Before, the export looked them up once per row: 1,259 user queries for 25 distinct users.
  • PluginPdfConfig::formatNumber(): the NumberFormatter is built once per user and request. It no longer reads the language configuration and the user for each amount. getDevices() reuses the global $PDF_DEVICES.
  • Installed software (PluginPdfItem_SoftwareVersion): all licenses of the item are read in one query and grouped by version, instead of one query per installed version (1,250 → 1).
  • Network ports (PluginPdfNetworkPort): the port query also selects instantiation_type and mac, so each port is no longer re-read with getFromDB() (-250 queries).
  • Init: Plugin::isActivated('datainjection' / 'geststock') queried glpi_plugins on every request, because plugins are not flagged as initialized yet in plugin_init_pdf. The check moved to post_init and uses Plugin::getPlugins(), the list built by core at boot.

No schema change.

Notes

  • Output: the ticket and computer PDFs were compared word by word (pdftotext -bbox) before and after: same pages, same words, same horizontal positions. One layout difference: a multi-column row that starts a page is now printed at the top margin, about 5 pt higher than before. The old offset was an artefact of the dry run.
  • In debug mode, Session::writeClose() keeps the session open, so the export still holds the session lock there.

Tests

  • Ticket and computer exports with all sections selected, on the hot items: content identical to the exports before the change (see Notes).
  • Mass export (front/export.massive.php) generates the PDF with the session released.
  • PHPUnit suite green (50 tests, 1,341 assertions); parallel-lint, PHPStan, Rector and php-cs-fixer clean.

@MyvTsv MyvTsv self-assigned this Nov 16, 2026
@MyvTsv
MyvTsv marked this pull request as ready for review November 16, 2026 13:12
@Rom1-B Rom1-B closed this Nov 18, 2026
@Rom1-B Rom1-B reopened this Nov 18, 2026
@Rom1-B
Rom1-B requested review from a team, Herafia, Mary-Clb and RomainLvr and removed request for a team November 18, 2026 14:36
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