Monday, January 21, 2019

When we need to use swap partition in Linux?

Recently i have an issue on my LightSail small instance which have 512MB of RAM,

I can not run react-script build on it

npm run build
Because of the issue: https://github.com/kitze/custom-react-scripts/blob/master/packages/react-scripts/template/README.md#npm-run-build-exits-too-early

So create a Linux swap is life saver.

Firstly, check if swap available :

sudo swapon --show
if it show nothing, then you don't have any swap yet.

Now, create a swap file, it should be 2x or 4x of your RAM capacity
sudo fallocate -l 1G /swapfile

To verify the swap is created
ls -lh /swapfile
To enable swap, you may need to change the permission

sudo chmod 600 /swapfile
make swap file
          sudo mkswap /swapfile        
then
sudo swapon /swapfile
check if it is ready
sudo swapon --show
 To enable swap permanent

Backup file
sudo cp /etc/fstab /etc/fstab.bak
Add the line at the end of file

/swapfile swap swap defaults 0 0

Sunday, January 6, 2019

Benchmarking web application tools



1. Apache benchmarking 

Home page https://httpd.apache.org/docs/2.4/programs/ab.html

  • c: ("Concurrency"). Indicates how many clients (people/users) will be hitting the site at the same time. While ab runs, there will be -c clients hitting the site. This is what actually decides the amount of stress your site will suffer during the benchmark.
  • n: Indicates how many requests are going to be made. This just decides the length of the benchmark. A high -n value with a -c value that your server can support is a good idea to ensure that things don't break under sustained stress: it's not the same to support stress for 5 seconds than for 5 hours.
  • k: This does the "KeepAlive" functionality browsers do by nature. You don't need to pass a value for -k as it it "boolean" (meaning: it indicates that you desire for your test to use the Keep Alive header from HTTP and sustain the connection). Since browsers do this and you're likely to want to simulate the stress and flow that your site will have from browsers, it is recommended you do a benchmark with this.
    The final argument is simply the host. By default it will hit http:// protocol if you don't specify it.
ab -k -c 350 -n 20000 example.com/ 

By issuing the command above, you will be hitting http://example.com/ with 350 simultaneous connections until 20 thousand requests are met. It will be done using the keep alive header.


E.g: Get user profile

ab -n2000 -c10 -k -m GET -T "application/json" -H "${accessToken}" http://localhost:8080/api/v1/users/profile


2. Apache Jmeter



Create many users using __UUID() random method example:






References:




Saturday, January 5, 2019

MacOS basic commands

1. Copy a folder into another folder


cp -r /path/folder/a /path/folder/b

result:
/path/folder/b
/path/folder/b/a

2. Move a folder into another folder


mv /path/folder/a /path/folder/b

result:
/path/folder/b
/path/folder/b/a

3. Remove files


rm -rf /path/to/file
rm -rf /path/to/file*

4. Process and ports


lsof -iTCP -sTCP:LISTEN -n -P
lsof -iTCP -sTCP:LISTEN -n -P | grep {PORT}

5. Kill a process


kill -9 ${PID}

6. Change owner of a folder


# for current user
sudo chown -R `id -un` /path/to/folder
# a user
sudo chown -R ${username} /path/to/folder

MongoDB basic commands

1. Setup MongoDB

  • Download: https://www.mongodb.com/download-center/community

  • Extract folder
    tar -zxvf mongodb-osx-x86_64-xx.tgz -C /path/to/container/folder

  • Add to $PATH
    Find out which current shell you are working on.
    zsh open ~/.zshrc
    bash open ~/.bashrc
    add lines at the end of the file.
    export MONGODB_HOME=~/Workspace/mongodb-osx-x86_64-xx/
    export PATH=$MONGODB_HOME/bin:$PATH

    Reload the environment variables for current shell
    # e.g
    source ~/.zshrc

  • Create the directory where MongoDB stores data
    sudo mkdir /data/db
    sudo chown -R `id -un` /data/db

  • Start MongoDB
    mongod

    For now , you have a mongodb server is running on default port 2017 without authentication, the data is stored on /data/db
2. Basic client commands

# connect to server
mongo

# list databases;
show databases;

# use a database
use ${db_name}

# list collections;
show collections;

# form now we can perform collection methods
db.${collectionName}.${command}

find({})
findOne()
find({"_id": new IdObject("${docId}")})
find({ <field>: { $regex: /pattern/, $options: '<options>' }})
deleteMany({})
deleteOne({})
remove({})
drop()

Checkout collection methods

2.Delete database

# from commandline
mongo ${dbName} --eval "db.dropDatabase()"

# inside mongoClient
use ${dbName}
db.dropDatabase()