doudouwd2017 2012-02-05 12:58
浏览 46
已采纳

Symfony - 响应中的图像 - 速度,优化

I generate dynamically graphic in my controller:

  1. if not file exists (unique filename) then generate graphic and save it

  2. return RedirectResponse($url)

(3. I've tried also:

    $fp = fopen($url, "rb");
    $str = stream_get_contents($fp);
    fclose($fp);

    $response = new Response($str, 200);
    $response->headers->set('Content-Type', 'image/png');
    return $response;

)

I need it to generate dynamically userbars for users [on this graphic some text will be automatically updating if some changes will be done on my websites].

So it's important for me that this script works fast and doesn't kill my server/bandwitch. To achieve that I've made cache where all generated graphics are stored. But i've got a problem with speed, solutions which i've described above are pretty slow, when i've tested it in chrome with developer tool (F12) then it shows me that both solution (2. and 3.) takes about ~1s (when the same graphics [without controllers etc. just direct url to .png] loads 44ms).

So if only for me it takes about ~1s per graphic then if for example 1000 users will generate at the same time this graphic it can slower my website I think.

Please help, do you know any faster ways to do that?

PS. Redirect isn't most desire thing for me, because I would prefer to leave url as it is, for example smth/username/userbar/ insted of smth/img/userbars/cache/blabla.png. But if it will be much faster then I can stand it :).

EDIT: To be clear: I'm not looking for solution to cache images, my problem is that returning images via controller (already generated images, just from ftp) takes about 1s, when normally if you do for example

           <img src="direct_url_to_png"> 

it takes about 44ms.

  • 写回答

1条回答 默认 最新

  • duanlu0559 2012-02-05 13:17
    关注

    You can use Varnish to cache the images, Varnish is a HTTP proxy that sits in front of the webserver. It takes some time & knowledge to configure it, but it is a very good piece of software.

    Alternatively, you may cache the images in memory using shared memory, memcache, or redis. This would probably be less effectly than Varnish, but it would save the filesystem IO.

    You should test where the most time is spent, I expect it's a combination of file I/O and PHP parsing time, but as we saying in Dutch, "Measuring is knowing" ;)

    Here's a simple example which should give you the general idea on how to do that:

    <?php
    class MyClass
    {
      function __construct()
      {
         $this->timer = array(
           'start' => microtime(True);
         );
       }
    
      function myAction()
      {
        $this->timer['startaction'] = microtime(True);
    
        $fp = fopen($url, "rb");
        $this->timer['startstream'] = microtime(True);
        $str = stream_get_contents($fp);
        fclose($fp);
        $this->timer['endstream'] = microtime(True);
    
        $response = new Response($str, 200);
        $response->headers->set('Content-Type', 'image/png');
    
        $this->timer['endaction'] = microtime(True);
        print_r($this->timer)
        return $response;
      }
    }
    

    Another thing that may be an option is to use HTML/CSS to show the graphs. This may or may not work depending in what your graphs look like but I've used this in the past and it worked quite well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?