Complex Cases. Traffic Class
In dotMemory Unit, memory traffic is represented with the Traffic
type. The object of the Traffic
type is returned as a result of the Memory.GetTrafficFrom* method.
note
* If you use the alternative approach for taking memory snapshots, you should use the
dotMemoryApi.GetTrafficBetween
method for working with memory traffic.
To slice and dice traffic data, you should use a lambda expression passed to the Where
method of the Traffic
type. The Where
method also returns an instance of the Traffic
type allowing you to build chains of Where
methods.
The Traffic
type allows you to get data about allocated and collected memory using two properties: AllocatedMemory
and CollectedMemory
. These properties, in turn, have the ObjectsCount
and SizeInBytes
properties that you can use in test assertions.
For example, to make the assertion that the bar.Foo()
method allocates no more than 1000 objects of the string
type, write the following:
[DotMemoryUnit(CollectAllocations=true)] // collect traffic data
[Test]
public void TestMethod1()
{
var checkPoint1 = dotMemory.Check();
bar.Foo();
dotMemory.Check(memory =>
{
Assert.That(memory.GetTrafficFrom(checkPoint1).Where(traffic => traffic.Type.Is<string>())
.AllocatedMemory.ObjectsCount, Is.LessThan(1000));
});
}
dotMemory Unit also provides alternative syntax for traffic queries. You can use the ==
, &
, and |
logic operators to combine a number of queries. For example:
Assert.That(memory.GetTrafficFrom(memoryCheckPoint2).Where(obj => obj.Type == typeof(Foo) | obj.Interface == typeof(IEnumerable))
.AllocatedMemory.ObjectsCount, Is.LessThan(10));
Another benefit of using the Traffic
class is the ability to group traffic by objects type:
dotMemory.Check(memory =>
{
var traffic = memory.GetTrafficFrom(checkPoint1);
var group = traffic.GroupByType();
var heavyTrafficTypes =
group.Where(typeMemoryInfo => typeMemoryInfo.AllocatedMemoryInfo.SizeInBytes > 1000000).ToList();
// do sth. with heavyTrafficTypes
});
Name | Description |
---|---|
| Gets memory traffic data by a specific condition. The condition is defined by a Returns an instance of the |
| Gets memory traffic data by a specific condition. The condition is defined by a reusable query - an instance of the TrafficPredicateQuery type. For more information, refer to QueryBuilder. Returns an instance of the |
| Returns a collection of objects of the TypeTrafficInfo type. Each object represents traffic of objects of a particular type and carries info about the allocated and collected objects of that type. |
Name | Type | Description |
---|---|---|
|
| Data on allocated objects: object count and total size. |
|
| Data on collected objects: object count and total size. |
The MemoryInfo
struct is used to get data about number of objects and their size.
Name | Type | Description |
---|---|---|
|
| Total number of objects in the set. |
|
| Total size of objects in the set. |
Filters traffic by a specific condition: type, interface, and other.
Name | Type | Description |
---|---|---|
| Allows filtering traffic by object type. | |
| Allows filtering traffic by interface. | |
| Allows filtering traffic by namespace. | |
| Allows filtering traffic by assemblies. |
TypeTrafficInfo
carries info about traffic of objects of the same type: allocated and collected objects. Instances of the TypeTrafficInfo
class are a result of grouping the traffic via the GroupByType()
method.
Name | Type | Description |
---|---|---|
|
| Data on allocated objects of the |
|
| Data on collected objects of the |
|
| Fully qualified name of the |
|
| Type of objects represented by this instance of the Can be |