Pages

Search This Blog

Friday, December 11, 2020

The messed up 'User-Agent' string !!

This is interesting. Ever wondered why all the User-Agents start with Mozilla? please read on...

What is User-Agent?

When you browse a web page via your browser, the browser sends this particular string called User-Agent along with other request headers. Below is the definition for User-Agent from Mozilla developers.

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

 

What is the need for User-Agent? In HTTP, the User-Agent string is often used for content negotiation, where the origin server selects suitable content or operating parameters for the response. For example, the User-Agent string might be used by a web server to choose variants based on the known capabilities of a particular version of client software. The concept of content tailoring is built into the HTTP standard in RFC 1945 "for the sake of tailoring responses to avoid particular user agent limitations.”. User-Agent is used to identify webcrawlers which uses the respective spiderbot name in their User-Agent. The other reason is for sniffing the client browser, for business gains which the below content is all about.


A typical User-Agent string looks like this (which can be found by checking your browser's request headers in developer tools or by hitting F12 and checking the Request headers in Headers section):

User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0 

 

The UA(User-Agent) string definition by Mozilla team is as follows :

Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion
1 Mozilla/5.0 is the general token that says the browser is Mozilla-compatible. For historical reasons, almost every browser today sends it. 
2 platform describes the native platform the browser is running on (Windows, Mac, Linux, Android, etc.), and if it's a mobile phone. Firefox OS phones simply say Mobile — the web is the platform. Note that platform can consist of multiple "; "-separated tokens. See below for further details and examples.  
3 rv:geckoversion indicates the release version of Gecko (such as "17.0"). In recent browsers, geckoversion is the same as firefoxversion.  
4 Gecko/geckotrail indicates that the browser is based on Gecko. (On Desktop, geckotrail is always the fixed string 20100101.)  
5 Firefox/firefoxversion indicates the browser is Firefox, and provides the version (such as "17.0").

 

And if we check the same on a Google-Chrome browser this is what we get, and why on earth does it say Safari and Gecko:

User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36


So why  do all these User-Agent string start with Mozilla?  

It follows a historical reason as mentioned by the Mozilla team in the above definition and the history goes like this...

Even though Tim Berners Lee's WorldWideWeb is the first web client/browser, the popularity explosion happened with  NCSA Mosaic which was a graphical browser (with pictures along with text) running originally on Unix and Mosaic called itself NCSA_Mosaic/2.0 (Windows 3.1)

Marc Andreessen, who was the leader of the Mosaic team at NCSA, quit to form a company that would later be known as Netscape Communications Corporation. Netscape released its flagship Navigator product in October 1994, and it took off the next year. 

The Netscape web browser did not use any NCSA Mosaic code. The internal codename for the company's browser was Mozilla, which stood for "Mosaic killer", as the company's goal was to displace NCSA Mosaic as the world's number one web browser. This codename they incorporated as their User-Agent header Mozilla/1.0. 

Microsoft, which had thus far not marketed a browser, finally entered the arena with its Internet Explorer product (version 1.0 was released 16 August 1995), purchased from Spyglass, Inc. This began what is known as the "browser wars" in which Microsoft and Netscape competed for the Web browser market.

Why the user-agent along with header? because at that time the “user agent sniffing” is done by webmasters to give the features to the particular browser request and not to other. Mozilla/Netscape2.0 had many cool features back then like frames, java, plugins etc.,   and to “Mozilla” user-agent's webmasters sent frames, but to other browsers they sent not frames. Guess what these browsers were not free at that time :)

Interesting history: 
In 1996, Netscape's share of the browser market reached 86% (with Internet Explorer edging up 10%); but then Microsoft began integrating its browser with its operating system and bundling deals with OEMs. 

 

When user-agent sniffing was the need of the hour, to get the features from Webmasters like frames, a feature supported by both Netscape and IE, but not other browsers of the era, IE spoofed the Mozilla string into their UA. 

The UA string for IE at that time was:
Mozilla/1.22 (compatible; MSIE 2.0; Windows 95),   
 

Unable to continue commercially funding their product's development, Netscape responded by open sourcing its product, creating Mozilla.  The Mozilla Organization rewrote the entire browser's source code based on the Gecko rendering engine, and all future Netscape releases were based on this rewritten code.

Mozilla updated it's UA string with Gecko as follows (the first two 'windows' fields being platform on which the browser runs):
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.1) Gecko/20020826,
 
