Created
May 29, 2018 19:24
-
-
Save trevren11/2f6ee995d14ab581bc9af2cc47cd47cf to your computer and use it in GitHub Desktop.
YP test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory = $true)] [string] $ImageFolderPath, # folder location of what files you want to load onto your android device | |
[Parameter(Mandatory = $true)] [string] $PhoneName, # name that shows up in Your Phone app and folder name where images will sync to on pc, ie "Galaxy S8" | |
[string] $AndroidPhotoFolderPath = "/sdcard/DCIM/", # android photo folder | |
[Int32] $WaitTime = 60, # How long to wait for full sync | |
[string] $ResultFile = "output.log", | |
[switch] $Help = $FALSE | |
) | |
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | |
# example usage | |
# .\PhotoTest.ps1 -PhoneName "Galaxy S8" -ImageFolderPath \\scratch2\scratch\tren\25 -WaitTime 30 -AndroidPhotoFolderPath "/sdcard/DCIM/Camera/" | |
Write-Host "Pulling images from folder: " -NoNewline | |
Write-Host $ImageFolderPath -ForegroundColor DarkBlue | |
Write-Host "Phone Name: "-NoNewline | |
Write-Host $PhoneName -ForegroundColor DarkBlue | |
Write-Host "Storing photos in android folder: " -NoNewline | |
Write-Host $AndroidPhotoFolderPath -ForegroundColor DarkBlue | |
Write-Host "Time for sync: " -NoNewline | |
Write-Host $WaitTime -ForegroundColor DarkBlue | |
Write-Host "Writing to file: " -NoNewline | |
Write-Host $ResultFile -ForegroundColor DarkBlue | |
$Global:Files = @() | |
$Global:FileNames = @() | |
$loc = resolve-path "$env:LOCALAPPDATA/Packages/Microsoft.YourPhone_8wekyb3d8bbwe/LocalCache/Indexed/*/User/$PhoneName/Recent Photos/" | |
$loc.path | |
$Global:YourPhoneFileLocation = $loc.path | |
$Global:ThumbnailMaxSize = 100kb | |
function GetFiles { | |
Write-Host "Getting files" -ForegroundColor DarkBlue | |
$Global:FileNames = Get-ChildItem "$ImageFolderPath\*" -Include *.png, *.jpg | |
$Global:Files = @() | |
foreach ($s in $FileNames) { | |
$FileName = (Split-Path -Path $s -Leaf).split("\") | |
$Global:Files += $FileName | |
} | |
} | |
function PushFilesToDevice { | |
Write-Host "Sending files to device for sync" -ForegroundColor DarkBlue | |
foreach ($s in $Global:FileNames) { | |
Invoke-Expression "adb push $s $AndroidPhotoFolderPath" | |
} | |
} | |
function RunYourPhoneUiTest { | |
Write-Host "Sending signal to open Your Phone and click refresh" -ForegroundColor DarkBlue | |
Start-Process ms-phone: | |
# this is currently not required as the app starts a sync on launch | |
# try { | |
# Invoke-Expression "MSTest.exe /testcontainer:\\scratch2\scratch\tren\scripts\YourPhoneUI.dll" | |
# } | |
# catch { | |
# $error = "MSTest is not installed, follow the link to install MSTest to run automated test`n" | |
# $error += "https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=TestAgent&rel=15" | |
# Write-Host "$error" -ForegroundColor Red | |
# } | |
} | |
function DeletePhoneFiles { | |
Write-Host "Deleting files on phone" -ForegroundColor DarkBlue | |
foreach ($file in $Global:Files) { | |
Write-Host "Deleting " $file | |
Invoke-Expression "adb shell rm '$AndroidPhotoFolderPath$file'" | |
} | |
} | |
function DeleteAllPCFiles { | |
Write-Host "Deleting all files from pc Your Phone app" -ForegroundColor DarkBlue | |
Remove-item -path $Global:YourPhoneFileLocation -Recurse -force -include *.jpg | |
} | |
function DeletePCFiles { | |
Write-Host "Deleting files from pc" -ForegroundColor DarkBlue | |
foreach ($file in $Global:Files) { | |
$FileToDelete = "$Global:YourPhoneFileLocation$($file)" | |
If (Test-Path $FileToDelete) { | |
Write-Host "Deleting " $file | |
Remove-Item $FileToDelete -force | |
} | |
} | |
} | |
function CreateFileArray { | |
$FileArray = @() | |
foreach ($s in $Global:Files) { | |
$FileObject = New-Object -TypeName PSObject | |
$FileObject | Add-Member -Name 'Name' -MemberType Noteproperty -Value $s | |
$FileObject | Add-Member -Name 'Thumbnail' -MemberType Noteproperty -Value $null | |
$FileObject | Add-Member -Name 'FullImage' -MemberType Noteproperty -Value $null | |
$FileArray += $FileObject | |
} | |
return $FileArray | |
} | |
function VerifySync { | |
Clear-Host | |
$FoundAllFiles = $TRUE | |
$SearchingForFirstSync = $TRUE | |
$SecondsToFirstSync = $null | |
$StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch | |
$StopWatch.Start() | |
$Synced = CreateFileArray | |
while ($StopWatch.Elapsed.TotalSeconds -le $WaitTime) { | |
$FoundAllFiles = $TRUE | |
$output = "Time Elapsed: " | |
$output += $StopWatch.Elapsed.TotalSeconds.ToString("#.0") | |
if ($SecondsToFirstSync) { | |
$output += "`nTook $SecondsToFirstSync seconds for first photo to sync" | |
} | |
$SyncedFiles = GetFilesInYourPhoneFolder | |
# check if all items are present | |
foreach ($f in $Global:Files) { | |
if ($SyncedFiles.Contains($f)) { | |
if ($SearchingForFirstSync) { | |
$SearchingForFirstSync = $FALSE | |
$SecondsToFirstSync = $StopWatch.Elapsed.TotalSeconds.ToString("#.0") | |
WriteToFile("Took $SecondsToFirstSync seconds for first photo to sync") | |
} | |
$FileArray = $Synced | Where-Object -Property Name -Contains $f | |
$file = "$Global:YourPhoneFileLocation$($FileArray.Name)" | |
$IsThumbnail = (Get-Item $file).length -le $Global:ThumbnailMaxSize | |
if ($IsThumbnail) { | |
$FoundAllFiles = $FALSE | |
if ($FileArray.Thumbnail -eq $null) { | |
$FileArray.Thumbnail = $StopWatch.Elapsed.TotalSeconds.ToString("#.0") | |
WriteToFile( "Thumbnail for $($FileArray.Name) synced in $($FileArray.Thumbnail) seconds") | |
} | |
} | |
if ($FileArray.FullImage -eq $null -and -not $IsThumbnail) { | |
$FileArray.FullImage = $StopWatch.Elapsed.TotalSeconds.ToString("#.0") | |
WriteToFile( "Synced $($FileArray.Name) in $($FileArray.FullImage) seconds") | |
} | |
} | |
else { | |
$FoundAllFiles = $FALSE | |
} | |
} | |
if ($FoundAllFiles) { | |
Clear-Host | |
Write-Host $output | |
$success = "`nAll photos synced in " | |
$success += $StopWatch.Elapsed.TotalSeconds.ToString('#.0') | |
$success += " seconds" | |
Write-Host $success -ForegroundColor DarkGreen | |
WriteToFile($output) | |
WriteToFile($success) | |
return | |
} | |
else { | |
$output += "`nSyncing..." | |
Start-Sleep -m 200 | |
} | |
Clear-Host | |
$ConsoleString = $Synced | Format-Table | out-string | |
$ConsoleString += $output | |
Write-Host $ConsoleString | |
} | |
if (-Not $FoundAllFiles) { | |
Write-Host "All files did not sync" -ForegroundColor Red | |
WriteToFile("All files did not sync") | |
} | |
} | |
function GetFilesInYourPhoneFolder { | |
$SyncedFiles = @() | |
$SyncedFileNames = Get-ChildItem "$Global:YourPhoneFileLocation/*" -Include *jpg | |
foreach ($s in $SyncedFileNames) { | |
$FileName = (Split-Path -Path $s -Leaf).split("\") | |
$SyncedFiles += $FileName | |
} | |
return , $SyncedFiles | |
} | |
function VerifyDelete { | |
$RemovedAllFiles = $TRUE | |
$SearchingForFirstDelete = $TRUE | |
$SecondsToFirstDelete = $null | |
$StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch | |
$StopWatch.Start() | |
$Synced = CreateFileArray | |
while ($StopWatch.Elapsed.TotalSeconds -le $WaitTime) { | |
$RemovedAllFiles = $TRUE | |
$output = "Time Elapsed: " | |
$output += $StopWatch.Elapsed.TotalSeconds.ToString("#.0") | |
if ($SecondsToFirstDelete) { | |
$output += "`nTook $SecondsToFirstDelete seconds for first photo to delete" | |
} | |
$DeletedFiles = GetFilesInYourPhoneFolder | |
# check if all items are deleted | |
if (-not $DeletedFiles -eq $null) { | |
foreach ($f in $Global:Files) { | |
if ($DeletedFiles.Contains($f)) { | |
$RemovedAllFiles = $FALSE | |
} | |
else { | |
if ($SearchingForFirstDelete) { | |
$SearchingForFirstDelete = $FALSE | |
$SecondsToFirstDelete = $StopWatch.Elapsed.TotalSeconds.ToString("#.0") | |
WriteToFile("Took $SecondsToFirstDelete seconds for first photo to delete") | |
} | |
$a = $Synced | Where-Object -Property Name -Contains $f | |
if ($a.FullImage -eq $null) { | |
$a.FullImage = $StopWatch.Elapsed.TotalSeconds.ToString("#.0") | |
WriteToFile( "Synced $($a.Name) in $($a.FullImage) seconds") | |
} | |
} | |
} | |
} | |
if ($RemovedAllFiles) { | |
Write-Host $output | |
$success = "`nAll photos deleted in " | |
$success += $StopWatch.Elapsed.TotalSeconds.ToString('#.0') | |
$success += " seconds" | |
Write-Host $success -ForegroundColor DarkGreen | |
WriteToFile($output) | |
WriteToFile($success) | |
return | |
} | |
else { | |
$output += "`nDeleting..." | |
Start-Sleep -m 200 | |
} | |
$Synced | |
Write-Host $output | |
} | |
if (-Not $RemovedAllFiles) { | |
Write-Host "All files did not sync" -ForegroundColor Red | |
WriteToFile("All files did not sync") | |
} | |
} | |
function TakePhonePicture { | |
Invoke-Expression "adb shell 'am start -a android.media.action.IMAGE_CAPTURE'" | |
Start-Sleep -s 1 | |
Invoke-Expression "adb shell 'input keyevent 27'" | |
} | |
function ClearPhoneImages { | |
adb shell rm -rf $AndroidPhotoFolderPath | |
} | |
function LoadTest { | |
# Clear images on phone | |
$remove = Read-Host -Prompt 'This will remove all of the photos in your cameral roll and in Your Phone, continue? [y/n]' | |
if ($remove -eq "y") { | |
ClearPhoneImages | |
DeleteAllPCFiles | |
} | |
$remove = Read-Host -Prompt 'Reset log file? [y/n]' | |
if ($remove -eq "y") { | |
ResetLogFile | |
} | |
Start-Process ms-phone: | |
WriteToFile "Load Test started at $(Get-Date)" | |
# Picture Loop | |
Write-Host "Load test running, press Ctrl + C to stop" | |
# wake | |
adb shell input keyevent KEYCODE_WAKEUP | |
Start-Sleep -s 2 | |
# open camera | |
adb shell input keyevent KEYCODE_CAMERA | |
Start-Sleep -s 2 | |
while ($true) { | |
$oldFiles = adb shell ls sdcard/DCIM/Camera | |
# Write-Host "Old files: $oldFiles" | |
# wake | |
adb shell input keyevent KEYCODE_WAKEUP | |
Start-Sleep -s 2 | |
# take picture | |
adb shell input keyevent KEYCODE_CAMERA | |
# Start-Sleep -s 2 | |
# Turn screen off | |
adb shell input keyevent KEYCODE_SLEEP | |
# get new image name | |
$newFiles = adb shell ls $AndroidPhotoFolderPath | |
# Write-Host "New files:" | |
# $newFiles | |
if ($oldFiles -eq $null){ | |
continue | |
} | |
$diff = Compare-Object -ReferenceObject $oldFiles -DifferenceObject $newFiles | |
$newFile = $diff.InputObject | |
# wait for sync for newFile | |
$path = "$Global:YourPhoneFileLocation/$newFile" | |
$itr = 1 | |
$failedSync = $true | |
while ($itr -le $WaitTime) { | |
$found = Test-Path $path -PathType Leaf | |
if ($found) { | |
$failedSync = $false | |
Write-Host "Image transferred in $itr seconds" | |
WriteToFile "$newFile transferred in $itr seconds" | |
break | |
} | |
Start-Sleep -s (1) | |
$itr += 1 | |
} | |
if ($failedSync){ | |
$failed = "$newFile failed to sync in $itr seconds" | |
WriteToFile $failed | |
Write-Host $failed -BackgroundColor Red | |
} | |
Start-Sleep -s (5) | |
} | |
} | |
function CleanUp { | |
Stop-Process -Name YourPhone -ErrorAction SilentlyContinue | |
GetFiles | |
DeletePhoneFiles | |
DeletePCFiles | |
} | |
function Sync15Photos { | |
# Loads 15 images on phone | |
# clicks refresh on Your Phone pc app | |
# deletes all images on phone and pc when done | |
WriteToFile("Sync 15 Photos start") | |
# GetFiles | |
CleanUp | |
PushFilesToDevice | |
RunYourPhoneUiTest | |
VerifySync | |
DeletePhoneFiles | |
DeletePCFiles | |
WriteToFile("Sync 15 Photos end") | |
} | |
function Sync15PhotosDeleteOnPhone { | |
# Loads 15 images on phone | |
# clicks refresh on Your Phone pc app | |
# deletes all images on phone and waits to see if the images are deleted on disk | |
WriteToFile("Sync 15 Photos Delete On Phone start") | |
CleanUp | |
PushFilesToDevice | |
RunYourPhoneUiTest | |
VerifySync | |
DeletePhoneFiles | |
# wait for pc to remove | |
VerifyDelete | |
DeletePCFiles | |
WriteToFile("Sync 15 Photos Delete On Phone end") | |
} | |
function ResetLogFile { | |
(Get-Date).ToString("yyyy/MM/dd/hh:mm:ss") | Out-File -FilePath $ResultFile | |
} | |
function WriteToFile($str) { | |
$str | Out-File -FilePath $ResultFile -Append | |
} | |
function Show-Usage { | |
Write-Host @" | |
$ScriptName [-Help] <all your flags listed here> | |
This script will run several tests to verify photo latency and syncing for the Your Phone app. | |
"@; | |
} | |
function Main { | |
if ($Help) { | |
Show-Usage | |
exit 1 | |
} | |
Clear-Host | |
ResetLogFile | |
Sync15Photos | |
# Start-Sleep -s 5 | |
# Sync15PhotosDeleteOnPhone | |
CleanUp | |
} | |
# Main | |
LoadTest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment