Sunday, October 15, 2017

Nginx—Knowing the Basics

Nginx is a lightweight, high performance web server designed to deliver large amounts of static content quickly with efficient use of system resources. nginx’s strong point is its ability to efficiently serve static content, like plain HTML and media files. Some consider it a less than ideal server for dynamic content.[1]

Concepts of Niginx

  • Nginx Process
    • Nginx has one master process and several worker processes.
      • Master process (1)
        • To read and evaluate configuration, and maintain worker processes
      • worker processes (N)
        • To do actual processing of requests
        • Each worker can handle thousands of concurrent connections. It does this asynchronously with one thread, rather than using multi-threaded programming.
  • Content Handling
    • Static Content
      • Nginx’s strong point is its ability to efficiently serve static content, like plain HTML and media files. 
    • Dynamic Content
      • Rather than using the embedded interpreter approach, nginx hands off dynamic content to CGI, FastCGI, or even other web servers like Apache, which is then passed back to nginx for delivery to the client.
  • Request distribution
    • Unlike Apache, which uses a threaded or process-oriented approach to handle requests, nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. 
      • which provides more predictable performance under load.


The way nginx and its modules work is specified in the configuration file.
  • nginx.conf
    • By default, the main configuration file is named nginx.conf and placed in the directory:
      • /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx
    • Can be also configured by a compile-time option:
      • --conf-path=/u01/data/config/nginx/nginx.conf
  • Need to reload configuration after changes
    • Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. 
    • To reload configuration, execute:
      • nginx -s reload
To learn how to start, stop, and reload configuration in more details, read here.

Configuration—Syntax and Semantics 

The syntax and semantics of nginx's configuration files are described below:
  • Comments
    • All lines preceded by a pound sign or hash (#) are comments
  • Directives 
    • nginx consists of modules which are controlled by directives specified in the configuration file.
      • Directives describe the basic behavior of the web server
    • Directives are divided into 
      • Simple directives 
        • All statements end with a semi-colon (;)
      • Block directives
        • Blocks are variables having arguments that are themselves sub-directives enclosed in brackets ({ })
        • Examples
          • http{} block — Universal Configuration
          • server{} block — Virtual Domains Configuration
            • Configures multiple servers virtually on different ports or with different server names
              • The server_name directive, which is located in the server block, lets the administrator provide name-based virtual hosting. 
          • upstream{} block
            • Defines a cluster that you can proxy requests to
              • Commonly used for defining either a web server cluster for load balancing, or an app server cluster for routing / load balancing
  • Include statement
    • Can be used to include directives from a separate file.
    • Anything written in the file is interpreted as if it was written inside the enclosing block. 
  • Context
    • If a block directive can have other directives inside braces, it is called a context (examples: events, http,server, and location).
    • Examples
      • Directives placed in the configuration file outside of any contexts are considered to be in the main context. 
        • The events and http directives reside in the main context, server in http, and location in server.
      • access_log directive sets the location of the nginx access log
        • which can be set in either
          • http block, or 
            • which can be used to log all access to a single file, or as a catch-all for access to virtual hosts that don’t define their own log files
          • server block
            • which sort the output specific to each virtual domain into its own file

Http Request Handling

When clients send http requests to nginx, it distributes requests based on the following information:
  • Request URL
  • Request Headers (e.g., Host  header)
and processes them in the following sequence:
  1. Receives data from clients
  2. Parses the http request
  3. Finds the virtual server
    • Configured by server {} blocks
  4. Finds the location
    • Specified by the location directive 
      • Directs requests to specific files and folders
  5. Runs phase handlers
  6. Generates the http response
  7. Filters response headers
  8. Filters the response body
  9. Sends out the output to the client
Note that nginx always fulfills request using the most specific match.

Sunday, September 17, 2017

JMeter: How to Verify JSON Response?

JSON (Javascript object notation) is a serialization format (in key-value pairs) of data structures .  For REST API, it is widely used for data transfer from server to client.  For example, a client sends an HTTP request with below header:

  • Accept: application/json

The server can respond with below sample JSON data:

    "result": [],
    "ccapiInfo": {
      "createdOn": "2017-09-07T15:25:29.000Z",
      "cachedOn": "2017-09-07T15:21:49.513Z",
      "origin": "cache",
      "canonicalLink": ""

with a response header of:
Content-Type: application/json

In this article, we will discuss how to achieve two tasks in Apache JMeter:

JSON Extractor / JSONPath

One of the advantages of XML is the availability of numerous tools to analyse, transform and selectively extract data out of XML documents. XPath is one of these powerful tools.  For JSON, we have a similar tool called JSONPath.

JSONPath is the XPath for JSON.  Since a JSON structure is normally anonymous, JSONPath assigns symbol $ as the root object.

Below is a side-by-side comparison of the JSONPath syntax elements with its XPath counterparts.[9]

/$the root object/element
.@the current object/element
/. or []child operator
..n/aparent operator
//..recursive descent. JSONPath borrows this syntax from E4X.
**wildcard. All objects/elements regardless their names.
@n/aattribute access. JSON structures don't have attributes.
[][]subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.
|[,]Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.
n/a[start:end:step]array slice operator borrowed from ES4.
[]?()applies a filter (script) expression.
n/a()script expression, using the underlying script engine.
()n/agrouping in Xpath

JSONPath expressions can use the dot–notation


or the bracket–notation


for input paths. For the internal or output paths, they will always be converted to the more general bracket–notation.  Below diagram shows the evaluation result using a JSONPath Online Evaluator with the input and JSONPath Expression as given in this article.

JSR223 Assertion

Assertion in JMeter help verify that your server under test returns the expected results. JMeter includes quite a few assertion elements for validating the sampler’s response, yet sometimes your validation decision might follow complex logic, and can’t be configured using the out-of-the-box JMeter assertions - scripting is then required.

If you need to write scripting assertion code to extend baseline JMeter functionality, JSR223, in combination with Groovy language is a good choice performance-wise—especially when its compilation caching is enabled.

Groovy Script

String jsonString =  vars.get("myCanonicalLink");
String userNameString = vars.get("user_name"); ("The canonicalLink is " + jsonString);

if ( jsonString != "" + 
      userNameString + "/api/v1/instance/Compute-" + userNameString + "/") 
AssertionResult.setFailureMessage("The canonicalLink is wrong");

However, every test element including assertion added to the test plan will increase the total CPU and memory requirements.  So, plan your use of assertions sparingly.