Skip to content

Commit 5cf814b

Browse files
authored
Merge pull request #12 from Matticusau/SQL2017
Improved ErrorHandling, Assembly loading, support for up to SQL 2017
2 parents 818a376 + a543633 commit 5cf814b

11 files changed

Lines changed: 1082 additions & 42 deletions

CHANGELOG.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Change log for SqlWorkloadGenerator
2+
3+
## Unreleased
4+
5+
* None
6+
7+
### 1.1.0.0
8+
9+
* Created version independant .sql files
10+
* Moved the Example_RunWorkload.ps1 to .\Examples
11+
* Renamed Example_RunWorkload.ps1 to AzureSQLDbExample.ps1
12+
* Created .\Examples\SQLServerExample.ps1
13+
* Created .\Examples\LoadTestExample.cmd
14+
* Improved error handling
15+
* SQL Assemblies are loaded by importing either SqlServer or SQLPS module
16+
17+
### 0.1.2
18+
19+
* Moved functions to a module for better code coverage
20+
* Improved test coverage through AppVeyor
21+
22+
### 0.1.1
23+
24+
* Added Duration parameter and better error verbatim (Issue4)
25+
26+
### 0.1.0
27+
28+
* Added functions Invoke-Workload, Invoke-WorkloadSetup, Invoke-WorkloadQuery (Issue6)
29+
30+
### 0.0.3
31+
32+
* Added ability to specify frequency for improved control over workload level
33+
34+
### 0.0.2
35+
36+
* Added minor fixes (issue #1) removal of Write-Host
37+
38+
39+
### 0.0.1
40+
41+
* Initial release includes basic functionality and sample scripts
42+
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<#
22
.NOTES
33
4-
Script: Example_RunWorkload.ps1
4+
Script: AzureSqlDbExample.ps1
55
Author: Matt Lavery (https://github.com/Matticusau/SqlWorkloadGenerator)
66
Created: 29/05/2015
77
Version: 0.0.1
@@ -10,6 +10,7 @@
1010
Version Who When What
1111
--------------------------------------------------------------------------------------------------
1212
0.0.1 MLavery 29/05/2015 Initial Coding
13+
0.0.2 MLavery 09/11/2017 Renamed from Example_RunWorkload.ps1 to AzureSQLDbExample.ps1
1314
1415
DISCLAIMER
1516
This Sample Code is provided for the purpose of illustration only and is not intended to be

Examples/LoadTestExample.cmd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@echo off
2+
REM Script: LoadTestExample.cmd
3+
REM Author: Matt Lavery (https://github.com/Matticusau/SqlWorkloadGenerator)
4+
REM Created: 09/11/2017
5+
REM Version: 0.0.1
6+
REM
7+
REM DISCLAIMER
8+
REM This Sample Code is provided for the purpose of illustration only and is not intended to be
9+
REM used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED
10+
REM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
11+
REM TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant
12+
REM You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and
13+
REM distribute the object code form of the Sample Code, provided that You agree: (i) to not use
14+
REM Our name, logo, or trademarks to market Your software product in which the Sample Code is
15+
REM embedded; (ii) to include a valid copyright notice on Your software product in which the
16+
REM Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our
17+
REM suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise
18+
REM or result from the use or distribution of the Sample Code.
19+
20+
Start PowerShell.exe -File "C:\Workload\RunWorkload.ps1" -SQLServer "SQLNode01\Prd01" -Database "AdventureWorks2014" -TSQLFile "C:\Workload\SqlScripts\AdventureWorksWorkload.sql" -Frequency "Fast"
21+
Start PowerShell.exe -File "C:\Workload\RunWorkload.ps1" -SQLServer "SQLNode01\Prd01" -Database "AdventureWorks2014" -TSQLFile "C:\Workload\SqlScripts\AdventureWorksWorkload.sql" -Frequency "Fast"
22+
Start PowerShell.exe -File "C:\Workload\RunWorkload.ps1" -SQLServer "SQLNode01\Prd01" -Database "AdventureWorks2014" -TSQLFile "C:\Workload\SqlScripts\AdventureWorksWorkload.sql" -Frequency "Normal"
23+
Start PowerShell.exe -File "C:\Workload\RunWorkload.ps1" -SQLServer "SQLNode01\Prd01" -Database "AdventureWorks2014" -TSQLFile "C:\Workload\SqlScripts\AdventureWorksWorkload.sql" -Frequency "Fast"
24+
Start PowerShell.exe -File "C:\Workload\RunWorkload.ps1" -SQLServer "SQLNode01\Prd01" -Database "AdventureWorks2014" -TSQLFile "C:\Workload\SqlScripts\AdventureWorksWorkload.sql" -Frequency "Fast"
25+
PAUSE

Examples/SQLServerExample.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<#
2+
.NOTES
3+
4+
Script: SQLServerExample.ps1
5+
Author: Matt Lavery (https://github.com/Matticusau/SqlWorkloadGenerator)
6+
Created: 09/11/2017
7+
Version: 0.0.1
8+
9+
Change History
10+
Version Who When What
11+
--------------------------------------------------------------------------------------------------
12+
0.0.1 MLavery 09/11/2017 Initial Coding
13+
14+
DISCLAIMER
15+
This Sample Code is provided for the purpose of illustration only and is not intended to be
16+
used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED
17+
"AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
18+
TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant
19+
You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and
20+
distribute the object code form of the Sample Code, provided that You agree: (i) to not use
21+
Our name, logo, or trademarks to market Your software product in which the Sample Code is
22+
embedded; (ii) to include a valid copyright notice on Your software product in which the
23+
Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our
24+
suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise
25+
or result from the use or distribution of the Sample Code.
26+
#>
27+
28+
Set-Location C:\WorkLoad
29+
30+
.\RunWorkload.ps1 -SQLServer svrSqlDev01.contoso.com -Database AdventureWorks2017 -UserName DemoUsr -Password ?????? -TSQLFile C:\WorkLoad\SqlScripts\AdventureWorksWorkload.sql

README.md

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@ Provides sample SQL Scripts and PowerShell scripts to automate the generation of
99
Current supported versions are:
1010
- SQL 2008 / R2
1111
- SQL 2012
12-
- SQL 2014 (via 2012 script)
12+
- SQL 2014
13+
- SQL 2016
14+
- SQL 2017
1315
- SQL Azure
1416

17+
This tool is currently only supported on Windows and PowerShell. It has not yet been tested on PowerShell Core. It will though work remotely against SQL Server on Linux.
18+
19+
NOTE: For SQL Server 2014 and higher, use the non-version specific scripts.
20+
21+
## Installation
22+
To install and use this toolset follow these steps.
23+
1. Create a folder *C:\Workload* or other suitable location
24+
2. Download the project source or one of the releases to that location you just created
25+
3. Create a wrapper script or start the RunWorkload.ps1 script from the console depending on your needs. Refer to the examples for various ways to execute this workload.
26+
1527
## Example
1628

1729
This example runs a single workload session against a database running in SQL Azure with a fast frequency between batches
@@ -21,13 +33,17 @@ Set-Location C:\WorkLoad
2133
.\RunWorkload.ps1 -SQLServer myazuredb.database.windows.net -Database DemoDB01 -UserName DemoUsr -Password ?????? -TSQLFile C:\WorkLoad\AdventureWorksAzureBOLWorkload.sql -Frequency 'Fast'
2234
```
2335

36+
For further examples see the files provided in [.\Examples](Examples\) folder.
37+
2438
## Running Larger Workloads
2539

2640
To run a larger workload you may need to execute this script multiple times. To achieve this create a .cmd file with multiple lines such as:
2741
```
2842
Start PowerShell.exe -File "C:\Workload\RunWorkload.ps1" -SQLServer "SqlServer.contoso.com" -Database "DemoDB01" -TSQLFile "C:\Workload\AdventureWorks2012BOLWorkload.sql" -Frequency "Fast"
2943
```
3044

45+
For an example see the file [.\Examples\LoadTestExample.cmd](Examples\LoadTestExample.cmd)
46+
3147
## PowerShell Execution Policies
3248

3349
As this workload is generated through a PowerShell script, it is not excempt from PowerShell Execution Policies. The default policy is Restricted which will stop you from executing the workload script. In production environments it is **not** recommended to set this to Unrestricted, instead you should use `RemoteSigned` or `AllSigned` and then code sign the script files with your organisations certificate.
@@ -67,32 +83,6 @@ The frequency of which to run the statements at (Fast, Normal, or Slow)
6783
### Duration
6884
The duration to run the workload for (seconds). 0 is unlimited, otherwise maximum allowed is 172800 (48hrs)
6985

70-
## Versions
71-
72-
### 0.1.2
73-
74-
* Moved functions to a module for better code coverage
75-
* Improved test coverage through AppVeyor
76-
77-
### 0.1.1
78-
79-
* Added Duration parameter and better error verbatim (Issue4)
80-
81-
### 0.1.0
82-
83-
* Added functions Invoke-Workload, Invoke-WorkloadSetup, Invoke-WorkloadQuery (Issue6)
84-
85-
### 0.0.3
86-
87-
* Added ability to specify frequency for improved control over workload level
88-
89-
### 0.0.2
90-
91-
* Added minor fixes (issue #1) removal of Write-Host
92-
93-
94-
### 0.0.1
95-
96-
* Initial release includes basic functionality and sample scripts
86+
## Change log
87+
A full list of changes in each version can be found in the [change log](CHANGELOG.md).
9788

98-

RunWorkload.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Script: RunWorkload.ps1
55
Author: Matt Lavery (https://github.com/Matticusau/SqlWorkloadGenerator)
66
Created: 29/05/2015
7-
Version: 0.1.1
7+
Version: 0.1.2
88
99
Change History
1010
Version Who When What

RunWorkload/RunWorkload.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'RunWorkload.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.0.0.0'
15+
ModuleVersion = '1.1.0.0'
1616

1717
# ID used to uniquely identify this module
1818
GUID = 'cd04fe86-97b8-473b-b104-b8e8040df9fe'

RunWorkload/RunWorkload.psm1

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
Script: RunWorkload.psm1
33
Author: Matt Lavery (https://github.com/Matticusau/SqlWorkloadGenerator)
44
Created: 05/05/2016
5-
Version: 1.0.0.0
5+
Version: 1.1.0.0
66
77
Change History
88
Version Who When What
99
--------------------------------------------------------------------------------------------------
1010
1.0.0.0 MLavery 05/05/2016 Initial Coding from existing scripts (Issue6)
11+
1.1.0.0 MLavery 09/11/2017 Better error handling and uses the SQL modules to load assemblies
1112
1213
1314
DISCLAIMER
@@ -102,9 +103,9 @@ function Invoke-SqlWorkload
102103
catch
103104
{
104105
#Report the error
105-
Write-Warning "Could not execute"
106-
Write-Warning "$($_.Exception.Message)";
107-
#throw $_;
106+
Write-Warning "Could not execute via Invoke-SqlWorkloadQuery"
107+
Write-Warning "$($PSItem.Exception.Message)";
108+
#throw $PSItem;
108109
}
109110
finally
110111
{
@@ -171,13 +172,23 @@ function Invoke-SqlWorkloadQuery
171172
Write-Debug "`$Query = $($Query)";
172173

173174
}
175+
# SQL Exception
176+
catch [System.Data.SqlClient.SqlException],[Microsoft.SqlServer.Management.Common.ExecutionFailureException]
177+
{
178+
# Use Verbose for troubleshooting
179+
Write-Verbose "Failed to execute: $($Query)";
180+
# we need to go a few levels deep to get the actual exception from sql
181+
Write-Verbose "$($PSItem.Exception.InnerException.InnerException.Message)";
182+
# throw $PSItem;
183+
}
184+
# All Other Exception
174185
catch
175186
{
176-
#Use Verbose for troubleshooting
187+
# Use Verbose for troubleshooting
177188
Write-Verbose "Failed to execute: $($Query)";
178-
#we need to go a few levels deep to get the actual exception from sql
179-
Write-Verbose "$($psitem.Exception.InnerException.InnerException.Message)";
180-
#throw $_;
189+
# General error so just use the exception
190+
Write-Verbose "$($PSItem.Exception.Message)";
191+
# throw $PSItem;
181192
}
182193
finally
183194
{
@@ -250,11 +261,26 @@ function Invoke-SqlWorkloadSetup
250261
{
251262
#Write an error
252263
Write-Error "Setup Script Failed"
253-
#throw $_;
264+
#throw $PSItem;
254265
Exit; # stop the script execution
255266
}
256267
}
257268

258269

259270
# Load the SMO assembly
260-
[void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");
271+
# [void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");
272+
# Import the SQL Module to load the assemblies
273+
if (-not(Get-module -Name SqlServer -ListAvailable -ErrorAction SilentlyContinue))
274+
{
275+
Import-Module -Name SqlServer;
276+
}
277+
elseif (-not(Get-module -Name SQLPS -ListAvailable -ErrorAction SilentlyContinue))
278+
{
279+
Import-Module -Name SqlServer -DisableNameChecking;
280+
}
281+
else
282+
{
283+
Write-Error "Neither the SqlServer or SQLPS PowerShell Modules are installed"
284+
#throw $PSItem;
285+
Exit; # stop the script execution
286+
}

0 commit comments

Comments
 (0)