为什么代码在本地运行良好,但在 Google App Engine 灵活环境上需要更长的时间?

问题描述 投票:0回答:1

我有这个 Node.JS 应用程序。我从这个基础制作了它的 docker 镜像:https://hub.docker.com/r/bcgovimages/alpine-node-libreoffice 当我通过在本地运行图像并将其部署在 Google Cloud App Engine (GAE) 灵活环境上来比较性能时,发现此功能没有太大差异:

const getWordDocument = async (req, res) => {

    try {

        const doc = new Document(); // This is a Word Document created using docx npm library. I omitted previous code for readability.

        const filename = uuidv4();

        // Saving in Docx format
        await Packer.toBuffer(doc).then((buffer) => {
            fs.writeFileSync(`./assets/${filename}.docx`, buffer);
        });

        const environment = process.env.NODE_SERVER_ENV || "PROD";

        // Executing libreoffice for .docx to .pdf conversion

        exec(`${environment == "PROD" ? "libreoffice" : "soffice"} --headless --convert-to pdf ./assets/${filename}.docx --outdir ./assets/`, (error, stdout, stderr) => {
            if (error) {
                console.error(`Error converting file: ${error}`);
                return;
            }
            // Uploading docs to Storage Bucket and sending their URLs back in response.
            uploadDocs(filename)
                .then((urls) => {
                    res.json({ urls: urls });
                })
                .catch((err) => {
                    console.error('Error uploading files:', error);
                    res.status(500).send('Error uploading files');
                })
        });

    } catch (error) {
        console.error('Error retrieving file:', error);
        res.status(500).send('Error retrieving file');
    }

};


async function uploadDocs(filename) {
    try {
        const bucketName = 'blah-blah-blah';
        const folderName = 'purchase';
        const uniqueIdentifier = uuidv4();
        const objectNameDocx = `${folderName}/${uniqueIdentifier}/Document.docx`;
        const objectNamePdf = `${folderName}/${uniqueIdentifier}/Document.pdf`;

        const bucket = storage.bucket(bucketName);

        await Promise.all([
            new Promise((resolve, reject) => {
                const docxFile = bucket.file(objectNameDocx);
                const docxStream = docxFile.createWriteStream({
                    metadata: {
                        contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                    }
                });
                fs.createReadStream(`./assets/${filename}.docx`)
                    .pipe(docxStream)
                    .on('error', reject)
                    .on('finish', resolve);
            }),
            new Promise((resolve, reject) => {
                const pdfFile = bucket.file(objectNamePdf);
                const pdfStream = pdfFile.createWriteStream({
                    metadata: {
                        contentType: 'application/pdf'
                    }
                });
                fs.createReadStream(`./assets/${filename}.pdf`)
                    .pipe(pdfStream)
                    .on('error', reject)
                    .on('finish', resolve);
            })
        ]);

        fs.unlinkSync(`./assets/${filename}.docx`);
        fs.unlinkSync(`./assets/${filename}.pdf`);

        const docxSignedUrl = await bucket.file(objectNameDocx).getSignedUrl({
            action: 'read',
            expires: Date.now() + 60 * 60 * 1000,
        });

        const pdfSignedUrl = await bucket.file(objectNamePdf).getSignedUrl({
            action: 'read',
            expires: Date.now() + 60 * 60 * 1000,
        });

        return [...docxSignedUrl, ...pdfSignedUrl];
    } catch (error) {
        console.error('Error uploading files:', error);
        throw error;
    }
}

根据我的观察,“使用 libreoffice 将 .docx 转换为 .pdf”这一特定部分花费的时间要长得多。 Exec 命令可能是罪魁祸首。因为应用程序的其他部分(与存储桶、API 等交互)运行良好,没有延迟。

使用此功能时,整个操作需要:

  • 本地环境:10-12秒
  • 本地 Docker 镜像:10-12 秒
  • 将 Docker 镜像部署到 GAE:2-3 分钟

我的笔记本电脑是 8GB RAM,英特尔 i5 第 12 代,所以我认为它不够快。我在 Google Cloud 上有免费配额,所以也许我没有获得太多资源?有人可以指出这个问题吗?

这是 dockerfile:

FROM bcgovimages/alpine-node-libreoffice:latest

WORKDIR /app

COPY package*.json ./

RUN npm install

# Copy the rest of your application code
COPY . .

# Expose the port your app runs on
EXPOSE 8080

# Command to run your application
CMD ["node", "index.js"]

app.yaml:

runtime: custom
env: flex

entrypoint: node index.js
handlers:
  - url: /.*
    script: auto
    secure: always
env_variables:
  NODE_SERVER_ENV: "PROD"

我必须在服务器上转换文档,因为我无法在 MS Word 文档中嵌入字体,在这里转换为 PDF 有助于保留字体。

谢谢你。

node.js google-cloud-platform google-app-engine dockerfile app-engine-flexible
1个回答
0
投票

更改

app.yaml
并声明
cpu
memory_bg
值。然后重新测试。

从 cpu = 2 和 memory_gb = 2 开始。根据需要增加。

© www.soinside.com 2019 - 2024. All rights reserved.