Mozilla then became firefox and followed the below UA for firefox:   
Mozilla/5.0 (Windows; U; Windows NT 5.1; sv-SE; rv:1.7.5) Gecko/20041108 Firefox/1.0, 
 
Gecko rendering engine was widely accepted and many browsers began to use the Gecko rendering engine like the below examples: 
  • Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.2) Gecko/20040825 Camino/0.8.1 
  • Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.8) Gecko/20071008 SeaMonkey/1.0 

each pretending to be Mozilla, and all of them powered by Gecko.

 

Konquerer browser which comes with Linux distros, used KHTML rendering engine, but it was not Gecko, and to get the Gecko tag (for browser sniffers) in UA string it appended  “like Gecko” to render good pages like that of Gecko.

Mozilla/5.0 (compatible; Konqueror/3.2; FreeBSD) (KHTML, like Gecko) 

 

Apple built Safari, and used KHTML, and added many features, by forkingthe project, and called it AppleWebKit, but wanted pages written for KHTML, so the spoofing continues by appending KHTML string along with Webkit.

Mozilla/5.0 (Macintosh; U; PPC Mac OS X; de-de) AppleWebKit/85.7 (KHTML, like Gecko) Safari/85.5,

 

Then comes the Chrome with much spoofing, which used the Webkit engine and wanted to get the pages build for Safari, here is that great impersonation with AppleWebKit and Safari tag:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36


The bottom-up approach of this spoofing/impersonations goes like this:

Chrome used WebKit, and pretended to be Safari(AppleWebKit), and WebKit pretended to be KHTML(KHTML), and KHTML pretended to be Gecko (like Gecko), and all browsers pretended to be Mozilla (Mozilla) 


Are you building a web-browser? If so, please follow the above mess !!



References:

Sunday, December 6, 2020

Disk IOPS - Throughput, Block size...,

We all might have heard these terms IOPS/Throughput, Disk blocks, Latency etc., let's look into these details.

What is IOPS? 

Input/output operations per second (IOPS, pronounced eye-ops) is an input/output performance measurement used to characterize computer storage devices like hard disk drives (HDD), solid state drives (SSD), and storage area networks (SAN).
To meaningfully describe the performance characteristics of any storage device, it is necessary to specify a minimum of three metrics simultaneously: IOPS, response time, and (application) workload. Absent simultaneous specifications of response-time and workload, IOPS are essentially meaningless.
IOPS number vary greatly with the input parameters that testers use, including the balance of read and write operations, the mix of sequential and random access patterns, the number of worker threads and queue depth, as well as the data block sizes. 
 

Sequential and Random access on IOPS performance: 

Sequential operations access locations on the storage device in a contiguous manner and are generally associated with large data transfer sizes, e.g. 128 kB. 
Random operations access locations on the storage device in a non-contiguous manner and are generally associated with small data transfer sizes, e.g. 4kB. 

For HDDs and similar electromechanical storage devices, the random IOPS numbers are primarily dependent upon the storage device's random seek time, whereas, for SSDs and similar solid state storage devices, the random IOPS numbers are primarily dependent upon the storage device's internal controller and memory interface speeds. 

IOPS = 1000 milliseconds / (Average Seek Time + Average Latency), with the latter two also measured in milliseconds.

What is Average Latency and Average Seek Time: 

