简体   繁体   中英

CodeIgniter incredibly slow on small page load

So i am experiencing a problem I should probably only be having on big pages but instead im getting that on ANY page.

I am trying to make a REST api using CodeIgniter and it works but any page I load is very very slow. I am talking 20-30 seconds page loads for a page worth 504 Bytes.

I've added extra benchmarks and it displays it takes roughly 0.04 sec to download the data.

Rankings.php (Controller)

class Rankings extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('rankingsmodel');
    }

    /**
     * Index Page for this controller.
     */
    public function index()
    {
        $this->benchmark->mark('code_start');

        $rankings =$this->rankingsmodel->getAllRankings();
        $mappedRankings = array_map(array($this, 'mapRank'), $rankings, array_keys($rankings));

        $this->benchmark->mark('code_end');
        echo $this->benchmark->elapsed_time('code_start', 'code_end');

        return $this->jsonIFy($mappedRankings);
    }

    public function mapRank($ranking, $k)
    {
        $ranking['rank'] = $k;
        return $ranking;
    }

    public function jsonIFy(array $data = array())
    {
        echo json_encode($data);
    }
}

And my rankingsmodel

class RankingsModel extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getAllRankings()
    {
        $this->benchmark->mark('code_start2');

        $query =$this->db->query('SELECT * FROM characters3 WHERE gm < 3 ORDER BY rebirths DESC')->result_array();

        $this->benchmark->mark('code_end2');
        echo $this->benchmark->elapsed_time('code_start2', 'code_end2');
        return $query;
    }
}

I have absolutely no idea why page loads so incredibly slow while my benchmarks are saying it would take 0.03/0.04 to load. I've tried setting up xdebug but i've not had any luck setting it up. Im using regular mysqli drivers that come with codeigniter.

Does anyone have any idea why it is so slow or any tool i can setup easily to debug?

Thank you

Okay so after a few more hours of digging i have found the problem.

The problem is the following:

My database was getting retrieved very slowly (we have a demo on db4free or smth) and that is what was taking so long. We also had $db['default']['pconnect'] set to false which meant we had to constantly reconnect to the database.

For testing purposes setting pconnect to true will solve the issue. And oh: get a faster database for production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM