pdalcol / Zippex

Native Apex Zip library for Salesforce.com
MIT License
143 stars 108 forks source link

Apex CPU governor impact? #16

Open afawcett opened 8 years ago

afawcett commented 8 years ago

This is very interesting indeed! Have you done any analysis on the impact it has on the Apex CPU Governor?

Savier9 commented 8 years ago

Hi there. Same issue. I it caused when getFile(filename) is called.

If the zip has like 20Kb, it will pass through, though really slow. 100kb (not so much I gues) zip just wont pass with CPU limit reached error.

34kb zip barely made it. 55Kb wont. Files inside the zip are just small images.

qdriscoll commented 7 years ago

Hello, I'm running up against the max string length of 6000000 when generating .zip file. Is it possible to get around this issue? Thanks!

pdalcol commented 7 years ago

@qdriscoll, I think you see that error because you are hitting the Apex heap size limit but there might be a higher limit on strings regardless of heap size. I recommend you clear large variables after you are done using them. For example after you add a file to a Zip archive you can set that variable to null.

Zippex sampleZip = new Zippex();
Blob fileData = Blob.valueOf('Sample text.');
sampleZip.addFile('sampleFolder/test.txt', fileData, null);
fileData = null; //REDUCE HEAP SIZE
Blob zipData = sampleZip.getZipArchive();

If that is not enough, you can also run your code asynchronously which will double the available heap size. And for even larger heap size you can use @future(limits='3xHeap').

pdalcol commented 7 years ago

@Savier9 it is extremely CPU intensive to inflate compressed Zip files. We are trying to add a few new optimizations to be able to handle larger files. @xoob has some ideas here: https://github.com/pdalcol/Zippex/pull/20 that we will be implementing soon.

In the meantime use asynchronous mode like queueable, batch or future to get a larger CPU limit. In future mode you can also use @future(limits='3xCPU') to get even larger limits. The simplest way to use asynchronous mode is by calling Zippex.unzipAttachment() and passing true in the fourth parameter.

dineshkumar1603 commented 7 years ago

We made the execution asynchronous and now we were able to unzipAttachement zip file successfully for file having less than 300kb per file inside Zip File, more than 300kb leads to CPU limit. @future(limits='3xCPU'). We are in need of processing file which is nearly 1Mb file inside Zip file. Please do provide us workaround for resolving CPU Time limit.

TheAmigo commented 3 years ago

For creating Zip files, one solution would be to allow setting the compression mode to "store". Such archives would be larger, but creating them will use far less CPU time. Ideally, this would be an optional 4th argument to addFile(). If missing, it would behave as it does now, but specifying "store" would add that file to the archive without trying to compress it.

One practical application is when creating .xlsx files. I'd gladly save some CPU time to create a larger file.