Skip to content

Commit 89c9029

Browse files
stuff
1 parent a2dc391 commit 89c9029

File tree

3 files changed

+206
-3
lines changed

3 files changed

+206
-3
lines changed

Invoke-RandomWorkload.ps1

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# With thanks to Jonathan Kehayias and Pieter Vanhove
2+
3+
<#
4+
.SYNOPSIS
5+
Runs a random workload against a database using a sql file
6+
7+
.DESCRIPTION
8+
Runs a random workload against a database using PoshRSJobs to create parallel jobs to run random
9+
queries from a T-SQL file by default it uses the AdventureWorksBOLWorkload.sql from Pieter Vanhove
10+
11+
.PARAMETER SqlInstance
12+
The SQL instance to run the queries against
13+
14+
.PARAMETER SqlCredential
15+
The SQL Credential for the Instance if required
16+
17+
.PARAMETER Database
18+
The name of the database to run the queries against
19+
20+
.PARAMETER NumberOfJobs
21+
The number of jobs to create - default 10
22+
23+
.PARAMETER Delay
24+
The delay in seconds for the output for the running jobs - default 10
25+
26+
.PARAMETER Throttle
27+
The number of parallel jobs to run at a time - default 5
28+
29+
.PARAMETER PathToScript
30+
The path to the T-SQL script holding the queries - default 'C:\temp\AdventureWorksBOLWorkload\AdventureWorksBOLWorkload.sql'
31+
32+
.PARAMETER Delimiter
33+
The delimiter in the T-SQL Script between the queries - default ------
34+
35+
.PARAMETER ShowOutput
36+
Shows the output from the jobs
37+
38+
.EXAMPLE
39+
Invoke-RandomWorkload -SqlInstance $SQL2019CTP23 -SqlCredential $cred -Database AdventureWorks2014 -NumberOfJobs 100 -Delay 10 -Throttle 10
40+
41+
Runs 100 queries with a maximum of 10 at a time against the AdventureWorks2014 database on $SQL2019CTP23
42+
43+
.EXAMPLE
44+
$x = 10
45+
while($X -gt 0){
46+
Invoke-RandomWorkload -SqlInstance $SQL2019CTP23 -SqlCredential $cred -Database AdventureWorks2014 -NumberOfJobs 1000 -Delay 10 -Throttle 10
47+
$x --
48+
}
49+
50+
Runs 1000 queries with a maximum of 10 at a time against the AdventureWorks2014 database on $SQL2019CTP23 10 times in a loop
51+
52+
53+
.NOTES
54+
With thanks to Pieter Vanhove
55+
https://blogs.technet.microsoft.com/msftpietervanhove/2016/01/08/generate-workload-on-your-azure-sql-database/
56+
and
57+
Jonathan Kehayias
58+
https://www.sqlskills.com/blogs/jonathan/the-adventureworks2008r2-books-online-random-workload-generator/
59+
#>
60+
61+
function Invoke-RandomWorkload {
62+
#Requires -Module PoshRsJob
63+
#Requires -Module SQLServer
64+
Param(
65+
[string]$SqlInstance,
66+
[pscredential]$SqlCredential,
67+
[string]$Database,
68+
[int]$NumberOfJobs = 10,
69+
[int]$Delay = 10,
70+
[int]$Throttle = 5,
71+
[string]$PathToScript = 'C:\temp\AdventureWorksBOLWorkload\AdventureWorksBOLWorkload.sql',
72+
[string]$Delimiter = "------",
73+
[switch]$ShowOutput
74+
)
75+
76+
#Check if there are old Workload Jobs
77+
$WorkloadJobs = Get-RSJob -Name Workload
78+
if ($WorkloadJobs) {
79+
Write-Output "Removing Old WorkLoad Jobs"
80+
$WorkloadJobs |Stop-RSJob
81+
$WorkloadJobs | Remove-RSJob
82+
}
83+
Write-Output "Creating Background Jobs"
84+
85+
1.. $NumberOfJobs | Start-RSJob -Name "WorkLoad" -Throttle $Throttle -ScriptBlock {
86+
87+
# Get the queries
88+
$Queries = Get-Content -Delimiter $Using:Delimiter -Path $Using:PathToScript
89+
# Pick a Random Query from the input object
90+
$Query = Get-Random -InputObject $Queries
91+
# Run the Query
92+
Invoke-SqlCmd -ServerInstance $Using:SqlInstance -Credential $Using:SqlCredential -Database $Using:Database -Query $Query
93+
# Choose a random number of milliseconds to wait
94+
$a = Get-Random -Maximum 2000 -Minimum 100;
95+
Start-Sleep -Milliseconds $a;
96+
}
97+
98+
$runningJobs = (Get-RSJob -Name WorkLoad -State Running).Count
99+
While ($runningJobs -ne 0) {
100+
$jobs = Get-RSJob -Name WorkLoad
101+
$runningJobs = $Jobs.Where{$PSItem.State -eq 'Running'}.Count
102+
$WaitingJobs = $Jobs.Where{$PSItem.State -eq 'NotStarted'}.Count
103+
$CompletedJobs = $Jobs.Where{$PSItem.State -eq 'Completed'}.Count
104+
105+
Write-Output "$runningJobs jobs running - $WaitingJobs jobs waiting - $CompletedJobs -jobs finished"
106+
Start-Sleep -Seconds $Delay
107+
}
108+
Write-Output "Jobs have finished"
109+
if ($ShowOutput) {
110+
Write-Output "WorkLoad Jobs Output below -"
111+
Get-RSJob -Name WorkLoad | Receive-RSJob
112+
}
113+
Write-Output "Removing Old WorkLoad Jobs"
114+
Get-RSJob -Name WorkLoad | Remove-RSJob
115+
Write-Output "Finished"
116+
}

