Skip to content

Instantly share code, notes, and snippets.

@WahidinAji
Last active March 30, 2024 04:21
Show Gist options
  • Save WahidinAji/7a0ca9b2c139e0b064de0e7ea522742c to your computer and use it in GitHub Desktop.
Save WahidinAji/7a0ca9b2c139e0b064de0e7ea522742c to your computer and use it in GitHub Desktop.
anu php

how to run?

php -S localhost:5000

result of k6

  • k6s scripts
export default function () {
    // Get homepage
    let response;
    response = http.get("http://localhost:5000");
    sleep(1)
}
  • result
image
  • scripts
import http from "k6/http";

export const options = {
    scenarios: {
        default: {
            executor: "shared-iterations",
            gracefulStop: "5s",
            vus: 10,
            iterations: 1000,
            maxDuration: "30s",
        },
        constant: {
            executor: "constant-vus",
            vus: 20,
            duration: "5m",
            startTime: "40s",
            gracefulStop: "10s",
        },
        ramping: {
            executor: "ramping-vus",
            stages: [
                { target: 2, duration: "1s" },
                { target: 10, duration: "20s" },
                { target: 20, duration: "20s" },
                { target: 30, duration: "1m" },
                { target: 40, duration: "1m" },
                { target: 50, duration: "1m" },
            ],
            startTime: "6m",
            gracefulStop: "20s",
        },
    },
};

export default function () {
    http.get("http://localhost:5000");
}
  • result image
<?php
class Database
{
private static $pgInstance = null;
private \PDO $pdo;
public function __construct($host, $port, $dbname, $user, $password)
{
$dsn = sprintf("pgsql:host=%s;port=%d;dbname=%s", $host, $port, $dbname);
try {
$this->pdo = new \PDO($dsn, $user, $password);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
}
//definitely this function's unused
public static function getInstance($pg_conn_string): ?Database
{
if (self::$pgInstance === null) {
self::$pgInstance = new Database($pg_conn_string);
}
return self::$pgInstance;
}
public function connection(): PDO
{
return $this->pdo;
}
}
<?php
require 'Database.php';
require 'UserComment.php';
$host = "localhost";
$port = 5432;
$dbName = "invitation";
$dbUser = "postgres";
$dbPassword = "password";
$newDb = new Database($host, $port, $dbName, $dbUser, $dbPassword);
try {
$userComments = new UserComment($newDb->connection());
$query = $userComments->getComments();
$response = [
'status' => 'success',
'data' => $query
];
} catch (Exception $e) {
$response = [
'status' => 'error',
'message' => $e->getMessage()
];
}
header('Content-Type: application/json; charset=utf-8', true, 200);
echo json_encode($response);
import { sleep } from "k6";
import http from "k6/http";
export default function () {
// // Get homepage
let response;
response = http.get("http://localhost:5000");
sleep(1)
}
<?php
class UserComment
{
private \PDO $db;
/**
* @throws Exception
*/
public function __construct($db)
{
if (!$db) {
throw new \Exception("Database connection error");
}
$this->db = $db;
}
public function getComments(): bool|array
{
return $this->db->query("SELECT * FROM user_comments")->fetchAll(\PDO::FETCH_ASSOC);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment