Custom counters can be created directly in the database.
The [LoadTestRunId] is needed, from the current load test run in the
[LoadTestRun] table. This id could be retreived by just getting the latest entry for a ControllerName (since a controller can only run one test at the time).
The question is how to get the datasource connection string.
### Example 1
In this case the counter is created as a counter on the highest (root) level in the results counters, using the [CounterCategoryId] for LoadTest:Scenario
in the [LoadTestPerformanceCounterCategory] table (below it had the value 6).
INSERT INTO [dbo].[LoadTestPerformanceCounter]([LoadTestRunId],[CounterCategoryId], [CounterId], [CounterName], [HigherIsBetter])
VALUES (4012, 6, 93, N'CustomCounter', 1)
The CounterId here is generated (last +1) and then used in the instance as well:
INSERT INTO [dbo].[LoadTestPerformanceCounterInstance]([LoadTestRunId],[CounterId], [InstanceId], [LoadTestItemId], [InstanceName], [CumulativeValue], [OverallThresholdRuleResult])
VALUES (4012, 93, 622, NULL, N'Custom', 10, 0)
Here the InstanceId is generated (last +1). InstanceName is the root leef name in the results tree. If the same name as an existing Scenario is used, the counter
is placed in that Scenarios branch. If it doesnt exist, it will be placed by its own in the root (as a new Scenario).
### Example 2
In this case we create a CUSTOM Computer in the Computers root list. It can have it's own categories, counters and instances or counters with a single instance.
INSERT INTO [dbo].[LoadTestPerformanceCounterCategory]([LoadTestRunId],[CounterCategoryId], [CategoryName], [MachineName], [StartTimeStamp100nSec])
CounterId from above, InstanceId is generated (last +1), InstanceName is a name for the instance or systemdiagnosticsperfcounterlibsingleinstance as above
CumulativeValue seems to have effect on the graphs max value for the y axis to calculate a range to be used.
### Creating values
Once the counters and instances are set, values (measurements) can be created in the [LoadTestPerformanceCounterSample] table. Below an example for the "User Load" counter