Apachebench Tutorial ab testing Tool Example in Ubuntu/Linux

Official Apache Page

Is there a time when you are thinking how to do a server load testing to evaluate your PHP web application performance? This is where you will be using the Apachebench or “ab” testing tool. This is a very useful open source tool not only to PHP developers but to the entire users of the Apache server. This tool is used to evaluate the execution times of your PHP web application or the response time of your website under “stressing” conditions. This condition means how your PHP web application will perform under conditions similar to what exactly will be happening on a real web server not just on your local host environment (such as XAMPP). For example in actual website deployment in a production server; it will be subjected to two stressing conditions:

a.) Number of connections to that application – these are the number of visitors to your website which are connected to that application. The higher the number of visitors to that application, the higher also the number of connections. This is a stressing condition because sometimes if your PHP web application in your website is poorly written, it will result to a very poor performance in actual deployment which can slowed down if there are a lot of users in your website.

b.) Number of concurrent users – these are the actual number of users that are using the application at exactly the same time. Of course in a very low traffic website, the concurrent users is almost zero because the probability that two users will exactly hit the application and do the same request is very small. This can be an issue with high traffic websites; it is because the number of concurrent users can slowed down the application and put a signficant load on the web server. Some web host even limits the number of concurrent users (typically those that are originating from a free web hosting scheme).

The good thing with Apachebench is that you measure the performance of a certain web application or website given a stress conditions. The stressing conditions required by Apache bench are two above. What is expected to happen is that a poorly written PHP web application will perform poorly under stressing conditions. This is why performance benchmarks of CMS, PHP frameworks, etc are using ab testing to evaluate the quality of the application.

How to Get Started with AB testing tool in Linux/Ubuntu:

Below are the steps :

1.) In Ubuntu, Apachebench can be installed using the Apache 2 utils. Go to Applications – Accessories – Terminal, at the command prompt type:

sudo apt-get install apache2-utils

2.) After installation, you are now ready to use Apachebench. Lets have an example. Supposing you will need to know how wordpress.org performs when there are 300 connections and 3 concurrent users. First, this is the syntax of ab:

ab -n [number of connections] -c [number of concurrent users] [url]

3.) Using the syntax above for ab test, below is the final command to type in the shell prompt:

ab -n 300 -c 3 https://wordpress.org/

4.) Below are the results, you should see in your terminal:

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, https://www.zeustech.net/
Licensed to The Apache Software Foundation, https://www.apache.org/

Benchmarking wordpress.org (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Finished 300 requests


Server Software:        nginx
Server Hostname:        wordpress.org
Server Port:            80

Document Path:          /
Document Length:        9172 bytes

Concurrency Level:      3
Time taken for tests:   72.917 seconds
Complete requests:      300
Failed requests:        296
   (Connect: 0, Receive: 0, Length: 296, Exceptions: 0)
Write errors:           0
Total transferred:      2789682 bytes
HTML transferred:       2743782 bytes
Requests per second:    4.11 [#/sec] (mean)
Time per request:       729.172 [ms] (mean)
Time per request:       243.057 [ms] (mean, across all concurrent requests)
Transfer rate:          37.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      214  225   6.8    224     253
Processing:   448  502 177.6    486    3481
Waiting:      221  252 175.9    237    3232
Total:        670  728 179.0    710    3722

Percentage of the requests served within a certain time (ms)
  50%    710
  66%    721
  75%    728
  80%    735
  90%    751
  95%    771
  98%    818
  99%    931
 100%   3722 (longest request)

Very important interpretation of results:

1.) It means it takes 3722ms for wordpress.org to serve longest request under a stressing conditions of 300 connections and 3 concurrent users. This is the longest response time.

2.) But on the “average” it will only take wordpress.org to respond within 728ms +- tolerance of 179 ms.

A more powerful and faster website can respond to faster times even with more stressing conditions. If you are using Windows, you can read this Apachebench tutorial using Windows OS.

Warning: Do not use Apachebench excessively to make enormous request to a web server that you do not own. This is a form of denial of service attack and can put you to jail: https://www.pcworld.com/businesscenter/article/181127/ex_ceo_charged_in_denial_of_service_attack.html

Excerpt custom function – character length

This code will limit the excerpt to the desired number of character, without truncating the last word. The function –

