-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Optimized Reseller Payment Import (Store) #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
sayuru-akash
wants to merge
1
commit into
main
from
perf/reseller-import-optimization-9377001618446848222
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature; | ||
|
|
||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Foundation\Testing\WithFaker; | ||
| use Tests\TestCase; | ||
| use App\Models\User; | ||
| use App\Models\Reseller; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Facades\Session; | ||
|
|
||
| class ResellerPaymentImportOptimizationTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| public function test_import_store_performance() | ||
| { | ||
| // Setup | ||
| $user = User::factory()->create(); | ||
| $this->actingAs($user); | ||
|
|
||
| // Seed 50 Resellers | ||
| $resellers = collect(); | ||
| for ($i = 0; $i < 50; $i++) { | ||
| $resellers->push(Reseller::create([ | ||
| 'business_name' => 'Reseller ' . $i, | ||
| 'name' => 'Owner ' . $i, | ||
| 'mobile' => '077123456' . $i, | ||
| 'due_amount' => 10000, | ||
| ])); | ||
| } | ||
|
|
||
| // Create 1000 simulated payment records | ||
| $previewData = []; | ||
| $resellerIds = $resellers->pluck('id'); | ||
| for ($i = 0; $i < 1000; $i++) { | ||
| $previewData[] = [ | ||
| 'reseller_id' => $resellerIds->random(), | ||
| 'amount' => 100, | ||
| 'method' => 'cash', | ||
| 'reference' => 'REF' . $i, | ||
| 'date' => now()->format('Y-m-d'), | ||
| 'errors' => [] | ||
| ]; | ||
| } | ||
|
|
||
| // Set session data | ||
| session(['import_preview_data' => $previewData]); | ||
|
|
||
| // Enable query logging | ||
| DB::enableQueryLog(); | ||
| $startTime = microtime(true); | ||
|
|
||
| // Call the store method via route | ||
| $response = $this->post(route('reseller-payments.import.store')); | ||
|
|
||
| $endTime = microtime(true); | ||
| $queries = DB::getQueryLog(); | ||
| $queryCount = count($queries); | ||
| $executionTime = $endTime - $startTime; | ||
|
|
||
| // Output results | ||
| echo "\n\nBenchmark Results:\n"; | ||
| echo "Execution Time: " . round($executionTime, 4) . " seconds\n"; | ||
| echo "Total Queries: " . $queryCount . "\n\n"; | ||
|
|
||
| // Assertions | ||
| $response->assertRedirect(route('reseller-payments.index')); | ||
| $this->assertDatabaseCount('reseller_payments', 1000); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new condition
isset($resellers[$row['reseller_id']])depends on an exact array key match, which can fail for string-formatted IDs (for example'001'or'1 ') even when preview accepted the row viaReseller::find(...)and SQL numeric coercion. In that case,ResellerPaymentis still created but the resellerβsdue_amountis not decremented, creating inconsistent financial data. Normalize/castreseller_idbeforewhereInand lookup to preserve previous behavior.Useful? React with πΒ / π.