behnampmdg3's avatar

How to test simple api endpoint to see if it can handle 100k or more calls (I dunno how many)

Hello;

I want to test an API endpoint under pressure.

This endpoint may receive up to 100k requests via API at one time.

I'm using transactions. I want to be able to test it before I go live.

I tested with 4k API calls and it all worked smoothly.

Senders may queue their calls but since this is open to different systems, I gotta be prepared for anything.

Thank you for tips

Here it the code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Data_receiver extends CI_Controller {

    public function __construct()
        {
            parent::__construct();
        }

    public function index()
        {

            $this->db->trans_start();
            //Validate Account Owner
            $sql = "SELECT * FROM launch_owners WHERE id = ?";
            $query = $this->db->query($sql, array($_POST['user_id']));
            if($query->num_rows()!=1)
                {
                    echo "Invalid Request";exit();
                }
            else
                {
                    $results = $query->result_array()[0];   
                    //More Validate Account Owner
                    if($results['status']!='active' || $results['secret_key']!=$_POST['secret_key'])
                        {
                            echo "Inactive Account Or Invalid Secret";exit();
                        }
                    else 
                        {
                            //Validate Launch
                            $sql = "SELECT * FROM launch_launches WHERE id = ? AND launch_type = ?";
                            $query = $this->db->query($sql, array($_POST['launch_id'], 'evergreen'));
                            if($query->num_rows()==1)
                                {
                                    //Does Prospect exist under this user?
                                    $sql = "SELECT * FROM launch_prospects WHERE email = ? AND owner_id = ?";
                                    $query = $this->db->query($sql, array($_POST['prospect_email'], $_POST['user_id']));
                                    if($query->num_rows()==1)
                                        {
                                            $prospect_id = $query->result_array()[0]['id'];
                                        }
                                    else 
                                        {
                                            $data = array(
                                                'email' => $_POST['prospect_email'],
                                                'owner_id' => $_POST['user_id'],
                                            );
                                            $this->db->insert('launch_prospects', $data);
                                            $prospect_id = $this->db->insert_id();
                                        }
                                    //Delete prospect from this launch if already exist in the seqeunce
                                    $this->db->delete('launch_launch_prospect', array('launch_id' => $_POST['launch_id'], 'prospect_id'=>$prospect_id)); 
                                    //Add Prospect To Launch
                                    $data = array(
                                                'launch_id' => $_POST['launch_id'],
                                                'prospect_id' => $prospect_id,
                                                'time_added' => time(),
                                                'date_added' => date('Y-m-d')
                                        );
                                    $this->db->insert('launch_launch_prospect', $data);

                                    //Insert the same into stats table
                                    $data = array(
                                                'launch_id' => $_POST['launch_id'],
                                                'prospect_id' => $prospect_id,
                                                'time_added' => time(),
                                                'date_added' => date('Y-m-d'),
                                                'source' => $_POST['source']
                                        );
                                    $this->db->insert('launch_launch_prospect_history', $data);
                                    echo "Added Successfully";
                                }
                            else 
                                {
                                    echo "Invalid launch";exit();
                                }   
                        }   
                }   



        $this->db->trans_complete();        

        }
}
0 likes
2 replies

Please or to participate in this conversation.