Skip to content

Commit

Permalink
add script to emulate datasets to test execution times
Browse files Browse the repository at this point in the history
  • Loading branch information
sweikenb committed Jul 14, 2024
1 parent 1fe0ff5 commit 857920d
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
53 changes: 53 additions & 0 deletions examples/100_random.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use Sweikenb\Library\Dirty\Model\ConfigModel;
use Sweikenb\Library\Dirty\Service\DirtyCheckService;

require __DIR__.'/../vendor/autoload.php';
$rdata = require __DIR__.'/data/random_data.inc.php';

// config
$numItems = 1000;

// create items
$items = [];
for ($i = 0; $i < $numItems; ++$i) {
$item = $rdata(mt_rand(5, 50));
$items[] = $item;
$items[] = $item;
}
$actualItems = count($items);
shuffle($items);

// get instance
$dirtyCheck = new DirtyCheckService();

// set exclude config
$exclude = new ConfigModel(ignoreFieldPath: [
'owner.password',
'owner.passwordSalt',
'owner.passwordSalt',
'owner.locationDetails.date',
'owner.preferences.date',
'reference.date',
'tags.0.date',
'tags.1.date',
'categories.0.date',
'categories.1.date',
'createdAt',
'updatedAt',
]);

// measure execution time
$start = microtime(true);
foreach ($items as $item) {
$result = $dirtyCheck->execute($item['id'], $item, $exclude);
}
$finish = microtime(true);

// process results
$diff = $finish - $start;
fwrite(STDOUT, sprintf("\nItems processed: %s", $actualItems));
fwrite(STDOUT, sprintf("\nExecution Time: %s", $diff));
fwrite(STDOUT, sprintf("\nAvg. Time/Item: %s", $diff / $actualItems));
fwrite(STDOUT, "\n");
95 changes: 95 additions & 0 deletions examples/data/random_data.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

function randomStr(int $length = 25): string
{
$signs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$max = mb_strlen($signs) - 1;

$buffer = '';
for ($i = 0; $i < $length; ++$i) {
$buffer .= $signs[mt_rand(0, $max)];
}

return $buffer;
}

class User
{
public static function create(): self
{
return new self(
mt_rand(1000, 100000),
randomStr(),
sprintf('%s@%s.com', randomStr(15), randomStr(15)),
randomStr(),
randomStr(),
SimpleVal::create(),
SimpleVal::create(),
);
}

public function __construct(
public readonly int $id,
public readonly string $name,
public readonly string $email,
public readonly string $password,
public readonly string $passwordSalt,
public readonly SimpleVal $locationDetails,
public readonly SimpleVal $preferences,
) {
}
}

class SimpleVal
{
public static function create(): self
{
return new self(
mt_rand(1000, 100000),
randomStr(),
randomStr(),
new DateTime('now')
);
}

public function __construct(
public readonly int $id,
public readonly string $key,
public readonly string $value,
public readonly DateTime $date,
) {
}
}

return function (int $numFields = 30) {
$fields = [];
for ($i = 0; $i < $numFields; ++$i) {
$code = randomStr();
$fields[$code] = [
'code' => $code,
'data' => SimpleVal::create(),
];
}

return [
'id' => mt_rand(100, 10000),
'owner' => User::create(),
'slug' => randomStr(),
'name' => randomStr(),
'reference' => SimpleVal::create(),
'tags' => [
SimpleVal::create(),
SimpleVal::create(),
],
'categories' => [
SimpleVal::create(),
SimpleVal::create(),
],
'fields' => $fields,
'details' => randomStr(500),
'createdAt' => new DateTime('-10 days'),
'createdBy' => User::create(),
'updatedAt' => new DateTime('now'),
'updatedBy' => User::create(),
];
};

0 comments on commit 857920d

Please sign in to comment.