GTmetrix API and .Net

GTmetrix API and .Net

I’m currently working on a project which requires keeping a close eye on performance. Because of this, we decided to include GTmetrix’s webpage performance analyzes service as part of the overall monitoring strategy. To simplify the implementation, I’ve decided to create a native .net client.

GTmetrix

For everyone not familiar with Gtmetrix, they are offering a service which analyzes webpage speed performance. They depend on PageSpeed and YSlow, for generating page performance scores and providing optimization suggestions. The service is free when accessed via the web interface and. Besides, they also have expose all features via an API.

web-performance-1

The API

The API is somewhat old fashion but simple to consume and understand. However, I prefer not having to deal with HTTP calls, response parsing, and streams. Therefore I created a helper client called GTmetrix-net.

GTmetrix-net

The GTmetrix-net client, a wrapper around the API, offers .Net developers an easy way to utilize GTmetrix’s performance testing service. Using this client, you can integrate performance testing into your development environment or your application using a simple .net interface.

Within the current version, it’s possible to submit test requests and retrieve Test results as well as retrieve Location, Browser, and Account status (Account quota) information. Additional helpers for downloading resources, Async poll state, and the ability to create test sets, will be added shortly.

Getting Started

First, you need to sign up at gtmetrix.com and obtain your unique API Key. You can generate or view your API key at the API Key box at the top of the API Details page. Once you have set up your account and created the Key, you can start using the .Net client.

Installation

To install the client, run the following command in the Package Manager Console Install-Package gtmetrix-net

Authentication

The first step is to authenticate by providing your unique API Key and Username while creating a new client connection. Use your e-mail address as the username.

Start a request

Starting a new Test request is simple. The first step is to create a new TestRequest found within the GTmetrix.Modelnamespace. The constructor overloads can be used to override the browser and location defaults. Or assign optional values by using its properties. After having set up your TestRequest, call SubmitTest, part of the client, and make sure to provide the test request.

Access the test results

You can access the test results by calling client.GetTest("test_id"); Just like the underlying API, some data might not be present based on the status of your test request. In the future, I will add a poll helper to eliminate the need to write additional polling code.

You can find additional information and code samples on the GitHub project page. Suggestions and recommendations are welcome!

Post Navigation