WitsCounter

counting Wits parameters throughput in real time

04 Jan 2016    606 words    3 minutes reading     Post a comment

Image by Winter

Recently, I published a post of an application called WitsGenerator to provide random Wits data through a TCP port. Today I’m publishing another application related to the Oil & Gas business, but now it’s to count the amount of parameters being transmitted through Wits.

For this project, I’m using:

  • Java JDK 8u73;
  • Maven 3.3.9.

This code is also available in my GitHub page. Feel free to comment if you have any doubt or suggestion. :smile:

Licensing

This software is licensed under the MIT license, what basically means that you can use it for any purpose without any warranty. :warning:

Functionality

This simple application provides 2 different information:

  • The total amount of parameters per time interval;
  • The amount of different parameters per time interval.

post-img-java-witscounter-printscreen1.jpg

Architecture

The application works as an implementation of Producer-Consumer over a matrix data structure. This matrix works as a memory and each cell of this matrix represents, in any given moment, the amount of record-item received for that position since last matrix clean up. If the number 3 is found in the position 0x0, it means that the record 1 item 1 was received 3 times since the last matrix clean up. If the number 2 is found in the position 9x3, it means that the record 10 item 4 was received 2 times since the last matrix clean up. To count the total amount of items received in a given moment, it’s necessary to do a big sum of all the values present in the matrix. To count the number of unique items received in a given moment, it’s necessary to do a sum of the number of cells which values differs to 0. This matrix is based in the type byte, that offers good sizing for big amount of time, and, at the same time, is cheaper in terms of memory.

The Producer-Consumer aspect comes from the way the matrix is used: one thread keeps reading data from the Wits server and adds into the matrix, while another thread, from time to time, counts the total and unique values in the matrix and cleans it up.

Thus, there are 5 important classes:

  • WitsCounter.java: is the class that orchestrates the start/stop of the resources;
  • WitsMatrixCounter.java: is the implementation of the matrix used to store current counters;
  • WitsCountConsumer.java: is the class responsible for consuming the data from the matrix and printing into the console;
  • WitsCountProducer.java: is the class responsible for reading the data from the socket and adding to the matrix;
  • TCPClient.java: is the representation of the TCP connection to the Wits server.

The usage is as follow:

Usage: java -jar wits-counter-1.1.jar [host] [port] [interval]
Connects to a Wits server through TCP Socket and prints the number
of parameters being received per time interval. It does not
reconnect if the connection fails.
  [host]	      The IP of hostname of the server
  [port]	      The port of the server
  [interval]	  The time interval, in milliseconds, to aggregate
		            values. If less or equal 0, 1000 is used.

When executed, it will start a TCP Client socket client to the data provider, and will start counting the parameters being received. No major treatment is done in the inputs, so you may have errors when wrong/unreal parameters are passed.

Code

I’m not attaching here the Main.class. If necessary, visit my GitHub page.

WitsCounter.java

WitsMatrixCounter.java

WitsCountConsumer.java

WitsCountProducer.java

TCPClient.java