function get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
  $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  return $excerpt;
}

Echoing the excerpt

<?php echo get_excerpt(125); ?>

Display Posts From Single Category in a page

 

                                                                                                            <?php
                                                                                                               // The Query
                                                                                                               query_posts( array ( 'category_name' => 'uncategorized', 'posts_per_page' => 4 ) );
                                                                                                               ?>
                           
                                                                                            <?php while ( have_posts() ) : the_post();
                                                                                                                       echo '<h1>';
                                                                                                                       the_title();
                                                                                                                       echo '</h1>';
                                                                                                                       the_excerpt();
                                                                                                                       echo get_the_time( 'j.n.Y'); 
                                                                                                                       echo ' <p class="postinfo">Written by: ';
                                                                                                                       the_author_posts_link();
                                                                                                                       echo 'Posted on ';
                                                                                                                       the_date();
                                                                                                                       echo 'Categories: ';
                                                                                                                       the_category(', ');
                                                                                                                       echo '</p>';
                                                                                                                       endwhile; ?>

                                                                                                                         <?php

                                                                                                                         // Reset Query
                                                                                                                         wp_reset_query();

                                                                                                                         ?>

Display Last Post From Category

<?php $cat_name = uncategorized; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>

<h1><?php the_title(); ?></h1>	

<?php the_content(); ?>

<?php endwhile; endif; ?>

Adding Custom Menu To WordPress Theme

This snippet is for the functions.php file

add_theme_support( 'menus' );

or


register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ));
register_nav_menu( 'footer', __( 'Footer Menu', 'twentytwelve' ));

This is where you want the menu to appear:

<?php wp_nav_menu( array( 'sort_column', 'menu_order', 'container_class' => 'menu-header' ) ); ?>

Twenty Eleven – remove “Theme options” tab

This code snippet remove tabs – “Theme Options”; “Header” and “Custom Background” from the twenty eleven theme

//Remove the custom options provided by the default twentyeleven theme.
add_action( ‘after_setup_theme’,’remove_twentyeleven_options’, 100 );
function remove_twentyeleven_options() {

	remove_custom_background();
	remove_custom_image_header();
	remove_action(‘admin_menu’, ‘twentyeleven_theme_options_add_page’);

}

Custom Login, Register and password reset template

PHP

<!-- Custom Login/Register/Password Code -->
<!-- Theme Template Code -->

