BAB 56. PERFORMANCE OPTIMIZATION
Target Performa
| Metrik | Target | Keterangan |
|---|---|---|
| Login | < 2 detik | Termasuk pembuatan token |
| Dashboard load | < 3 detik | Semua data dengan cache |
| AI Matching | < 5 detik | Async via queue |
| Chat realtime | < 500 ms | WebSocket latency |
| API rata-rata | < 300 ms | Untuk endpoint biasa |
| Leaderboard | < 1 detik | Dari cache Redis |
Optimasi Backend Laravel
1. Eager Loading (N+1 Problem Prevention)
// ❌ Buruk — N+1 query
$requests = Request::all();
foreach ($requests as $r) {
echo $r->student->name; // Query baru setiap iterasi!
}
// ✅ Baik — Eager loading
$requests = Request::with(['student.user', 'subject', 'topic'])->get();
2. Database Indexing
// database/migrations/xxxx_add_indexes.php
Schema::table('requests', function (Blueprint $table) {
$table->index(['school_id', 'status']);
$table->index(['student_id', 'created_at']);
});
Schema::table('bookings', function (Blueprint $table) {
$table->index(['tutor_id', 'status']);
$table->index(['scheduled_at']);
});
Schema::table('ratings', function (Blueprint $table) {
$table->index(['ratee_id']);
});
3. Pagination (Hindari Load Semua Data)
// Selalu gunakan paginate, bukan ->get() untuk daftar
return Request::where('school_id', $schoolId)
->latest()
->paginate(15); // Maksimum 15 per halaman
4. Redis Cache untuk Data Sering Diakses
Lihat BAB 52 — Caching Strategy untuk detail implementasi.
5. Queue untuk Proses Berat
AI Matching, SIS calculation, dan report generation diproses via queue agar API response tetap cepat.
Optimasi Database
Index Utama:
| Tabel | Kolom | Alasan |
|---|---|---|
users | username | Login lookup |
students | school_id, sis_score | Leaderboard query |
requests | school_id, status | Daftar request aktif |
bookings | scheduled_at | Meeting reminder |
ratings | ratee_id | Hitung rata-rata rating |
meetings | check_in_at | Filter sesi aktif |
Query Optimization:
// Gunakan select() untuk ambil kolom yang diperlukan saja
Student::select(['id', 'user_id', 'sis_score', 'level'])
->where('school_id', $schoolId)
->orderByDesc('sis_score')
->limit(100)
->get();
Optimasi Flutter
| Teknik | Implementasi | Dampak |
|---|---|---|
const widget | Gunakan const di semua widget statis | Hindari rebuild berulang |
| Lazy loading | ListView.builder bukan ListView | List panjang lebih efisien |
| Cached image | Package cached_network_image | Kurangi network request |
| Riverpod caching | keepAlive: true untuk data jarang berubah | Kurangi API call |
| Pagination | Load 15 item per scroll | Kurangi payload |
| Skeleton loading | Tampilkan skeleton saat loading | UX lebih baik |
// Contoh const widget
const BookingCard(
title: 'Matematika - Integral',
tutor: 'Ahmad',
);
// Contoh lazy list
ListView.builder(
itemCount: requests.length,
itemBuilder: (context, index) => RequestCard(request: requests[index]),
)
Monitoring Performa
| Metrik | Tools | Threshold Alert |
|---|---|---|
| Response time | Laravel Telescope / Horizon | > 500ms |
| Memory usage | Server monitoring | > 80% |
| CPU usage | Server monitoring | > 75% |
| Queue length | Laravel Horizon | > 100 pending |
| Error rate | Log monitoring | > 1% |
| Cache hit rate | Redis monitoring | < 80% |