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

Feature request: add max execution time #620

Open
Gemorroj opened this issue Nov 13, 2024 · 6 comments
Open

Feature request: add max execution time #620

Gemorroj opened this issue Nov 13, 2024 · 6 comments

Comments

@Gemorroj
Copy link

Gemorroj commented Nov 13, 2024

Sometimes "BoxPacker" works for a very long time and we need to force it to stop and continue working with other code.
I would like to add some kind of countdown and throw an exception when a certain limit is reached.
Something like this:

$limitSeconds = 2;


$startTime = microtime(true);
while(true) { // packing process
    sleep(1);

    $currentTime = microtime(true);
    if ($currentTime - $limitSeconds > $startTime) {
        throw new \Exception('Time Limit Exception');
    }
}
@dvdoug
Copy link
Owner

dvdoug commented Nov 16, 2024

Hi @Gemorroj

I understand the desire, but would like to understand what it is that's taking too long for you in the first place. Is this a case where you're packing a very large number of items and/or have a very large number of box types?

@Gemorroj
Copy link
Author

@dvdoug These are both cases. There are many packages, there are many goods, there are many of both.
This is a service for our customers and customers have different scenarios.

@dvdoug
Copy link
Owner

dvdoug commented Nov 16, 2024

If you could supply a couple of examples, I'd be happy to try and figure out if there are any obvious bottlenecks that could speed things up

@Gemorroj
Copy link
Author

<?php

declare(strict_types=1);

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

use DVDoug\BoxPacker\Packer;
use DVDoug\BoxPacker\Test\TestBox;
use DVDoug\BoxPacker\Test\TestItem;
use DVDoug\BoxPacker\Rotation;

$packer = new Packer();
$packer->setMaxBoxesToBalanceWeight(0);

// boxes
for ($i = 0; $i < 100; $i++) {
    $box = new TestBox(
        reference: 'box '.$i,
        outerWidth: $i * 10,
        outerLength: $i * 10,
        outerDepth: $i * 10,
        emptyWeight: 1,
        innerWidth: $i * 10,
        innerLength: $i * 10,
        innerDepth: $i * 10,
        maxWeight: 10000,
    );
    $packer->addBox($box);
}

// items
$item = new TestItem(
    description: 'item 1',
    width: 100,
    length: 100,
    depth: 100,
    weight: 100,
    allowedRotation: Rotation::BestFit, // need exactly best fit
);
$packer->addItem($item, 500);


$startTime = \microtime(true);
$result = $packer->pack();
$endTime = \microtime(true);

echo \round($endTime - $startTime, 2).'s.'.\PHP_EOL;
// \print_r($result);

on my local computer, the code runs for about 50 seconds.

dvdoug added a commit that referenced this issue Nov 17, 2024
@dvdoug
Copy link
Owner

dvdoug commented Nov 17, 2024

Hi @Gemorroj

Do you have Xdebug enabled? That slows things down significantly. The testcase I just added based on your example takes about 7.3seconds for me and that's on a fairly underpowered laptop-class CPU.

On the CI, the difference between https://github.com/dvdoug/BoxPacker/actions/runs/11879381968/job/33101223212 and https://github.com/dvdoug/BoxPacker/actions/runs/11880522441/job/33103770101 is only 3.1 seconds

@Gemorroj
Copy link
Author

@dvdoug hm. yes. There was a xdebug.mode=develop. I didn't think this mode had such a big impact. In xdebug.mode=off mode, I also get 7 seconds.
But even this time is too long for us. And there may be even more boxes or items.

Gemorroj added a commit to Gemorroj/BoxPacker that referenced this issue Dec 23, 2024
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

No branches or pull requests

2 participants