2

I'm using express js as a backend.

Each user can upload 3/5 files. and the admin can download the files from the dashboard as zip file.

I'm using ADM-ZIP to zip the files and download the zip.

 const zip = new AdmZip();
   const uploadDir = await fsPromises.readdir("./static/uploads/applications/"+user.id);

  for(var i = 0; i < uploadDir.length;i++){
    zip.addLocalFile('./static/uploads/applications/'+user.id+'/'+uploadDir[i]);
  }
 
  const downloadName = `${user.name}.zip`;

  const data = await zip.toBuffer();

  res.set('Content-Type','application/octet-stream');
  res.set('Content-Disposition',`attachment; filename=${downloadName}`);
  res.set('Content-Length',data.length);
 return  res.send(data);

the code works fine. but I noticed there is high CPU usage when pressing download.

is this normal to happen in express? is this approach good for zipping multiple files and downloading them?

I'm new to express so I'm a bit worried about having problems taking this approach.

  • 2
    Can you show us some stats of CPU-usage before and while/on downloading? – iLuvLogix Dec 23 '21 at 14:51
  • @iLuvLogix How do I do that? is there any built function to this ? – Chinese_hacker_lm Dec 23 '21 at 15:11
  • How big are those files? What operating system are you on? Did you try to zip those files by accessing the folder from your command line and using a compression utility? Does the CPU usage go up like when you do it with your app? – Johnny Dec 23 '21 at 15:22
  • @Johnny yes on the task manager it goes from 8% to 16% 25%. maximum file upload is 5 MB .. max files on one zip file are 5 . I use windows 10 64bit. and I haven't tried to use compression utility – Chinese_hacker_lm Dec 23 '21 at 15:25
  • _" 8% to 16% 25%. maximum"_ doesn't sound tremendously unacceptable - What chip-set are you running this on? – iLuvLogix Dec 23 '21 at 15:31
  • @iLuvLogix I'm using amd ryzen 3 2200g – Chinese_hacker_lm Dec 23 '21 at 15:34
  • Ok - that compares to the higher i3 chips from Intel.. What I would do is compare following two tests: Once download the file via the app and once dowload it via wget from the command line and compare cpu usage – iLuvLogix Dec 23 '21 at 15:39
  • @iLuvLogix using wget it gets to from 3% to 10% .. in the app it gets from 3-5% to 20% 19% .. :\ – Chinese_hacker_lm Dec 23 '21 at 15:55
  • I see in your code that you are (on pressing the upload) reading the dir and then compressing all the files and then dowload the archive once done - that obviously takes more processing power than just downloading the archive via wget from CLI. What comes to my mind in order to reduce the CPU-usage is to generate a new archive as soon as someone uploads a file (including all the other files in the upload-dir) so you have your zip ready and only dowload the buffred zip - this way your CPU-usage should drop a bit since zipping is excluded - is that an option? – iLuvLogix Dec 23 '21 at 16:04
  • Do you mean while the user is uploading it should create another zip file with the files and add it to the dir ? .. humm wouldn't that make the CPU usage higher ?? i mean I will try and compare the CPU usage – Chinese_hacker_lm Dec 23 '21 at 16:37

0 Answers0