Complex Scenarios. 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.
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.
Example
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));
Traffic Class
Traffic Methods
Name | Description |
---|---|
Where(Func<TrafficProperty, Query> query): Traffic | Gets memory traffic data by a specific condition. The condition is defined by a Query that should be returned by a lambda expression. The TrafficProperty instance passed to the lambda allows creating queries that filter traffic data by object type, interface, and other parameters. Returns an instance of the Traffic type. |
Where(TrafficQuery predicate): Traffic | Gets memory traffic data by a specific condition. The condition is defined by a reusable query - an instance of the TrafficPredicateQuery type. See QueryBuilder for details. Returns an instance of the Traffic type. |
Traffic Properties
Name | Type | Description |
---|---|---|
AllocatedMemory | MemoryInfo | Data on allocated objects: objects count and total size. |
CollectedMemory | MemoryInfo | Data on collected objects: objects count and total size. |
MemoryInfo Struct
The MemoryInfo
struct is used to get data about number of objects and their size.
MemoryInfo Properties
Name | Type | Description |
---|---|---|
ObjectsCount | int | Total number of objects in the set. |
SizeInBytes | long | Total size of objects in the set. |
TrafficProperty Class
Filters traffic by a specific condition: type, interface, and other.
TrafficProperty Properties
Name | Type | Description |
---|---|---|
Type | TypeProperty | Allows filtering traffic by object type. |
Interface | InterfaceProperty | Allows filtering traffic by interface. |
Namespace | NamespaceProperty | Allows filtering traffic by namespace. |
Assembly | AssemblyProperty | Allows filtering traffic by assemblies. |