New-GitHubRepository.ps1

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ Function New-GitHubRepository {
2525
#write full native response to the pipeline
2626
[switch]$Raw
2727
)
28-
28+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
2929
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
3030
#display PSBoundparameters formatted nicely for Verbose output
3131
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
3232
Write-Verbose "[BEGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*2)$_"}) | Out-String) `n"
3333

3434
#create the header
3535
$head = @{
36-
Authorization = 'Basic ' + $UserToken
36+
Authorization = 'token ' + $UserToken
3737
}
38-
38+
39+
$msg = $head | ConvertTo-Json
40+
Write-Verbose $msg
3941
#create a hashtable from properties
4042
$hash = @{
4143
name = $Name

get-functioncalls.ps1

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Function Get-FunctionCalls {
2+
<#
3+
.SYNOPSIS
4+
Get's the functions called in a script and the the file containing those definitions
5+
6+
.DESCRIPTION
7+
8+
9+
.PARAMETER FunctionName
10+
The root function name you're wanting to process
11+
12+
.PARAMETER FunctionPaths
13+
An object linking a function's name to the the file containing it's definition.
14+
Is built by the first call, and then passed to later calls.
15+
16+
.PARAMETER ModulePath
17+
Path to the root of the powershell module you're analyzing
18+
19+
.PARAMETER Depth
20+
How deep to recurse into the function calls.
21+
Default is 1. This will just load the functions called within the first function. Higher numbers increase the level of recurions, increasing the number of function definitions retrieved, and slowing down the Chronometer run
22+
0 - Will only load the definition of the root function
23+
24+
.EXAMPLE
25+
$Path = Get-FunctionCalls -FunctionName Restore-DbaDataBase -ModulePath C:\dbatools\module\ -depth 1
26+
$Chronometer = @{
27+
Path = $Path.FileName
28+
Script = {Restore-DbaDatabase -SqlServer localhost\sqlexpress2016 -Path C:\dbatools\backups -WithReplace -OutputScriptOnly}
29+
}
30+
$results = Get-Chronometer @Chronometer
31+
32+
Will scan all the ps1 files in c:\dbatools\Module for function definitions, and then will scan the definition of
33+
Restore-DbaDatabase for function calls. As depth is set to 1, it will then add the definitions of those called functions
34+
from within the module to the Chronometer path. Higher depth values will scan for the definition of functions called in
35+
the next tier of functions recursively.
36+
37+
.NOTES
38+
Original Author: Stuart Moore (@napalmgram), https://stuart-moore.com
39+
Source tracked at https://github.com/Stuart-Moore/VariousHelperFunctions/blob/master/Get-FunctionCalls.ps1
40+
Written as a helper function to Kevin Marquette's Chronometer module (https://github.com/KevinMarquette/Chronometer)
41+
42+
This parses the function to be analysed for it's definition and other functions it calls, so that an entire
43+
stack can be monitored without needin to manually build up the path or adding every file in the module and
44+
killing performance
45+
46+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
47+
48+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
49+
50+
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
51+
#>
52+
[CmdletBinding()]
53+
param (
54+
[string[]]$FunctionName,
55+
[object[]]$FunctionPaths,
56+
[String]$ModulePath,
57+
[int]$depth=1
58+
)
59+
if ($FunctionPaths -eq $null)
60+
{
61+
Write-Verbose "$FunctionName - Need to populate the paths to Function calls"
62+
$files = Get-ChildItem $ModulePath -filter *.ps1 -recurse
63+
$FunctionPaths = @()
64+
$null = $files | ForEach{Get-Content $_.FullName | Where-Object {$_ -match 'function\s(\w+-\w+)'} | %{$FunctionPaths += [PSCustomObject]@{Function = $Matches[1]; FileName = $_.PsPath}}}
65+
}
66+
$FunctionFile = $FunctionPaths | Where-Object {$_.Function -eq $FunctionName}
67+
$FileContents = Get-Content ($FunctionFile.FileName)
68+
$funcs = $FileContents | Where-Object {$_ -match '[^\s|(]\w+-\w+'} | ForEach{$Matches[0]} | Select-Object -Unique
69+
$results = @()
70+
if ($depth -ge 1){
71+
Foreach ($func in $funcs){
72+
$results += $FunctionPaths | Where-Object {$_.Function -eq $func}
73+
}
74+
}else{
75+
$results = $FunctionFile
76+
}
77+
if ($depth -gt 1)
78+
{
79+
Foreach ($call in $results)
80+
{
81+
$results += Get-FunctionCalls -FunctionName $call.Function -FunctionPaths $FunctionPaths -depth ($depth-1) -ModulePath $ModulePath
82+
}
83+
}
84+
$results | Select-Object -Unique -property Function,FileName
85+
}

0 commit comments

Comments
 (0)