Average Latency is the time it takes the disk platter to spin halfway around. It is calculated by dividing 60 seconds by the Rotational Speed of the disk, then dividing that result by 2 and multiplying everything by 1,000 milliseconds. ((60 / RPM)/2)*1,000. 

Average Seek Time is the time it takes for the head (the piece that reads data) to reach the area on the disk upon which that data is stored. The head needs to move around the storage area in order to locate the targeted data. You must average both read and write seek times in order to find the average seek time. 

The reason for SSD's having higher IOPS is because that for solid state drives, the average latency drops significantly, as there is no rotating disk inside. The seek time is also nil/null/N/A. 

While traditional HDDs have about the same IOPS for read and write operations, most NAND flash-based SSDs are much slower writing than reading due to the inability to rewrite directly into a previously written location forcing a procedure called garbage collection. 

 

What is Throughput? 

Throughput measures the data transfer rate to and from the storage media in megabytes per second.The disk throughput describes how many data may be transferred per second,hence disk block size play an important role: 

throughput = [IOPS] * [block size] 

Let's consider as the sample the 3000 IOPS and SQL database engine, the block size in terms of db engine is called the page size and for SQL Server it's equal to 8 KB. If you wish to calculate the actual throughput, if the IOPS defined, you will end up with the formula below:

 throughput = [IOPS] * [block size] = 3000 * 8 = 24 000 KB/s = 24 MB/s 

IOPS can therefore become a bottleneck for throughput. It is likely that your CPU and memory will not be a limiting factor, as they can handle more information per second than most storage devices and networks. A serially-attached storage device might only have a maximum throughput of up to 2,812 MBps, while Ethernet reaches up to 12,500 MBps and fiber channel networks reach 4,000 MBps. The size and number of blocks traversing the network can make a big difference in throughput. A 1 MB block will take longer to pass through the network but you may not move as many of them. A 4 KB block will move very quickly, but you’ll likely face a high volume. 

 

So what is a Block size/blocks? 

