/** * Creates all needed classes and filles them with dummie data */ public function installAction() { Time::check(); $nameGenerator = new \NameGenerator\Generator(); $this->createTables(); $teachersCount = $this->getRequest()->getParam('teachers', 10000); $pupilsCount = $this->getRequest()->getParam('pupils', 100000); /** @var \Application\Model\Teacher $teacherTable */ $teacherTable = $this->getServiceLocator()->get('TeacherTable'); /** @var \Application\Model\Pupil $pupilTable */ $pupilTable = $this->getServiceLocator()->get('PupilTable'); /** @var \Application\Model\TeacherPupil $teacherPupilTable */ $teacherPupilTable = $this->getServiceLocator()->get('TeacherPupilTable'); Console::getInstance()->writeLine('Tables created in ' . Time::check() . ' sec.'); $pupils = $nameGenerator->get($pupilsCount, ['email', 'birthday']); foreach ($pupils as $randomPupil) { $level = \Application\Entity\Pupil::$levels[rand(0, count(\Application\Entity\Pupil::$levels) - 1)]; /** @var \Application\Entity\Pupil $pupil */ $pupil = $pupilTable->getEntity(); $pupil->setName($randomPupil['first'] . ' ' . $randomPupil['last'])->setEmail($randomPupil['email'])->setBirthday($randomPupil['birthday'])->setLevel($level); $pupilTable->insert($pupil); } Console::getInstance()->writeLine($pupilsCount . ' pupils generated in ' . Time::check() . ' sec.'); $teachers = $nameGenerator->get($teachersCount, ['phone']); foreach ($teachers as $randomTeacher) { $gender = $randomTeacher['gender'] == \NameGenerator\Gender::GENDER_MALE ? \Application\Entity\Teacher::GENDER_MALE : \Application\Entity\Teacher::GENDER_FEMALE; /** @var \Application\Entity\Teacher $teacher */ $teacher = $teacherTable->getEntity(); $teacher->setName($randomTeacher['first'] . ' ' . $randomTeacher['last'])->setGender($gender)->setPhone($randomTeacher['phone']); $teacherTable->insert($teacher); } Console::getInstance()->writeLine($teachersCount . ' teachers generated in ' . Time::check() . ' sec.'); $pupilMaxId = $pupilTable->getMaxId(); $teacherMaxId = $teacherTable->getMaxId(); $linksCount = 0; for ($teacherId = 1; $teacherId < $teacherMaxId; $teacherId++) { $except = []; for ($j = 0; $j < rand(0, 3); $j++) { $pupil_id = rand(0, $pupilMaxId); if (in_array($pupil_id, $except)) { continue; } $except[] = $pupil_id; $link = $teacherPupilTable->getEntity(); $link->teacher_id = $teacherId; $link->pupil_id = $pupil_id; $teacherPupilTable->insert($link); $linksCount++; } } Console::getInstance()->writeLine($linksCount . ' links generated in ' . Time::check() . ' sec.'); }
<?php spl_autoload_register(function ($class) { return include preg_replace('/\\\\/i', DIRECTORY_SEPARATOR, $class) . '.php'; }); $count = 1000; $start = microtime(true); $nameGenerator = new \NameGenerator\Generator(); $names = $nameGenerator->get($count, ['birthday', 'phone']); var_dump($names[0]); //foreach ($names as $name) { //echo $name['first'] . ' ' . $name['last'] . ' (' . $name['gender'] . ')' . ' - ' . $name['email'] . PHP_EOL; // //} echo $count . ' names generated in ' . (microtime(true) - $start) . ' sec.' . PHP_EOL;
/** * @param $service \App\Service\MysqlStorageble * @param $itemsCount int * @param $additionalFields array * @param $callable callable * @return int * @throws \Exception */ private function generateItems($service, $itemsCount, $additionalFields, $callable) { $count = 0; $generator = new \NameGenerator\Generator(); while ($count < $itemsCount) { $currentCount = min(self::CHUNK_SIZE, $itemsCount - $count); $items = $generator->get($currentCount, $additionalFields); $items = array_map($callable, $items); try { $count += $service->bulkInsert($items); } catch (\Exception $e) { } } return $count; }