google-cloud-node icon indicating copy to clipboard operation
google-cloud-node copied to clipboard

High CPU usage with Cloud Storage

Open mikamaunula opened this issue 9 years ago • 4 comments

Hi,

We are using Cloud storage to store small images. There are average one or two images(~75KB) posted per second.

When we update the latest Cloud Storage module we see that CPU usage goes up after a time.

grab

Using older version of [email protected] bundle CPU usage is fine.

Environment details

  • OS: Debian GNU/Linux 8 (jessie)
  • Node.js version: v6.10.3
  • npm version: 3.10.10
  • @google-cloud/[email protected]

Steps to reproduce

Code is pretty much the same as in the documentation. (https://cloud.google.com/appengine/docs/flexible/nodejs/using-cloud-storage)

'use strict';

const format = require('util').format;
const express = require('express');
const Multer = require('multer');

const Storage = require('@google-cloud/storage');

const storage = Storage();

const app = express();
app.set('view engine', 'pug');

const multer = Multer({
  storage: Multer.memoryStorage(),
  limits: {
    fileSize: 10 * 1024 * 1024
  }
});

app.post('/upload', multer.single('file'), (req, res, next) => {
  if (!req.file) {
    res.status(400).send('No file uploaded.');
    return;
  }

  const blob = bucket.file(req.file.originalname);
  const blobStream = blob.createWriteStream();

  blobStream.on('error', (err) => {
    next(err);
    return;
  });

  blobStream.on('finish', () => {
    res.status(200).send('ok');
  });

  blobStream.end(req.file.buffer);
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {});

There seems to be related issue https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1943 but it seems to be closed.

mikamaunula avatar Jun 21 '17 06:06 mikamaunula

One thing that should help is disabling resumable uploads. They are enabled by default with write streams. To disable:

-const blobStream = blob.createWriteStream();
+const blobStream = blob.createWriteStream({ resumable: false });

If the problem persists, would it be possible for you to supply a test script that doesn't have any external dependencies, and simulates as many requests as it takes to reproduce the issue?

Thanks for reporting the issue, and sorry for the trouble.

stephenplusplus avatar Jun 21 '17 12:06 stephenplusplus

Setting resumable to false worked. CPU usage is now in normal.

Big thanks!

mikamaunula avatar Jun 22 '17 06:06 mikamaunula

For those of us who'd like to use resumable uploads, should we still be cautious about high CPU usage with the latest version?

sabrehagen avatar Mar 27 '18 09:03 sabrehagen

We recommend disabling resumable uploads when you need to transmit several small files in a short amount of time. In those scenarios, there isn't a benefit to a resumable upload, and the small performance penalty of a single resumable upload will be compounded and noticeable.

stephenplusplus avatar Mar 27 '18 12:03 stephenplusplus