1 Using the cache mechanism and other performance related questions
1.1 Performance considerations
Since PHP is not a compiled language and the plot generated by this library require non-trivial work by PHP this must be seriously considered in the overall design for a web site. Generating complex graphs with many data-points is bound to take time. As a rough guideline most of the graphs demonstrated above take in the order of 1-2s to be generated and send back to a browser on a local network using a rather old PC with a slow disk as my local server (PII 166MHz). Experience shows that time spend is roughly 30% parsing the actual PHP code and 70% of the overall time depends on the complexity of the graphs. Hence no matter how simple graphs are you will have to face at least a standard hit to generate each graph.
This might be unacceptable in a high volume site. There is not much we can do about the complexity of the library if we want all this functionality and there is little we can do about the speed by which PHP parses the library.
However, we can do something. If you have non-real-time graphs that might only get new data, say every, 24h. Then it would be possible to generate the image and then save it in a file cache so that the next time a user requests this graph it is read from disk instead of generated by PHP. Every night we might then clear the cache and the first user whom requests the graph the next day will take the hit of actually generating it but the rest of the user will just be fetching the generated cached version.
The cache mechanism kicks in if you call the graph constructor with an additional file name as an additional parameter. One of two things will now happen
1. The file cache is searched for a file with this name. If the file exists it is read and passed through to the browser with very little overhead.
2. The file does not exist in the cache. In that case the graph is generated in the normal way but before it is passed back to the browser it is saved as a file in the cache
To use the cache your call could for example be
$graph = new Graph(300,200,”myfilename.png”)
Note that In the above example I used the extension “*.PNG” for the file. To use any specific extension is not necessary or any extension at all in fact.
For the cache to work you must have a directory called “jpgraph_cache” in the same place as you run your script from since the library will search for a directory called “./jpgraph_cache”. This directory must be readable and writeable for PHP. This scheme is not completely free from hassle from a security point of view. The other way would be to have a “global” cache directory but this increases the risk for a name clash and it might also not be possible if you are using some ISP that only allows you to create files within your own area.
The name of the directory used for caching might also be easily changed since it is defined in jpgraph.php as a
DEFINE(“CACHE_DIR”, “./jpgraph_cache”)
near the top of the file. You could for example change it to some general temp area (perhaps /var/tmp or similar) .
I’m not completely satisfied with the way this currently works since the cache directory in practice must be writeable for everyone and his uncle unless you can persuade the administrator to add you and PHP to the same group and then just make the directory writeable for member so that group. Any suggestions on how to better cope with this potential security whole are welcome!