forked from microsoft/VirtualClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompression7zipExecutor.cs
More file actions
219 lines (190 loc) · 8.76 KB
/
Copy pathCompression7zipExecutor.cs
File metadata and controls
219 lines (190 loc) · 8.76 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace VirtualClient.Actions
{
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using VirtualClient.Common;
using VirtualClient.Common.Extensions;
using VirtualClient.Common.Platform;
using VirtualClient.Common.Telemetry;
using VirtualClient.Contracts;
using VirtualClient.Contracts.Metadata;
/// <summary>
/// The 7zip compression workload executor.
/// </summary>
[SupportedPlatforms("win-x64,win-arm64")]
public class Compression7zipExecutor : VirtualClientComponent
{
private IFileSystem fileSystem;
private IPackageManager packageManager;
private IStateManager stateManager;
private ISystemManagement systemManager;
/// <summary>
/// Constructor for <see cref="Compression7zipExecutor"/>
/// </summary>
/// <param name="dependencies">Provides required dependencies to the component.</param>
/// <param name="parameters">Parameters defined in the profile or supplied on the command line.</param>
public Compression7zipExecutor(IServiceCollection dependencies, IDictionary<string, IConvertible> parameters)
: base(dependencies, parameters)
{
this.systemManager = this.Dependencies.GetService<ISystemManagement>();
this.packageManager = this.systemManager.PackageManager;
this.stateManager = this.systemManager.StateManager;
this.fileSystem = this.systemManager.FileSystem;
}
/// <summary>
/// The options passed to Compressor7zip.
/// </summary>
public string Options
{
get
{
this.Parameters.TryGetValue(nameof(Compression7zipExecutor.Options), out IConvertible options);
return options?.ToString();
}
}
/// <summary>
/// Compressor7zip space separated input files or directories
/// </summary>
public string InputFilesOrDirs
{
get
{
this.Parameters.TryGetValue(nameof(Compression7zipExecutor.InputFilesOrDirs), out IConvertible inputFilesOrDirs);
return inputFilesOrDirs?.ToString();
}
}
/// <summary>
/// The name of the directory where the Compressor7zip is executed.
/// </summary>
protected string Compressor7zipDirectory
{
get
{
return this.GetPackagePath(this.PackageName);
}
}
/// <summary>
/// Executes the Compressor7zip workload.
/// </summary>
protected override async Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
using (BackgroundOperations profiling = BackgroundOperations.BeginProfiling(this, cancellationToken))
{
string commandLineArguments = this.GetCommandLineArguments();
using (IProcessProxy process = await this.ExecuteCommandAsync("7z", commandLineArguments, this.Compressor7zipDirectory, telemetryContext, cancellationToken)
.ConfigureAwait(false))
{
if (!cancellationToken.IsCancellationRequested)
{
await this.LogProcessDetailsAsync(process, telemetryContext, "7Zip", logToFile: true);
process.ThrowIfWorkloadFailed();
this.CaptureMetrics(process, telemetryContext, commandLineArguments);
}
}
}
}
/// <summary>
/// Initializes the environment for execution of the Lzbench workload.
/// </summary>
protected override async Task InitializeAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
Compression7zipState state = await this.stateManager.GetStateAsync<Compression7zipState>($"{nameof(Compression7zipState)}", cancellationToken)
?? new Compression7zipState();
if (!state.Compressor7zipStateInitialized)
{
if (!this.fileSystem.Directory.Exists(this.Compressor7zipDirectory))
{
this.fileSystem.Directory.CreateDirectory(this.Compressor7zipDirectory);
}
// Choose default file for compression and decompression if files/dirs are not provided.
if (string.IsNullOrWhiteSpace(this.InputFilesOrDirs))
{
await this.ExecuteCommandAsync("wget", $"--no-check-certificate https://sun.aei.polsl.pl//~sdeor/corpus/silesia.zip", this.Compressor7zipDirectory, cancellationToken);
await this.ExecuteCommandAsync("unzip", "silesia.zip -d silesia", this.Compressor7zipDirectory, cancellationToken);
}
state.Compressor7zipStateInitialized = true;
}
await this.stateManager.SaveStateAsync<Compression7zipState>($"{nameof(Compression7zipState)}", state, cancellationToken);
}
private void CaptureMetrics(IProcessProxy process, EventContext telemetryContext, string commandArguments)
{
process.ThrowIfNull(nameof(process));
this.MetadataContract.AddForScenario(
"7Zip",
commandArguments,
toolVersion: null);
this.MetadataContract.Apply(telemetryContext);
Compression7zipMetricsParser parser = new Compression7zipMetricsParser(process.StandardOutput.ToString());
IList<Metric> metrics = parser.Parse();
this.Logger.LogMetrics(
"7zip",
this.Scenario,
process.StartTime,
process.ExitTime,
metrics,
null,
commandArguments,
this.Tags,
telemetryContext);
}
private async Task ExecuteCommandAsync(string pathToExe, string commandLineArguments, string workingDirectory, CancellationToken cancellationToken)
{
if (!cancellationToken.IsCancellationRequested)
{
this.Logger.LogTraceMessage($"Executing process '{pathToExe}' '{commandLineArguments}' at directory '{workingDirectory}'.");
EventContext telemetryContext = EventContext.Persisted()
.AddContext("command", pathToExe)
.AddContext("commandArguments", commandLineArguments);
await this.Logger.LogMessageAsync($"{nameof(Compression7zipExecutor)}.ExecuteProcess", telemetryContext, async () =>
{
DateTime start = DateTime.Now;
using (IProcessProxy process = this.systemManager.ProcessManager.CreateElevatedProcess(this.Platform, pathToExe, commandLineArguments, workingDirectory))
{
this.CleanupTasks.Add(() => process.SafeKill());
await process.StartAndWaitAsync(cancellationToken)
.ConfigureAwait(false);
if (!cancellationToken.IsCancellationRequested)
{
await this.LogProcessDetailsAsync(process, telemetryContext)
.ConfigureAwait(false);
process.ThrowIfErrored<WorkloadException>(errorReason: ErrorReason.WorkloadFailed);
}
}
}).ConfigureAwait(false);
}
}
private string GetCommandLineArguments()
{
string inputFilesOrDirs = string.IsNullOrWhiteSpace(this.InputFilesOrDirs)
? this.PlatformSpecifics.Combine(this.Compressor7zipDirectory, "silesia/*")
: this.InputFilesOrDirs;
return @$"{this.Options} {inputFilesOrDirs}";
}
internal class Compression7zipState : State
{
public Compression7zipState(IDictionary<string, IConvertible> properties = null)
: base(properties)
{
}
public bool Compressor7zipStateInitialized
{
get
{
return this.Properties.GetValue<bool>(nameof(Compression7zipState.Compressor7zipStateInitialized), false);
}
set
{
this.Properties[nameof(Compression7zipState.Compressor7zipStateInitialized)] = value;
}
}
}
}
}