<div id="login-register-password">

	<?php global $user_ID, $user_identity; get_currentuserinfo(); if (!$user_ID) { ?>

	<ul class="tabs_login">
		<li class="active_login"><a href="#tab1_login">Login</a></li>
		<li><a href="#tab2_login">Register</a></li>
		<li><a href="#tab3_login">Forgot?</a></li>
	</ul>
	<div class="tab_container_login">
		<div id="tab1_login" class="tab_content_login">

			<?php $register = $_GET['register']; $reset = $_GET['reset']; if ($register == true) { ?>

			<h3>Success!</h3>
			<p>Check your email for the password and then return to log in.</p>

			<?php } elseif ($reset == true) { ?>

			<h3>Success!</h3>
			<p>Check your email to reset your password.</p>

			<?php } else { ?>

			<h3>Have an account?</h3>
			<p>Log in or sign up! It’s fast & <em>free!</em></p>

			<?php } ?>

			<form method="post" action="<?php bloginfo('url') ?>/wp-login.php" class="wp-user-form">
				<div class="username">
					<label for="user_login"><?php _e('Username'); ?>: </label>
					<input type="text" name="log" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="11" />
				</div>
				<div class="password">
					<label for="user_pass"><?php _e('Password'); ?>: </label>
					<input type="password" name="pwd" value="" size="20" id="user_pass" tabindex="12" />
				</div>
				<div class="login_fields">
					<div class="rememberme">
						<label for="rememberme">
							<input type="checkbox" name="rememberme" value="forever" checked="checked" id="rememberme" tabindex="13" /> Remember me
						</label>
					</div>
					<?php do_action('login_form'); ?>
					<input type="submit" name="user-submit" value="<?php _e('Login'); ?>" tabindex="14" class="user-submit" />
					<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
					<input type="hidden" name="user-cookie" value="1" />
				</div>
			</form>
		</div>
		<div id="tab2_login" class="tab_content_login" style="display:none;">
			<h3>Register for this site!</h3>
			<p>Sign up now for the good stuff.</p>
			<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" class="wp-user-form">
				<div class="username">
					<label for="user_login"><?php _e('Username'); ?>: </label>
					<input type="text" name="user_login" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="101" />
				</div>
				<div class="password">
					<label for="user_email"><?php _e('Your Email'); ?>: </label>
					<input type="text" name="user_email" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" id="user_email" tabindex="102" />
				</div>
				<div class="login_fields">
					<?php do_action('register_form'); ?>
					<input type="submit" name="user-submit" value="<?php _e('Sign up!'); ?>" class="user-submit" tabindex="103" />
					<?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?>
					<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
					<input type="hidden" name="user-cookie" value="1" />
				</div>
			</form>
		</div>
		<div id="tab3_login" class="tab_content_login" style="display:none;">
			<h3>Lose something?</h3>
			<p>Enter your username or email to reset your password.</p>
			<form method="post" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" class="wp-user-form">
				<div class="username">
					<label for="user_login" class="hide"><?php _e('Username or Email'); ?>: </label>
					<input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" />
				</div>
				<div class="login_fields">
					<?php do_action('login_form', 'resetpass'); ?>
					<input type="submit" name="user-submit" value="<?php _e('Reset my password'); ?>" class="user-submit" tabindex="1002" />
					<?php $reset = $_GET['reset']; if($reset == true) { echo '<p>A message will be sent to your email address.</p>'; } ?>
					<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?reset=true" />
					<input type="hidden" name="user-cookie" value="1" />
				</div>
			</form>
		</div>
	</div>

	<?php } else { // is logged in ?>

	<div class="sidebox">
		<h3>Welcome, <?php echo $user_identity; ?></h3>
		<div class="usericon">
			<?php global $userdata; get_currentuserinfo(); echo get_avatar($userdata->ID, 60); ?>

		</div>
		<div class="userinfo">
			<p>You’re logged in as <strong><?php echo $user_identity; ?></strong></p>
			<p>
				<a href="<?php echo wp_logout_url('index.php'); ?>">Log out</a> | 
				<?php if (current_user_can('manage_options')) { 
					echo '<a href="' . admin_url() . '">' . __('Admin') . '</a>'; } else { 
					echo '<a href="' . admin_url() . 'profile.php">' . __('Profile') . '</a>'; } ?>

			</p>
		</div>
	</div>

	<?php } ?>

</div>

<!-- Custom Login/Register/Password Code -->

CSS

<!-- Custom Login/Register/Password Code-->
<!-- CSS -->

<style type="text/css">
/* tabbed list */
ul.tabs_login {
	padding: 0; margin: 20px 0 0 0;
	position: relative;
	list-style: none;
	font-size: 14px;
	z-index: 1000;
	float: left;
	}
ul.tabs_login li {
	border: 1px solid #E7E9F6;
	 -webkit-border-top-right-radius: 10px;
	 -khtml-border-radius-topright: 10px;	
	 -moz-border-radius-topright: 10px;
	border-top-right-radius: 10px;
	 -webkit-border-top-left-radius: 10px;
	 -khtml-border-radius-topleft: 10px;	
	 -moz-border-radius-topleft: 10px;
	border-top-left-radius: 10px;
	line-height: 28px; /* = */ height: 28px;
	padding: 0; margin: 0 5px 0 0;
	position: relative;
	background: #fff;
	overflow: hidden;
	float: left;
	}
ul.tabs_login li a {
	text-decoration: none;
	padding: 0 10px;
	display: block;
	outline: none;
	}
html ul.tabs_login li.active_login {
	border-left: 1px solid #E7E9F6;
	border-bottom: 1px solid #fff;
	 -webkit-border-top-right-radius: 10px;
	 -khtml-border-radius-topright: 10px;	
	 -moz-border-radius-topright: 10px;
	border-top-right-radius: 10px;
	 -webkit-border-top-left-radius: 10px;
	 -khtml-border-radius-topleft: 10px;	
	 -moz-border-radius-topleft: 10px;
	border-top-left-radius: 10px;
	background: #fff;
	color: #333;
	}
