High CPU load when using XMLHttpRequest



  • Hello!

    In my QML application I am using multiple instances of a custom Item, which uses XMLHttpRequest to get and put data (Strings) from and to a REST API. The App is working fine and the CPU load rarely passes the 10% mark. However, since I added the REST interface the CPU load increased dramatically, somtimes even over 90%..

    The QML Item:

    import QtQuick 2.0
    
    Item {
        id:ohi
    
        property string itemname: ""
        property int port: 8080
        property var state
        property var lastReceivedJson
        property var step
        property var min
        property var max
        property var inc_val
        property var dec_val
        property bool polling: false
        property string ip_addr: (Qt.platform.os == "windows") ? "192.168.2.55" : "127.0.0.1"
        property bool receivedData: false
        property var json
        onJsonChanged: {
            //lagFix.stop();
            if(json.state !== undefined) {
                receivedData = true;
            } else {
                receivedData = false;
            }
            lastReceivedJson = json;
            state = json.state;
        }
    
        function mylog( val ) {
            console.log( val );
        }
    
        function setValue( value ) {
            var url = "http://" + ip_addr + ":" + String(port) + "/rest/items/" + itemname;
            setWorker.sendMessage( {"url": url, "value": value } );
        }
    
        Timer {
            id: updateTimer
            interval: 5000
            running: polling; repeat: true
            onTriggered: {
                var url = "http://" + ip_addr + ":" + String(port) + "/rest/items/" + itemname;
                getWorker.sendMessage( {"url": url, "parameter": null } );
            }
        }
    
        WorkerScript {
            id: getWorker
            source: "workerscript_get.js"
    
            onMessage: {
                json = messageObject.json;
            }
        }
    
        WorkerScript {
            id: setWorker
            source: "workerscript_post.js"
        }
    }
    

    The JS for the WorkerScripts (workerscript_get):

    function request(url, arguments, callback) {
        var myurl = url;
        // todo: add arguments
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onreadystatechange = function() {
                if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
                    callback(xhr);
                }
        }
        xhr.send();
    }
    
    WorkerScript.onMessage = function(message) {
        request(message.url, message.parameter, mycallback );
    }
    
    function mycallback( o ) {
        var json = JSON.parse(o.responseText);
        WorkerScript.sendMessage({ json: json });
    }
    

    and workerscript_post:

    
    function sendVal(url, value) {
        var myurl = url;
        // todo: add arguments
        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.onreadystatechange = function() {
                if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {}
                else { console.log("Error sending state " + value + " !!"); }
        }
        xhr.send( value );
    }
    
    WorkerScript.onMessage = function( message ) {
        sendVal(message.url, message.value);
    }
    

    Could anyone lead me to the reason of the high CPU load?

    Thanks a lot!!

    HW: Allwinner A20


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.