0
0

More than 5 years have passed since last update.

Laravelでfakerを利用し、Eloquentのcursor()・all()・chunk()の速度を計測するために、テストの点数のデータを登録してみた(第1回)

Last updated at Posted at 2019-04-07

◆目的
最終的に、laravelのeloquentのcursor()・all()・chunk()の速度を計測する
今回:テストの点数データの登録まで

◆model・migrationファイル作成

まず、テーブルを作成する。

コマンド
php artisan make:model Models/Student -m
migrationファイル
class CreateStudentsTable extends Migration
{
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 20)->comment('名前');
            $table->bigInteger('japanese')->comment('国語');
            $table->bigInteger('math')->comment('数学');
            $table->bigInteger('english')->comment('英語');
            $table->bigInteger('social_studies')->comment('社会');
            $table->bigInteger('science')->comment('理科');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('students');
    }
}

名前とご科目の点数を入れるカラムを用意した。

コマンド
php artisan migrate
Migrating: 2019_04_07_085555_create_students_table
Migrated:  2019_04_07_085555_create_students_table

テーブル作成完了

modelクラス
class Student extends Model
{
    //明示的にテーブルと紐づけ(記載しない場合、Model名の複数形のテーブルと紐づく(今回ならstudents))
    protected $table = 'students';

    //複数代入に代入を許可しないカラム(主キーなどを設定する)
    protected $guarded = ['id'];

    //データが入ったときに自動でcreated_atとupdate_atにデータが入ることを明示的に表示
    public $timestamps = true;

    //複数代入時に代入を許可するカラム
    protected $fillable = ['name', 'japanese', 'math', 'english', 'social_studies', 'science'];
}

設定完了

◆factory・fakerの利用

今回は、速度計測が最終目標のため、一つ一つデータを入れず、factory・fakerを使って、ランダムなデータを大量に入れていきたいと思う。

faker:ダミーデータの作成
factory:DBにレコードを登録するときの便利クラス

コマンド
//ファクトリークラスの作成
php artisan make:factory StudentFactory
StudentFactory
$factory->define(Student::class, function (Faker $faker) {
    return [
        'name'           => $faker->name,
        'japanese'       => $faker->numberBetween(0, 100),
        'math'           => $faker->numberBetween(0, 100),
        'english'        => $faker->numberBetween(0, 100),
        'social_studies' => $faker->numberBetween(0, 100),
        'science'        => $faker->numberBetween(0, 100),
    ];
});

名前はランダムで、点数は0~100点をランダムで生成してもらうことにした。
[fakerで設定できるもの]https://qiita.com/tosite0345/items/1d47961947a6770053af

名前を日本語で登録してもらうために、

config\app.php
'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'),
env
APP_FAKER_LOCALE=ja_JP
コマンド
//設定変えたので、キャッシュクリア
php artisan config:cache

[日本語設定]
https://qiita.com/shima_6_you/items/83af3b9fe79387d82286

データ登録用にSeederファイルを作成する

コマンド
php artisan make:seed StudentsTableSeeder
StudentsTableSeeder
class StudentsTableSeeder extends Seeder
{
    public function run()
    {
        factory(Student::class, 10000)->create();
    }
}

ファクトリーメソッドで、10000件登録する。

DatabaseSeeder
class DatabaseSeeder extends Seeder
{
    public function run()
    {
         $this->call(StudentsTableSeeder::class);
    }
}

実行するSeederファイルを加えて、コマンドでデータを入れてみる。

コマンド
php artisan db:seed
Seeding: StudentsTableSeeder
Database seeding completed successfully

10000件登録完了

◆まとめ

長くなりそうなので、今回はfacker・factoryを利用して、データの登録までを行った。
次回以降、all()・chunck()・cursor()の速度を図りたいと思う。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0