A sector is a physical spot on a formatted disk that holds information (please see the above disk diagram). When a disk is formatted, tracks are defined (concentric rings from inside to the outside of the disk platter. Each track is divided into a slice, which is a sector. On hard drives and floppies, each sector can hold 512 bytes of data. 

A block, on the other hand, is a group of sectors that the operating system can address (point to). A block might be one sector, or it might be several sectors (2,4,8, or even 16). The bigger the drive, the more sectors that a block will hold. 

So why are there blocks. Why doesn't the operating system just point straight to the sectors? Because there are limits to the number of blocks, or drive addresses, that an operating system can address. By defining a block as several sectors, an OS can work with bigger hard drives without increasing the number of block addresses. 

True or False: When a file is copied from a hard drive to a floppy, it will usually take up less space than it takes up on a hard drive --> TRUE. 
If you tend to store lots of small files on your hard drive the blocks used to store all those little files can have a lot of wasted space in them. 
Likewise, compressing lots of little files may not save as much space on a big hard drive with a big block size 
 
Every file takes up one block no matter its size, so choosing the correct block size to efficiently consume storage can make a big difference when it comes to performance. 
For example. SQL server logs in 64 kb blocks, while Windows Server might use 4 kb blocks, and the underlying vSphere hypervisor uses 1 MB blocks. As you’ll see, the block size and IOPS have a snowballing effect on network traffic throughput. 

 

Typically Linux and Windows use a 4KB default filesystem block size. 
Even though the disk Sector size (logical/physical) shows 512 bytes, the OS filesystem IO block will be 4K. 
In a Linux machine you can verify this using the below commands: 
 
#fdisk -l /dev/(diskname) 
#cat /sys/block/sd*/queue/hw_sector_size
#cat /sys/block/sd*/queue/physical_block_size 
#stat -f filename
  File: "IOPS" ID: e68758737c7b7176 Namelen: 255 Type: ext2/ext3    Block size:   4096 Fundamental block size: 4096

For blocksize and performance, you can refer the link --> https://knowledgebase.progress.com/articles/Article/18293 

 

I find the below analogy quite easy and enjoyable to understand on IOPS/Throughput (thanks to @Kgothatso Kurt -stackoverflow) 

You have 4 buckets (Disk blocks) of the same size that you want to fill or empty water. You'll be using a jug to transfer the water into the buckets. 

Now your question will be : 

At a given time (per second), how many jugs of water can you pour(write) or withdraw (read)? This is IOPS 

At a given time (per second) what's the amount(bit, kb, mb, ect) of water the jug can transfer into/out of the bucket continuously? This is throughput. 

Additionally, there is a delay in the process of you pouring and or withdrawing the water. This is Latency. 

There are 3 things to consider about IOPS and Throughput. 

- Size (file size/ block size) 
- Patterns (Random/Sequential) 
- Mix (Read/Write) percentage 
(all these factors has been explained as of now) 

 

Disk IOPS info from iostat:

In GNU/Linux, iostat is a command line tool which comes with the package systat 

iostat output on a machine with low IO usage:

# iostat
Linux 3.10.0-1062.18.1.el7.x86_64 (infra-85)     Sunday 06 December 2020     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.53    0.00    0.65    1.66    0.16   96.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
xvda              2.63         7.80        26.39  112212822  379585947


%tps : The tps number here is the number of I/O Operations Per Second (IOPS), transactions per second. 

%iowait : is first and foremost a CPU metric that measures the percentage of time the CPU is idle while waiting for an I/O operation to complete. This metric is heavily influenced by both your CPU speed and CPU load The only thing %iowait tells us for sure is that the CPU is occasionally idle and can thus handle more computational work.

 -x will give details report 

-m shows result in MB 

-d show disk utilization 

 

#iostat -dmx
Linux 3.10.0-1062.18.1.el7.x86_64 (infra-85)     Sunday 06 December 2020     _x86_64_    (2 CPU)

Device:         rrqm/s   wrqm/s     r/s     w/s    rMB/s    wMB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
xvda              0.00     0.09    0.28    2.35     0.01     0.03    25.97     0.26  100.18  138.73   95.59  18.27   4.81

 

%avgqu-sz : The avgqu-sz metric is an important value. it shows the number of operations that were either queued or being serviced. Single digit numbers with the occasional double digit spike are safe(ish) values. Triple digit numbers are generally not. Note that this metric is unlikely to hover around zero unless you are doing very little I/O. A certain amount of queueing is generally unavoidable as modern storage devices reorder disk operations so as to improve overall performance. 

%await : The await metric is the average time from when a request is put in the queue to when the request is completed. Therefore, this metric is the sum of the time a request spent waiting in the queue and the time your storage device was working on servicing the request. This number is highly dependent on the number of items in the queue. Much like avgqu-sz, you'll want to have an idea of the value of this metric during normal operations for use as a reference when trouble occurs. 

%util : Percentage of elapsed time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100% for devices serving requests serially. But for devices serving requests in parallel, such as RAID arrays and modern SSDs, this number does not reflect their performance limits.

#iostat 5 2

gives 2 statistics in 5 seconds interval 

What is a good or bad number though? As with most metrics, if the first time you look at it is when you are in trouble then it’s less helpful. You should have an idea of how much IO you typically do, then if you experience issues and are doing 10x that or only getting 1/10 from the disks then you have a good candidate explanation for them.

Generally a HDD will have an IOPS range of 55-180, while a SSD will have an IOPS from 3,000 – 40,000. 

 

Below tables shows the IOPS as per the manufacturer: 

HDD's/Mechanical drives:
SSD's IOPS:

 

References: 

GNU, Unix, Linux, *nix, FOSS, F(L)OSS,..

Woah, that's a long list keywords..and yes, these are some of the basic terms that comes in the minds of programmers or sysadmins(i gues...