简体   繁体   English

我的CPU在Google Cloud上的使用率已达到160%,我似乎不知道为什么吗?

[英]My CPU is reaching 160% on Google Cloud and I can't seem to figure out why?

So I was working on this project and I've deployed it to the virtual machine on google cloud with a machine type of g1-small (v1 CPU, 1.7 GB). 因此,我正在从事这个项目,并将其部署到Google Cloud上的虚拟机上,其计算机类型为g1-small(v1 CPU,1.7 GB)。 In the monitoring section, the CPU reaches upwards of 175% with 160% being the average. 在监视部分,CPU达到了175%的平均值,其中平均值为160%。 Google recommends me to increase my machine type to a custom (v1 CPU, 1.75 GB), but I'm not quite sure what's eating up the CPU. Google建议我将计算机类型增加到自定义(v1 CPU,1.75 GB),但是我不太确定CPU占用了什么。

I have read into blocking and non-blocking in node.js and I know that my code is currently non-blocking so that may be an issue. 我已经阅读了node.js中的阻止和非阻止功能,并且我知道我的代码当前处于非阻止状态,因此可能是一个问题。 But if I change it to blocking, the code will run sequentially and it will run slower. 但是,如果我将其更改为阻塞,则代码将按顺序运行,并且运行速度会变慢。

Thank you in advance. 先感谢您。

Here's my code: https://github.com/QuaziH/CUNYfirst-API/blob/master/looper.js 这是我的代码: https : //github.com/QuaziH/CUNYfirst-API/blob/master/looper.js

const db = require('./db/index');
const text = require('./text');
const api = require('./cf-api');

let looper = () => {
    db.query(`SELECT DISTINCT institution, term, subject FROM classes`, async (error, response) => {
        if (error) {
            return console.error('Error fetching client', error);
        }

        if(response.rowCount === 0){
            console.log(`Table is empty`);
        } else {
            for (let i = 0; i < response.rows.length; i++) {
                let allClasses = await api.getAllSections(`${response.rows[i].institution}`, `${response.rows[i].term}`, `${response.rows[i].subject}`);
                db.query(`SELECT phone, carrier, topic, class_num FROM classes WHERE institution='${response.rows[i].institution}' AND term='${response.rows[i].term}' AND subject='${response.rows[i].subject}'`, (error, response) => {
                    if (error) {
                        return console.error('Error fetching client', error);
                    }

                    for (let j = 0; j < response.rows.length; j++) {
                        let status;
                        try {
                            status = allClasses[response.rows[j].class_num].Status;
                        } catch (e) {
                            if (response.rows[j].carrier === '@tmomail.net' || response.rows[j].carrier === '@mymetropcs.com') {
                                text.emailError(`${response.rows[j].phone}${response.rows[j].carrier}`, `Your class ${response.rows[j].topic}- ${response.rows[j].class_num} does not exist in CUNYfirst anymore`);
                            } else {
                                text.twilio(`${response.rows[j].phone}`, `Uh oh! \n Your class ${response.rows[j].topic}- ${response.rows[j].class_num} does not exist in CUNYfirst anymore`);
                            }
                            db.query(`DELETE FROM classes WHERE phone='${response.rows[j].phone}' AND class_num=${response.rows[j].class_num}`, (err, res) => {
                                if (err) {
                                    console.error(`Error deleting from database: ${err}`);
                                }
                            });
                            continue;
                        }

                        if (status === 'Closed') {
                            console.log(`Class is still closed ${response.rows[j].topic}- ${response.rows[j].class_num}`);
                        } else if (status === 'Open') {
                            if (response.rows[j].carrier === '@tmomail.net' || response.rows[j].carrier === '@mymetropcs.com') {
                                text.emailOpen(`${response.rows[j].phone}${response.rows[j].carrier}`, `Your class ${response.rows[j].topic}- ${response.rows[j].class_num} is now open!`);
                            } else {
                                text.twilio(`${response.rows[j].phone}`, `Class Opened! \n Your class ${response.rows[j].topic}- ${response.rows[j].class_num} is now open!`);
                            }
                            db.query(`DELETE FROM classes WHERE phone='${response.rows[j].phone}' AND class_num=${response.rows[j].class_num}`, (err, res) => {
                                if (err) {
                                    console.error(`Error deleting from database: ${err}`);
                                }
                            });
                        }
                    }
                });
            }
        }
    });
};

setInterval(looper, 300000);

Well that explains because Google Cloud Platform uses virtual machines. 这很好解释了,因为Google Cloud Platform使用虚拟机。 Virtual machines want to perform like a bare metal machine. 虚拟机希望像裸机一样执行。 My next step would be to analyze your Cpu performance. 我的下一步是分析您的Cpu性能。 You can do it from opening two SSH windows (one launching the code, other monitoring) or by connecting from your remote. 您可以通过打开两个SSH窗口(一个启动代码,进行其他监视)或通过远程连接来实现。 From the Google Cloud you have tools like the Stackdriver monitoring (which would be more than suitable for this testing) and the Stackdriver login . 在Google Cloud中,您可以使用诸如Stackdriver监控(不仅仅适合此测试)和Stackdriver登录之类的工具 Also you have the serial port output from your instance. 另外,您还有实例的串行端口输出。 With this information you can make the changes to adapt this instance to yoru code. 使用此信息,您可以进行更改以使该实例适应yoru代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM