首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker映像在云运行上比运行本地的要慢得多

Docker映像在云运行上比运行本地的要慢得多
EN

Stack Overflow用户
提问于 2022-05-31 09:03:18
回答 1查看 253关注 0票数 0

我有一个Docker映像,应该使用.docx节点包将libreoffice-convert文件转换为.pdf。我想使用gRPC与服务进行通信。

在本地构建和运行映像时,执行和返回响应大约需要5-6秒,而在云上运行时则至少需要一分钟或更长时间。

我已经尝试过运行修改,总是分配CPU,并且至少有一个最小的实例,但是在响应时间方面没有什么真正的改变。

我使用1 vCPU和512 80内存运行修订,请求超时为300秒,每个容器的最小请求设置为80,使用默认的执行环境。

如何调试此问题?我读过关于运行后台活动的文章,但是让CPU始终分配应该解决了这个问题,相反,时间几乎是相同的。

Dockerfile:

代码语言:javascript
复制
FROM node:16-alpine

RUN apk update && apk add libreoffice
RUN apk add --no-cache msttcorefonts-installer fontconfig
RUN update-ms-fonts

# Google fonts
RUN wget https://github.com/google/fonts/archive/main.tar.gz -O gf.tar.gz && \
    tar -xf gf.tar.gz && \
    mkdir -p /usr/share/fonts/truetype/google-fonts && \
    find $PWD/fonts-main/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || return 1 && \
    rm -f gf.tar.gz && \
    # Remove the extracted fonts directory
    rm -rf $PWD/fonts-main && \
    # Remove the following line if you're installing more applications after this RUN command and you have errors while installing them
    rm -rf /var/cache/* && \
    fc-cache -f

ENV DIR /app
WORKDIR ${DIR}
COPY . ${DIR}
RUN npm install
RUN npm rebuild grpc
ENV TZ Europe/Rome

CMD [ "node", "index.js" ]

index.js

代码语言:javascript
复制
const grpc = require("grpc");
const protoLoader = require('@grpc/proto-loader');
const fs = require('fs');
const libre = require('libreoffice-convert');
const tmp = require('tmp');

const reportGenProto = protoLoader.loadSync("reportGen.proto");
const packageObject = grpc.loadPackageDefinition(reportGenProto);
const server = new grpc.Server();
server.addService(packageObject.ReportGeneratorService.service, {
    Ping: async function (call, callback) {
        console.log("PONG");
        return callback(null, {
            pong: "pong"
        });
    },
    Generate: async function (call, callback) {
        try {
            const template = Buffer.from(call.request.templateFile, 'base64');

                const tmpFile = tmp.fileSync({ postfix: '.docx' });
                fs.writeFileSync(tmpFile.name, template);

                console.log('Saved temp file to', tmpFile.name);

                libre.convertAsync = require('util').promisify(libre.convert);

                const ext = '.pdf'

                console.log('Converting to', ext);

                let pdfBuf = await libre.convertAsync(doc, ext, undefined);

                console.log('Conversion complete');

                callback(null, {
                    content: Buffer.from(pdfBuf).toString('base64')
                });
        } catch (err) {
            console.error(err)
            callback({ code: grpc.status.INTERNAL, message: err.toString() });
        }
    }
});

server.bind(
    `0.0.0.0:${process.env.PORT}`,
    grpc.ServerCredentials.createInsecure()
);
console.log(`Server started on PORT ${process.env.PORT}`);
server.start();
代码语言:javascript
复制
syntax = "proto3";

service ReportGeneratorService {
  rpc Generate(GenerateForm) returns (Report){}
  rpc Ping (Empty) returns (Pong) {}
}

message Pong {
  string pong = 1;
}

message Report {
  string content = 1;
}

message GenerateForm {
  string templateFile = 1;
}

message Empty {}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-01 12:46:23

事实证明,主要的瓶颈是libreoffice转换。

当使用libreoffice --headless命令进行转换时,软件必须启动,加载到内存中,并在每次请求时退出。对于云运行性能来说,这似乎是一项繁重的任务。

有一些可以随时使用的服务,如Unoserver戈滕贝格,它们使用LibreOffice的侦听器模式以某种方式解决了这个问题。对于我的用例来说,这已经足够了,我没有从我的gRPC服务器(它做的是转换成PDF之外的其他事情)做我需要的所有事情,而是用Gotenberg的Cloud映像设置了另一个云运行实例,我将在我的服务器中调用它的API。

响应时间(包括我的服务器正在做的其他事情)现在不到5秒。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72444976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档