Hello, I use response.readTest() to obtain the bod...
# ktor
v
Hello, I use response.readTest() to obtain the body string but for a long response it’s take a long time. Is it a solution to optimize that please ?
o
how long is response and how long it takes? It’s essentially downloading everything that is being sent and constructing a
String
out of it, so it’s kinda expected.
v
The response Json have 7000 lines. So I use gson.fromJson(response.readText(), Array<Employee>::class.java) to convert the response in an Array of Employee and that take at least 4 - 5 seconds
Is it a solution to optimize this without maybe response.readText() ?
o
Hard to tell without specific information, but 4-5secs looks like too much. If you just read text from file and use gson to deserialize it, how long does it takes?
v
Just to read the text with response.readText() take 2~3sec
o
what if you use
curl
to query the same endpoint?
v
The request take 2 sec with postman (like curl ?) And after the readText() took a long time
o
Not sure, if I understand. If postman takes 2sec to read it, and response.readText takes almost same time… is there a ktor problem, or server or connection problem?
May be if gson takes about 2sec to deserialize it, and then server serializes something for approx same time, that could be giving you 4secs total?
v
When I isolate each line, I found the time is lost in response.readText()…
o
Sorry for being slow, may be evening 🙂 Let’s try to do it step by step. 1.
response.readText()
takes 2-3 secs? 2. The rest of the time 4-5 minus 2-3 = 2-3 secs are consumed by
gson
deserializing? 3.
postman
to the same URL takes 2 secs? Are statements above true?
v
1. Fetch all my data from an external API : 2 sec with postman
2 : Between 4 ~ 5 sec to readText and deserialize
And When I isolate readText, 2~3 sec
Sorry if It’s not clear 😞
If I do that println(“START”) val text = response.readText() println(“END”) return gson.fromJson(text, Array<Employee>::class.java) //This line
I see response.readText() take very long time
o
But
readText
takes same time as
postman
, so it’s just fetching the data from remote server that takes this time, no?
v
No with postman I just tried to fetch from Zoho API And I get the result in 2 sec. In ktor, I fetch this result, I transform it with fromJson and after I resend it with my transformations so when I have 6 ~ 7 sec , we can remove 2 sec for the request and the others secondes was took by readText()
o
I think I can’t get what’s the problem is. Fetching data from the remote server requires socket connection, transferring data over network, decoding text with UTF8 into a String and a lot of other activities.
postman
and
readText
both do this work and from your statements it sounds like they both use the same amount of time. I don’t understand what can you optimize there.
v
When I fetch the data with postman it’s directly on Zoho API. With Ktor I do exactly the same call to Zoho but in my ktor application when I decode the response with readText() it takes a lot of time with postman only 2 sec with readText() at lest 5~6 sec. So is it another method without readText() please
o
Between 4 ~ 5 sec to readText and deserialize
Just to read the text with response.readText() take 2~3sec
You said this previously, and
postman
doesn’t deserialize anything. So I don’t understand changes in numbers (2-3, 5-6, 6-7) and what other API are you looking for.
v
I’m sorry if it’s not clear. To put it in a nutshell : I know the request to Zoho take 2 sec, In the ktor back, I received this response, I extract the content with response.readText() and after I transform the string in Employee object. I transform this object and I send it back. When I asked ktor, the response came after 6~7 sec, I know the request take 2 sec, so the other time is for readText() because if I print before it and after it, I have at least 3 sec to display the print after. So readText() take much time to just transform my content in string, is it possible to optimize this function, is it another method like readBytes() and after transform it in string please ?
👀 1
c
You can use
client.get<ByteArray>(...
to receive response body as array
e
Could you check the data size? We measure
readText
and in worst case works around
63.205ms
on
32Mb
Unicode text.
v
Only 261 Kb so it’s really strange that takes that time …
I’m pretty sure readText() take long time because when I remove that line, it’s fast <= 2sec
e
Could you provide the request and measure code?
v
suspend fun getAllEmployees(): Array<Employee>? { val responseHttp = client.get<HttpResponse>(routeZohoAllEmployee) return verificationResponse(responseHttp) }
//Check status response to see if the request was OK suspend fun verificationResponse(response: HttpResponse): Array<Employee>? { if (response.status == HttpStatusCode.OK) { return gson.fromJson(response.readText(), Array<Employee>::class.java) //This line } //Convert and filter to have the good informations else { throw returnErreurZoho(response.readText()) } }
And to measure the time, i isolated the readResponse()
e
Did you measure a single call of
getAllEmployees
?
and where is the
readResponse()
method?
v
eturn gson.fromJson(response.readText(), Array<Employee>::class.java) //This line
response.readText()
No I measure multiple calls and each call have different time between 3s to 11sec
e
could you measure the same code with client.get<ByteArray> instead?
v
Ok
After I transform the ByteArray in string or I just Measure the time for client.get<ByteArray> ?
Ok just client.get<ByteArray> takes between 3~9 sec
e
The
get<HttpResponse>()
method doesn’t receive the body. But
readText()
and
get<ByteArray>
receive.
v
Yes and that’s what takes all the time but The strange things is you said that takes only 63 ms for 32Mb
But not in my case 😞
e
It takes
63ms
without network
v
With postman the request took between 1~2 sec so the rest of the time is used by readText()...
With fuel is very faster...
I don’t understand why
e
Could you provide the code how you do parallel requests?
v
I don’t understand sorry, what do you want please ?
It’s very weird with fuel, I’m omly between 2~3 sec
e
You mentioned that you test with multiple requests. Could you explain how you run it? What’s is request count and total execution time?
v
I did it very manually with postman just send multiple request. with the ktor client the minimum time is 4~5 sec with fuel it’s 2~3 sec
e
The question is about how exact(and where) you launch the query. And what is the total time of execution?
v
The total time of execution is variable between 4 to 12 sec with the client apache in ktor but with fuel it’s between 3~4 sec. I launch the query in my route /employee after check auth of the employee