MockViewPosts.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Post;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Redis;
  7. class MockViewPosts extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'mock:view-posts';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Mock View Posts';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. // 1、先清空 posts 表
  38. Post::truncate();
  39. // 2、删除对应的 Redis 键
  40. Redis::del('popular_posts');
  41. // 3、生成 100 篇测试文章
  42. Post::factory()->count(100)->create();
  43. // 4、模拟对所有文章进行 10000 次随机访问
  44. for ($i = 0; $i < 1000; $i++) {
  45. $postId = mt_rand(1, 100);
  46. $response = Http::get('http://localhost/posts/' . $postId);
  47. $this->info($response->body());
  48. }
  49. }
  50. }