Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. High CPU load when using XMLHttpRequest
Forum Updated to NodeBB v4.3 + New Features

High CPU load when using XMLHttpRequest

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 541 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PhilipT
    wrote on 11 Apr 2017, 08:49 last edited by
    #1

    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

    1 Reply Last reply
    0

    1/1

    11 Apr 2017, 08:49

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved