Skip to content
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

Get upload speed? #2318

Open
Coder-Miao opened this issue Aug 26, 2023 · 1 comment
Open

Get upload speed? #2318

Coder-Miao opened this issue Aug 26, 2023 · 1 comment
Labels

Comments

@Coder-Miao
Copy link

I have a file upload request, and moya will return the upload progress, but I need to get the upload speed, how should I handle it?

@peril-moya peril-moya bot added the question label Aug 26, 2023
@BhavaniReddyAndem
Copy link

To calculate the upload speed based on the progress updates received from an upload request in Moya, you can track the time it takes to upload a specific amount of data and then calculate the speed using the formula:

Speed = Uploaded data size / Time taken

Here's how you can implement this in Swift:

Track Upload Progress and Time:
In the Moya provider, use the progress closure to track the progress updates. Also, record the start time when the upload begins and the end time when the upload completes.

import Moya

// Create a Moya provider with your target (your API provider)
let provider = MoyaProvider()

// Your upload data
let data: Data = // ... your data to upload ...

// Track upload progress and time
let startTime = Date()

provider.request(.upload(data: data), callbackQueue: .main, progress: { progress in
// Track progress updates
let uploadedDataSize = Double(progress.completedUnitCount)
let totalTimeElapsed = Date().timeIntervalSince(startTime)

// Calculate upload speed
let uploadSpeed = uploadedDataSize / totalTimeElapsed // in bytes per second
let speedInKBps = uploadSpeed / 1024 // in KBps

// Use uploadSpeed as needed (e.g., update UI, log, etc.)
print("Upload Speed: \(speedInKBps) KBps")

}) { result in
// Handle upload completion
switch result {
case .success(let response):
// Handle success
print("Upload completed successfully")
case .failure(let error):
// Handle error
print("Upload failed with error: (error)")
}
}

Calculate Speed and Use It:
Inside the progress closure, calculate the upload speed by dividing the uploaded data size (in bytes) by the total time elapsed (in seconds). You can convert the speed to KBps, MBps, etc., based on your preference.

In the example above, the upload speed is calculated in KBps. You can adjust the units based on your requirements.

Please note that this approach assumes a continuous upload process without interruptions. If there are pauses or fluctuations in the upload process, you might want to handle those cases accordingly. Additionally, consider implementing error handling and validations to ensure accurate speed calculations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants