Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion samples/Chart/32_Chart_read_write.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
$inputFileType = 'Xlsx';
$inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx';

if ((isset($argc)) && ($argc > 1)) {
$inputFileNames = [];
for ($i = 1; $i < $argc; ++$i) {
Expand Down
1 change: 0 additions & 1 deletion samples/Chart/32_Chart_read_write_HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

$inputFileType = 'Xlsx';
$inputFileNames = __DIR__ . '/../templates/36write*.xlsx';

if ((isset($argc)) && ($argc > 1)) {
$inputFileNames = [];
for ($i = 1; $i < $argc; ++$i) {
Expand Down
1 change: 0 additions & 1 deletion samples/Chart/32_Chart_read_write_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

$inputFileType = 'Xlsx';
$inputFileNames = __DIR__ . '/../templates/36write*.xlsx';

if ((isset($argc)) && ($argc > 1)) {
$inputFileNames = [];
for ($i = 1; $i < $argc; ++$i) {
Expand Down
1 change: 0 additions & 1 deletion samples/Chart/35_Chart_render.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
$inputFileType = 'Xlsx';
$inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx';
//$inputFileNames = __DIR__ . '/../templates/32readwriteStockChart5.xlsx';

if ((isset($argc)) && ($argc > 1)) {
$inputFileNames = [];
for ($i = 1; $i < $argc; ++$i) {
Expand Down
1 change: 0 additions & 1 deletion samples/Chart/35_Chart_render33.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

$inputFileType = 'Xlsx';
$inputFileNamesString = $helper->getTemporaryFolder() . '/33_Chart_create_*.xlsx';

if ((isset($argc)) && ($argc > 1)) {
$inputFileNames = [];
for ($i = 1; $i < $argc; ++$i) {
Expand Down
88 changes: 88 additions & 0 deletions samples/ParallelBenchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* Benchmark: Sequential vs Parallel Xlsx Writing.
*
* Usage:
* php samples/ParallelBenchmark.php [sheets] [rows]
*
* Examples:
* php samples/ParallelBenchmark.php # 5 sheets, 5000 rows each
* php samples/ParallelBenchmark.php 10 10000 # 10 sheets, 10000 rows each
*/

use PhpOffice\PhpSpreadsheet\Parallel\Backend\PcntlBackend;
use PhpOffice\PhpSpreadsheet\Parallel\CpuDetector;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

require __DIR__ . '/../vendor/autoload.php';

$sheetCount = (int) ($argv[1] ?? 5);
$rowCount = (int) ($argv[2] ?? 5000);
$colCount = 10;

echo "=== Parallel Xlsx Writing Benchmark ===\n";
echo "Sheets: {$sheetCount}, Rows/sheet: {$rowCount}, Cols/sheet: {$colCount}\n";
echo 'CPU cores detected: ' . CpuDetector::detectCpuCount() . "\n";
echo 'pcntl available: ' . (PcntlBackend::isAvailable() ? 'yes' : 'no') . "\n";
echo 'Total cells: ' . ($sheetCount * $rowCount * $colCount) . "\n\n";

// Build spreadsheet
echo "Building spreadsheet...\n";
$buildStart = microtime(true);
$spreadsheet = new Spreadsheet();

for ($s = 0; $s < $sheetCount; ++$s) {
$sheet = ($s === 0) ? $spreadsheet->getActiveSheet() : $spreadsheet->createSheet();
$sheet->setTitle("Sheet{$s}");

for ($row = 1; $row <= $rowCount; ++$row) {
for ($col = 1; $col <= $colCount; ++$col) {
$sheet->setCellValue([$col, $row], "S{$s}R{$row}C{$col}");
}
}
}
$buildTime = microtime(true) - $buildStart;
$peakAfterBuild = memory_get_peak_usage(true);
echo sprintf("Build time: %.3fs, Peak memory: %.1fMB\n\n", $buildTime, $peakAfterBuild / 1048576);

// Sequential write
echo "--- Sequential write ---\n";
$tempSeq = tempnam(sys_get_temp_dir(), 'bench_seq_');
$seqStart = microtime(true);
$writer = new Xlsx($spreadsheet);
$writer->save($tempSeq);
$seqTime = microtime(true) - $seqStart;
$seqSize = filesize($tempSeq);
echo sprintf("Time: %.3fs, File size: %.1fKB\n", $seqTime, ($seqSize ?: 0) / 1024);
@unlink($tempSeq);

// Parallel write
if (PcntlBackend::isAvailable()) {
echo "\n--- Parallel write ---\n";

$tempPar = tempnam(sys_get_temp_dir(), 'bench_par_');
$parStart = microtime(true);
$writer2 = new Xlsx($spreadsheet);
$writer2->setParallelEnabled(true);
// $writer2->setMaxWorkers(4); // Optional: override auto-detect
$writer2->save($tempPar);
$parTime = microtime(true) - $parStart;
$parSize = filesize($tempPar);
echo sprintf("Time: %.3fs, File size: %.1fKB\n", $parTime, ($parSize ?: 0) / 1024);
@unlink($tempPar);

echo "\n--- Results ---\n";
$speedup = $seqTime / $parTime;
$saved = (1 - ($parTime / $seqTime)) * 100;
echo sprintf("Sequential: %.3fs\n", $seqTime);
echo sprintf("Parallel: %.3fs\n", $parTime);
echo sprintf("Speedup: %.2fx (%.1f%% faster)\n", $speedup, $saved);
} else {
echo "\npcntl not available — parallel benchmark skipped.\n";
}

echo sprintf("\nPeak memory: %.1fMB\n", memory_get_peak_usage(true) / 1048576);

$spreadsheet->disconnectWorksheets();
80 changes: 80 additions & 0 deletions samples/ParallelProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Profile where time is spent in Xlsx::save() to identify real bottlenecks.
*/

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

require __DIR__ . '/../vendor/autoload.php';

$sheetCount = (int) ($argv[1] ?? 5);
$rowCount = (int) ($argv[2] ?? 5000);
$colCount = 10;

echo "Building {$sheetCount} sheets x {$rowCount} rows x {$colCount} cols...\n";
$spreadsheet = new Spreadsheet();
for ($s = 0; $s < $sheetCount; ++$s) {
$sheet = ($s === 0) ? $spreadsheet->getActiveSheet() : $spreadsheet->createSheet();
$sheet->setTitle("Sheet{$s}");
for ($row = 1; $row <= $rowCount; ++$row) {
for ($col = 1; $col <= $colCount; ++$col) {
$sheet->setCellValue([$col, $row], "S{$s}R{$row}C{$col}");
}
}
}
echo "Done building.\n\n";

// Time individual components by using reflection/hooks
$writer = new Xlsx($spreadsheet);

// Time createStringTable
$t = microtime(true);
$ref = new ReflectionMethod($writer, 'createStringTable');
$ref->setAccessible(true);
$ref->invoke($writer);
$stringTableTime = microtime(true) - $t;

// Time createStyleDictionaries
$t = microtime(true);
$ref2 = new ReflectionMethod($writer, 'createStyleDictionaries');
$ref2->setAccessible(true);
$ref2->invoke($writer);
$styleTime = microtime(true) - $t;

// Time per-sheet writeWorksheet
$stringTableProp = new ReflectionProperty($writer, 'stringTable');
$stringTableProp->setAccessible(true);
$stringTable = $stringTableProp->getValue($writer);

$worksheetWriter = $writer->getWriterPartWorksheet();
$sheetTimes = [];
for ($i = 0; $i < $sheetCount; ++$i) {
$t = microtime(true);
/** @var array<string> $stringTable */
$xml = $worksheetWriter->writeWorksheet($spreadsheet->getSheet($i), $stringTable, false);
$sheetTimes[$i] = microtime(true) - $t;
}
$totalSheetTime = array_sum($sheetTimes);

// Time full save
$tempFile = tempnam(sys_get_temp_dir(), 'profile_');
$writer2 = new Xlsx($spreadsheet);
$t = microtime(true);
$writer2->save($tempFile);
$totalSaveTime = microtime(true) - $t;
@unlink($tempFile);

echo "=== Time Breakdown ===\n";
echo sprintf("createStringTable: %.3fs (%.1f%%)\n", $stringTableTime, $stringTableTime / $totalSaveTime * 100);
echo sprintf("createStyleDictionaries: %.3fs (%.1f%%)\n", $styleTime, $styleTime / $totalSaveTime * 100);
echo sprintf("writeWorksheet (all): %.3fs (%.1f%%)\n", $totalSheetTime, $totalSheetTime / $totalSaveTime * 100);
for ($i = 0; $i < $sheetCount; ++$i) {
echo sprintf(" sheet %d: %.3fs\n", $i, $sheetTimes[$i]);
}
$otherTime = $totalSaveTime - $stringTableTime - $styleTime - $totalSheetTime;
echo sprintf("other (zip, rels, etc): %.3fs (%.1f%%)\n", $otherTime, $otherTime / $totalSaveTime * 100);
echo sprintf("TOTAL save(): %.3fs\n", $totalSaveTime);

$spreadsheet->disconnectWorksheets();
21 changes: 21 additions & 0 deletions src/PhpSpreadsheet/Parallel/Backend/BackendInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace PhpOffice\PhpSpreadsheet\Parallel\Backend;

use Closure;

interface BackendInterface
{
/**
* Execute tasks in parallel (or sequentially for the fallback backend).
*
* @param list<mixed> $tasks Array of task inputs
* @param Closure $worker Function that receives a task input and returns a result
* @param int $maxWorkers Maximum number of concurrent workers
*
* @return list<mixed> Results in the same order as tasks
*/
public function execute(array $tasks, Closure $worker, int $maxWorkers): array;

public static function isAvailable(): bool;
}
29 changes: 29 additions & 0 deletions src/PhpSpreadsheet/Parallel/Backend/ParallelTaskError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace PhpOffice\PhpSpreadsheet\Parallel\Backend;

/**
* Serializable error container passed from child to parent via IPC.
*/
class ParallelTaskError
{
private string $message;

private int $code;

public function __construct(string $message, int $code = 0)
{
$this->message = $message;
$this->code = $code;
}

public function getMessage(): string
{
return $this->message;
}

public function getCode(): int
{
return $this->code;
}
}
Loading
Loading