Created
December 9, 2012 16:33
-
-
Save ThePixelDeveloper/4245916 to your computer and use it in GitHub Desktop.
Generating 15,000 fake users for local development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15,000 profiles created | |
real 0m34.258s | |
user 0m23.657s | |
sys 0m5.092s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Nolimits\Dummy\Commands; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class UsersCommand extends Command | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('dummy:users') | |
->setDescription('Generate dummy users') | |
->addOption( | |
'seed', | |
null, | |
InputOption::VALUE_OPTIONAL, | |
'Passing a seed will force the same generation of users each time.' | |
) | |
; | |
} | |
protected function batchSize() | |
{ | |
return $this->total(); | |
} | |
protected function total() | |
{ | |
return 15000; | |
} | |
protected function getSilex() | |
{ | |
return $this->getHelperSet()->get('app')->getSilex(); | |
} | |
protected function entityManager() | |
{ | |
$silex = $this->getSilex(); | |
return $silex['orm.em']; | |
} | |
protected function fakerGenerator() | |
{ | |
$silex = $this->getSilex(); | |
return $silex['faker']; | |
} | |
protected function userEntity() | |
{ | |
return new \Nolimits\Users\Entities\UsersEntity; | |
} | |
protected function userData($generator) | |
{ | |
return array( | |
'username' => $generator->firstName, | |
'email' => $generator->safeEmail, | |
'password' => $generator->safeEmail, | |
'logins' => $generator->randomDigit, | |
'last_login' => $generator->unixTime, | |
'ip_address' => sprintf('%u\n', ip2long($generator->ipv4)), | |
'score' => $generator->randomDigit, | |
'registration_date' => $generator->unixTime, | |
'registration_ip_address' => sprintf('%u\n', ip2long($generator->ipv4)), | |
); | |
} | |
protected function counter($current, $total) | |
{ | |
$current = str_pad($current, strlen($total)); | |
return "$current/$total"; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$generator = $this->fakerGenerator(); | |
if ($seed = $input->getOption('seed')) | |
{ | |
$generator->seed($seed); | |
} | |
for($i = 1; $i <= $this->total(); $i++) | |
{ | |
$data = $this->userData($generator); | |
$output->write("Profile [".$this->counter($i, $this->total())."][".$data['username']."]\033[K\r"); | |
$user = $this->userEntity(); | |
$user->exchangeArray($data); | |
$this->entityManager()->persist($user); | |
if ($i % $this->batchSize() === 0) | |
{ | |
$this->entityManager()->flush(); | |
$this->entityManager()->clear(); | |
} | |
} | |
$output->writeln("\033[K\r<info>".number_format($this->total()).' profiles created</info>'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment