Hit-Count (read) array in Java

To evaluate the algorithm, I have to calculate how often the elements of the byte array are read / received. The byte array is filled with the contents of the file, and my algorithm can skip many bytes in the array (for example, the Boyer-Moore string search algorithm). I have to find out how often the item is actually read. This byte array is passed to several methods and classes.

My ideas so far:

  • Increment the counter at each place where the byte array is read. This seems erroneous as many of these places. In addition, I would have to delete this code subsequently so that it does not affect the runtime of my algorithm.

  • Use an ArrayList instead of a byte array and overwrite its get method. Again, there are many methods that will need to be changed, and I suspect there will be a performance loss.

  • Can I use Eclipse debug mode in some way? I see that I can specify the number of hits for the observation points, but it does not seem possible to display the number of hits ?!

  • Can the Reflection API help me in some way?

  • Most likely, like 2), but in order to reduce efforts. Can I force the Java method to accept an ArrayList where it needs an array so that it transparently calls the get method every time an element is read?

+4
source share
4 answers

Perhaps there is a ready-made solution, but I would probably just wrap an array of bytes in a simple class.

public class ByteArrayWrapper { private byte [] bytes; private long readCount = 0; public ByteArrayWrapper( byte [] bytes ) { this.bytes = bytes; } public int getSize() { return bytes.length; } public byte getByte( int index ) { readCount++; return bytes[ index ]; } public long getReadCount() { return readCount; } } 

Something like that. Of course, this affects the working time, but not very much. You can try and once the difference, if you find that it is important, we will have to find another way.

+2
source

The most efficient way to do this is to add some injection of code. However, this will probably be much more complicated if you get the right to write a wrapper for your byte [] and pass it. (tedious, but at least the compiler will help you). If you use a shell that basically does nothing (without consideration), it will be almost as effective as not using a shell, and when you want to count, you can use an implementation that does this.

+1
source

You can use EHCache without extra costs: implement a cache in memory, with a key at the index of the array. EHCache provides an API that allows you to request bid values ​​out of the box.

0
source

It is not possible to do this automatically using real byte[] . Using the JVM TI may help here, but I suspect it is too much.

Personally, I would write a simple wrapper around byte[] with methods for read() and write() specific fields. Then these methods can track all hits (individually for each byte or as summary or both).

Of course, this requires that the actual access be changed, but if you are testing some algorithms that may not be such a big drawback. The same goes for performance: it will definitely fix a bit, but the effect can be small enough not to worry about it.

0
source

Source: https://habr.com/ru/post/1339020/


All Articles