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.
For everyone not familiar with Gtmetrix, they are offering a service which analyzes webpage speed performance. They are depending on PageSpeed and YSlow, for generating page performance scores and providing optimization suggestions. The service is free when accessed via the web interface and. In addition, they also have expose all features via an 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.
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 into 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 in the near future.
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 generated the Key, you can start using the .Net client.
To install the client, run the following command in the Package Manager Console Install-Package gtmetrix-net
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 setup your
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 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!