html body ul.tabs_login li.active_login a { font-weight: bold; }
.tab_container_login {
	background: #fff;
	position: relative;
	margin: 0 0 20px 0;
	border: 1px solid #E7E9F6;
	 -webkit-border-bottom-left-radius: 10px;
	 -khtml-border-radius-bottomleft: 10px;	
	 -moz-border-radius-bottomleft: 10px;
	border-bottom-left-radius: 10px;
	 -webkit-border-bottom-right-radius: 10px;
	 -khtml-border-radius-bottomright: 10px;	
	 -moz-border-radius-bottomright: 10px;
	border-bottom-right-radius: 10px;
	 -webkit-border-top-right-radius: 10px;
	 -khtml-border-radius-topright: 10px;	
	 -moz-border-radius-topright: 10px;
	border-top-right-radius: 10px;
	z-index: 999;
	float: left;
	width: 100%;
	top: -1px;
	}
.tab_content_login {
	padding: 7px 15px 15px 15px;
	padding-top: 10px;
	}
	.tab_content_login ul {
		padding: 0; margin: 0 0 0 15px;
		}
		.tab_content_login li { margin: 5px 0; }
/* global styles */
#login-register-password {}
	#login-register-password h3 {
		border: 0 none;
		margin: 10px 0;
		padding: 0;
		}
	#login-register-password p {
		margin: 0 0 15px 0;
		padding: 0;
		}
/* form elements */
.wp-user-form {}
	.username, .password, .login_fields {
		margin: 7px 0 0 0;
		overflow: hidden;
		width: 100%;
		}
		.username label, .password label { float: left; clear: none; width: 25%; }
		.username input, .password input { 
			font: 12px/1.5 "Lucida Grande", "Lucida Sans Unicode", Verdana, sans-serif;
			float: left; clear: none; width: 200px; padding: 2px 3px; color: #777;
			}
.rememberme { overflow: hidden; width: 100%; margin-bottom: 7px; }
#rememberme { float: left; clear: none; margin: 4px 4px -4px 0; }
.user-submit { padding: 5px 10px; margin: 5px 0; }
.userinfo { float: left; clear: none; width: 75%; margin-bottom: 10px; }
	.userinfo p { 
		margin-left: 10px; 
		}
.usericon { float: left; clear: none; width: 15%; margin: 0 0 10px 22px; }
	.usericon img {
		border: 1px solid #F4950E;
		padding: 1px;
		}

<!-- Custom Login/Register/Password Code-->

jQuery

<!-- Custom Login/Register/Password Code-->
<!-- jQuery -->

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
	$j(document).ready(function() {
		$j(".tab_content_login").hide();
		$j("ul.tabs_login li:first").addClass("active_login").show();
		$j(".tab_content_login:first").show();
		$j("ul.tabs_login li").click(function() {
			$j("ul.tabs_login li").removeClass("active_login");
			$j(this).addClass("active_login");
			$j(".tab_content_login").hide();
			var activeTab = $j(this).find("a").attr("href");
			if ($j.browser.msie) {$j(activeTab).show();}
			else {$j(activeTab).show();}
			return false;
		});
	});
</script>

<!-- Custom Login/Register/Password Code-->

Defragment ext4

ext4 filesystems are not actually fragment-proof, however they fragment the drives a lot less than ntfs for example, you can use

e4defrag

Option c will check if defragment is need, and then you can run it with v
Example:

 sudo e4defrag -v <filename or mount point of ext4 drive> 

Manual page

Creating Custom Sidebar (Widget Area)

1. Find file called functions.php in your theme folder and add the code in it.

if ( function_exists('register_sidebar') )
    register_sidebar( array(
   'name' => __( 'Homepage'),
   'id' => 'homepagearea',
   'description' => __( 'Widget area for your site homepage', 'twentyeleven' ),
   'before_widget' => '<aside id="%1$s" class="widget %2$s">',
   'after_widget' => "</aside>",
   'before_title' => '<h3 class="widget-title">',
   'after_title' => '</h3>',
   ) );

2. Place this code in the div or any other container you wish to display you new custom widget area.

<?php 
 // Custom widget Area Start
 if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Homepage') ) : ?>
<?php endif;
// Custom widget Area End
?>

That’s all.