vapor / fluent

Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
https://docs.vapor.codes/4.0/fluent/overview/
MIT License
1.32k stars 172 forks source link

Really slow performance on benchmarks #550

Closed grubicv closed 4 years ago

grubicv commented 6 years ago

I've cloned the API template, changed it to use MySQL, configured MySQL params, added one route (GET /devices) which calls the DeviceController's index method which gets 100 devices from the DB and sends a response as a json array. I've benchmarked it with ab and it fails (timeout). With wrk:

wrk -d 10 -t 10 -c 10 http://localhost:8080/devices

I get only 7 req/sec. Laravel does 20-30 req/sec. Rx-Jersey does 400 req/sec.

MySQL query that Vapor does (and others) takes 1-2ms.

SELECT * FROM `devices` LIMIT 100 OFFSET 0;

Making a request from Postman and similar tools: Vapor 3: 110-120ms Laravel: 70-80ms Rx-Jersey: 9-12ms

Attached is a DB dump and here is the model and the controller for reproduction:

import Vapor

final class DeviceController {
    func index(_ req: Request) throws -> Future<[Device]> {
        return Device.query(on: req).range(..<100).all()
    }
}
import FluentMySQL
import Vapor

final class Device: MySQLModel {
    static let name = "devices"
    var id: Int?

    // mandatory
    var createdOn: Date
    var bdMAC: String
    var serial: String
    var model: String
    var code: String
    var account_id: Int

    // optional
    var mvi: String?
    var lastOdometer: Int?
    var lastSeenOn: Date?
    var lastLat: Float?
    var lastLong: Float?
    var lastHeading: Int?
    var lastUpgradeChkOn: Date?
    var lastExtra: String?
    var lastEventCreatedOn: Date?
    var bvi: String?
    var note: String?
    var device_group_id: Int?

    init(id: Int? = nil, createdOn: Date, bdMAC: String, serial: String, model: String, code: String, account_id: Int, mvi: String?, lastOdometer: Int?, lastSeenOn: Date?, lastLat: Float?, lastLong: Float?, lastHeading: Int?, lastUpgradeChkOn: Date?, lastExtra: String?, lastEventCreatedOn: Date?, bvi: String?, note: String?, device_group_id: Int?) {
        self.id = id
        self.createdOn = createdOn
        self.bdMAC = bdMAC
        self.serial = serial
        self.model = model
        self.code = code
        self.account_id = account_id
        self.mvi = mvi
        self.lastOdometer = lastOdometer
        self.lastSeenOn = lastSeenOn
        self.lastLat = lastLat
        self.lastLong = lastLong
        self.lastHeading = lastHeading
        self.lastUpgradeChkOn = lastUpgradeChkOn
        self.lastExtra = lastExtra
        self.lastEventCreatedOn = lastEventCreatedOn
        self.bvi = bvi
        self.note = note
        self.device_group_id = device_group_id
    }
}

extension Device: Migration { }

extension Device: Content { }

extension Device: Parameter { }

vapor_test_2018-07-25.sql.zip

tanner0101 commented 6 years ago

Hey @grubicv thanks for reporting this. That should definitely be going faster. I will take a look.

tanner0101 commented 4 years ago

Track here: https://github.com/vapor/fluent-kit/issues/301