-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathXMLA - ExecuteQuery.ps1
149 lines (107 loc) · 4.55 KB
/
XMLA - ExecuteQuery.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
param(
$connStr = "Provider=MSOLAP;Integrated Security=SSPI;Persist Security Info=False;Data Source=localhost\sql2019; Initial Catalog = AdventureWorks;SubQueries=2;"
,
$query = "
SELECT NON EMPTY CrossJoin(Hierarchize(CrossJoin({[Geography].[Country Region Name].[Country Region Name].AllMembers}, {([Customer].[Education].[Education].AllMembers)})), {[Measures].[Internet Total Sales],[Measures].[Internet Total Units],[Measures].[Internet Total Tax Amt],[Measures].[Internet Total Product Cost],[Measures].[Internet Total Margin],[Measures].[Internet Total Freight],[Measures].[Internet Distinct Count Sales Order],[Measures].[Internet Total Discount Amount]}) DIMENSION PROPERTIES MEMBER_CAPTION ON COLUMNS , NON EMPTY Hierarchize(CrossJoin({[Customer].[Customer Id].[Customer Id].AllMembers}, {([Date].[Date].[Date].AllMembers,[Geography].[Postal Code].[Postal Code].AllMembers,[Internet Sales].[Due Date].[Due Date].AllMembers,[Product].[Product Id].[Product Id].AllMembers)})) DIMENSION PROPERTIES MEMBER_CAPTION ON ROWS FROM [Adventure Works Internet Sales Model]
"
,
$logRowCount = 10000
,
$executionTimes = 2
,
$executionSleep = 5
,
$reuseConnection = $true
,
$assemblyPath = $null
#$assemblyPath = "C:\Program Files\Microsoft Power BI Desktop\bin\Microsoft.PowerBI.AdomdClient.dll"
#$assemblyPath = "C:\Program Files\On-premises data gateway\Microsoft.AnalysisServices.AdomdClient.dll"
#$assemblyPath = "C:\Program Files\On-premises data gateway\m\Microsoft.PowerBI.AdomdClient.dll"
)
cls
$ErrorActionPreference = "Stop"
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
if (!$assemblyPath)
{
$nugetName = "Microsoft.AnalysisServices.AdomdClient.retail.amd64"
$nugetVersion = "19.54.1"
if (!(Test-Path "$currentPath\Nuget\$nugetName.$nugetVersion" -PathType Container)) {
Install-Package -Name $nugetName -ProviderName NuGet -Scope CurrentUser -RequiredVersion $nugetVersion -SkipDependencies -Destination "$currentPath\Nuget" -Force
}
$assemblyPath = Resolve-Path "$currentPath\Nuget\$nugetName.$nugetVersion\lib\net45\Microsoft.AnalysisServices.AdomdClient.dll"
}
Add-Type -Path $assemblyPath
$assembly = [Microsoft.AnalysisServices.AdomdClient.AdomdConnection].Assembly
Write-Host "Assembly version loaded: '$($assembly.FullName)' from '$($assembly.Location)'"
$report = @()
$conn = $null
for ($i = 1; $i -le $executionTimes; $i++)
{
Write-Host "Execution $i / $executionTimes"
try
{
if (!$reuseConnection -or !$conn)
{
Write-Host "Opening connection"
$conn = new-object Microsoft.AnalysisServices.AdomdClient.AdomdConnection
$conn.ConnectionString = $connStr
$conn.Open()
}
try
{
Write-Host "Executing Query"
$sw2 = $null
$sw = New-Object System.Diagnostics.Stopwatch
$sw.Start()
$cmd = $conn.CreateCommand()
$cmd.CommandText = $query
$reader = $cmd.ExecuteReader()
$rowCount = 0
while($reader.Read())
{
if (!$sw2)
{
$sw2 = New-Object System.Diagnostics.Stopwatch
}
if ($rowCount % $logRowCount -eq 0)
{
Write-Host "Reading... [$rowCount] - [$([datetime]::UtcNow.ToString("yyyy-MM-dd HH:mm:ss"))] - $([Math]::Round($sw2.Elapsed.TotalMilliseconds,0))ms"
$sw2.Restart()
}
$rowCount++
}
}
finally
{
$sw.Stop()
Write-Host "Query Execution: $($sw.Elapsed.TotalSeconds.ToString("N3"))s"
$report += @{ExecutionId = $i; Duration = [Math]::Round($sw.Elapsed.TotalSeconds,3)}
if ($reader)
{
$reader.Dispose()
}
if ($cmd)
{
$cmd.Dispose()
}
}
}
finally
{
if (!$reuseConnection -and $conn -ne $null)
{
Write-Host "Disposing Connection"
$conn.Dispose()
$conn = $null
}
}
Write-Host "Sleeping..."
Start-Sleep -Seconds $executionSleep
}
if ($conn -ne $null)
{
Write-Host "Disposing Connection"
$conn.Dispose()
$conn = $null
}
$report |% { [PSCustomObject]$_ } | format-table -AutoSize