Skip to content

Instantly share code, notes, and snippets.

View kuntalchandra's full-sized avatar
🎯
Focusing

Kuntal Chandra kuntalchandra

🎯
Focusing
View GitHub Profile
@kuntalchandra
kuntalchandra / trap-error
Created August 23, 2017 11:51
bash: Trap error and exit
#!/bin/bash
trap "exit 1" TERM
TOP_PID=$$
function alert()
{
echo "Do something"
kill -s TERM $TOP_PID
}
@kuntalchandra
kuntalchandra / client.py
Last active March 11, 2021 12:21
Python socket programming: server-client design.
import socket
import sys
def main():
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 8888
try:
soc.connect((host, port))
@kuntalchandra
kuntalchandra / check-premium.bash
Created September 11, 2017 12:42
Basic program to send desktop notifications in Ubuntu
#!/bin/bash
COUNT=`php /etc/checkDuePremium.php`
if [ $COUNT -gt 0 ]
then
notify-send 'Hello Kuntal!' "$COUNT premium is due. Check your inbox for details" --icon=dialog-information
echo insurance reminder job ran at `date` >> /var/log/custom.log
fi
@kuntalchandra
kuntalchandra / mysqlCsvConverter.php
Created September 11, 2017 13:18
MySQL to CSV Converter
<?php
$exportData = [
'db0' => ['table0', 'table1'],
'db1' => ['table0']
];
$user = 'test';
$password = 'test123';
$host = '127.0.0.1';
@kuntalchandra
kuntalchandra / branchPredictionPerformance.php
Created September 12, 2017 08:42
Why if/else block should be avoided in loop, if possible.
<?php
$sortedSet = range(1, 10000000);
$randomSet = $sortedSet;
shuffle($randomSet);
$cnt = count($sortedSet);
$filter = $cnt / 2;
$sortedSetChunks = array_chunk($sortedSet, $filter);
$randomSetChunks = array_chunk($randomSet, $filter);
@kuntalchandra
kuntalchandra / referenceTest.php
Created September 14, 2017 13:19
How variable reference works in PHP: Deep or shallow copy
<?php
//example of shallow copy
$arr = array(1, 2);
$arr1 = &$arr;
$arr1[0]++;
echo "\nCopied by reference\n";
echo $arr[0], "\n";
echo $arr1[0], "\n";
@kuntalchandra
kuntalchandra / ImageResizerMultiProcess.php
Created September 14, 2017 13:34
Forking multiple processes in PHP using pcntl_fork()
<?php
include("Lib.php");
class ImageResizerMultiProcess
{
private $sizes = [];
public function setSizes($sizes)
{
@kuntalchandra
kuntalchandra / image_resizer.py
Last active June 4, 2019 20:24
Parallel processing using multiprocessing and multithreading
import concurrent.futures
from PIL import Image
from resizeimage import resizeimage
from os import listdir, getpid
from os.path import isfile, join
SOURCE_DIR = '/var/tmp/source/'
TARGET_DIR = '/var/tmp/target/'
MAX_PROCESSES = 4
MAX_THREADS = 2
@kuntalchandra
kuntalchandra / convert-mkv-avi.sh
Last active January 12, 2018 05:20
Convert mkv file to avi file. Applicable to a list of files. Uses xargs for parallel processing.
#!/bin/bash
shopt -s nullglob
source_files=(/home/kuntal/Videos/Tom.and.Jerry.The.Golden.Collection.Volume.One.720p.BluRay/*.mkv)
convert() {
source_file=$(echo $0 | cut -f1 -d:)
target_file=$(echo $0 | cut -f2 -d:)
echo "source file: $source_file"
ffmpeg -i $source_file $target_file
echo "converted file: $target_file"
@kuntalchandra
kuntalchandra / cmd.sh
Last active August 1, 2022 05:53
Load testing example using Apache Bench (ab) to POST JSON to an API
# Shoot off 300 requests at the server, with a concurrency level of 10, to test the number of requests it can handle per second
# -p POST
# -H Authentication headers
# -T Content-Type
# -c Concurrent clients
# -n Number of requests to run
# -l Accept variable document length
# -k Connection keep-alive
# -v Verbosity level