jay_gorio's avatar

Why laravel 5.3 phpunit so slow when using faker

I am using sqlite for my test and using phpunit. I configure created another .env.testing and defined connection as sqlite. I also configure phpunit.xml and using DB_CONNECTION as sqlite. Also I used faker for my test as shown below. However it took so long (more than 3 minutes) for the test to be completed. I created also a new laravel 5.3 project but with the same testing result. When using laravel 5.2, my tests are extremely fast. However in 5.3 it seems there is something wrong, especially using faker. Did anyone experience this in laravel 5.3? Or am I missing something?

https://s17.postimg.org/cwjnz6567/result_of_test.png

use Faker\Factory as Faker;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class SeriesTest extends TestCase
{
    use DatabaseTransactions;

    protected $fake;

    public function setUp()
    {
        parent::setUp();

        $this->fake = Faker::create();
    }

        /** @test */
     public function it_display_all_series()
     {
             $series = factory(App\Series::class, 2)->create();

            $this->get('/api/v1/series');

            foreach($series as $series){
            $this->seeJson([        
                'title' => $series->title,
                'description' => $series->description,              
                'image_cover' => $series->image_cover,
                'slug' => $series->slug
            ]);
          }

        $this->assertResponseOk();
    }

Here is my model factory setup

$factory->define(App\User::class, function (Faker\Generator $faker) {
        static $password;

        return [
            'name' => $faker->name,
            'email' => $faker->unique()->safeEmail,
            'password' => $password ?: $password = bcrypt('secret'),
            'remember_token' => str_random(10),
        ];
    });

    $factory->define(App\Series::class, function (Faker\Generator $faker) {
        
        $title = $faker->sentence;

        return [
            'user_id' => factory(App\User::class)->create()->id,
            'title' => $title,
            'description' => $faker->paragraph,
            'image_cover' => $faker->image($dir = '/tmp', $width = 640, $height = 480),
            'slug' => str_slug($title)
        ];
    }); 
0 likes
5 replies
iak's avatar

Old tread. But i'm having same problem now. Did you ever find a solution?

prrng's avatar

Old tread. But i'm having same problem now. Did you ever find a solution? (2)

prrng's avatar

Hai @sti3bas , yes I am. but that is not the problem. I have found the culprit. It is the $faker. On one of my factories, I have 40 fields with 7 of them calling the $faker->randomDigit. After I initiated a variable $randomDigit = $faker->randomDigit and use the variable to the 7 fields. That reduces the time From 8.77 minutes to 4.26 seconds. It is fast!!!!

EDIT to make it relevant with this thread:

I also use $image = 'image.jpg' to fill 3 of fields with supposed to be filled with image path, to substitute $faker->image().

1 like
thamer.belfkih's avatar

this is happening because the use of $faker->image, creating just 5 rows that have an image as a column took 520.43 seconds which is huge amount of time

1 like

Please or to participate in this conversation.