Serializing dates is very slow
To be honest I'm not sure whether this is a problem with active_model_serializers or another library used for serialization, but I've discovered that serializing dates (e.g. created_at and updated_at) is slooooow.
For comparison, I benchmarked serializing about 300 models, with and without including created_at and updated_at fields.
Without created_at and updated_at:
Concurrency Level: 12
Time taken for tests: 2.700 seconds
Complete requests: 150
Total transferred: 16468800 bytes
Requests per second: 55.56 [#/sec] (mean)
With created_at and updated_at:
Time taken for tests: 7.401 seconds
Total transferred: 19644000 bytes
Requests per second: 20.27 [#/sec] (mean)
I will do some more research into what's causing this, but wanted to know if anyone else has experienced anything like this.
Also, for comparison, I included but hard-coded these fields:
def updated_at
"2012-12-29T00:42:44Z"
end
def created_at
"2012-12-29T00:42:44Z"
end
The result is essentially the same as in the case that the fields are computed:
Requests per second: 55.23 [#/sec] (mean)
Right. I'm pretty sure this operation is just slow. Further benchmarks needed though.
Yeah, upon further inspection it seems that the Date/Time classes are just very slow...
I was able to improve performance by adding these poor-man's serialization methods to the serializer:
def updated_at
object.updated_at_before_type_cast[0..18] + 'Z'
end
def created_at
object.created_at_before_type_cast[0..18] + 'Z'
end
Which resulted in:
Requests per second: 45.58 [#/sec] (mean)
Should work in (all?) reasonable SQL DBMSes.
Seems good. Thanks.
Title of the issue is incorect. I've benchmarked my dataset:
w\ type_cast w\o type_cast
date +~150ms(15%) +~140ms(13%)
date_time +~400ms(41%) +~120ms(12%)
So It might be an issue with TZ stuff or alike and It only affects date_time columns.