Skip to content

Commit

Permalink
tidy logging, lamire upfront threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLloyd committed Oct 13, 2024
1 parent 0c7ca6c commit 9a1d990
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 218 deletions.
2 changes: 1 addition & 1 deletion CsCheck/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// limitations under the License.
namespace CsCheck;
using System.Threading.Channels;
using Logging;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using CsCheck;

/// <summary>Main random testing Check functions.</summary>
public static partial class Check
Expand Down
57 changes: 57 additions & 0 deletions CsCheck/Logging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace CsCheck;

using System.Text.Json;
using System.Threading.Channels;

public static class GenLogger
{
public record LogContext<T>(T Value, bool Success);

public enum LogProcessor
{
Tyche
}

public static Func<(Func<Task> loggingTask, Channel<LogContext<T>>)> CreateLogger<T>(StreamWriter w, LogProcessor p, string propertyUnderTest)
{
return () =>
{
w.AutoFlush = true;
var channel = Channel.CreateUnbounded<LogContext<T>>(
new UnboundedChannelOptions
{
SingleReader = true,
SingleWriter = false
}
);
switch (p)
{
case LogProcessor.Tyche:
var t = async () =>
{
while (await channel.Reader.WaitToReadAsync().ConfigureAwait(false))
{
var d = new Dictionary<string, string>(StringComparer.Ordinal);
var item = await channel.Reader.ReadAsync().ConfigureAwait(false);
var value = item.Value;
var timestamp = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds() / 1000.0;
var tycheData = new TycheData(
"test_case", timestamp, propertyUnderTest,
item.Success ? "passed" : "failed", JsonSerializer.Serialize(value),
"reason", d, "testing", d, null, d, d
);
var serializedData = JsonSerializer.Serialize(tycheData);
await w.WriteLineAsync(serializedData).ConfigureAwait(false);
}
};
return (t, channel);
default:
throw new ArgumentOutOfRangeException(nameof(p), p, null);
}
};
}
}

public record TycheData(string type, double run_start, string property, string status, string representation,
string? status_reason, Dictionary<string, string> arguments, string? how_generated, Dictionary<string, string> features,
Dictionary<string, string>? coverage, Dictionary<string, string> timing, Dictionary<string, string> metadata);
55 changes: 0 additions & 55 deletions CsCheck/Logging/Logging.cs

This file was deleted.

6 changes: 0 additions & 6 deletions CsCheck/Logging/TycheData.cs

This file was deleted.

94 changes: 43 additions & 51 deletions Tests/GenLogsTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Text;
namespace Tests;
using System.Text;
using System.Text.Json;
using CsCheck;
using CsCheck.Logging;

namespace Tests;

public class GenLogsTest
{
Expand All @@ -19,41 +17,38 @@ static int[] Tally(int n, int[] ia)
[InlineData(0)]
public void Bool_Distribution_WithTycheLogs(int generatedIntUponTrue)
{
using (MemoryStream memoryStream = new())
using (StreamWriter writer = new StreamWriter(memoryStream))
{
var loggerFunc = GenLogger.CreateLogger<int[]>(writer, GenLogger.LogProcessor.Tyche,
"Bool_Distribution_WithTycheLogs");
using var memoryStream = new MemoryStream();
using var writer = new StreamWriter(memoryStream);
var loggerFunc = GenLogger.CreateLogger<int[]>(writer, GenLogger.LogProcessor.Tyche,
"Bool_Distribution_WithTycheLogs");

// Random test logic
const int frequency = 10;
var expected = Enumerable.Repeat(frequency, 2).ToArray();
// Random test logic
const int frequency = 10;
var expected = Enumerable.Repeat(frequency, 2).ToArray();

//Try catch to suppress failing original test logic
try
{
Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array[2 * frequency]
.Select(sample => Tally(2, sample))
.Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2, loggerFunc:loggerFunc);
}
catch
{
}

//Actual logic we want to test.
memoryStream.Position = 0;
string json;
using (StreamReader reader = new StreamReader(memoryStream, Encoding.UTF8))
{
json = reader.ReadToEnd();
}
//Try catch to suppress failing original test logic
try
{
Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array[2 * frequency]
.Select(sample => Tally(2, sample))
.Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2, loggerFunc: loggerFunc);
}
catch
{
}

var tycheData = JsonSerializer.Deserialize<TycheData>(json);
//Actual logic we want to test.
memoryStream.Position = 0;
string json;
using (var reader = new StreamReader(memoryStream, Encoding.UTF8))
{
json = reader.ReadToEnd();
}

Assert.True(tycheData != null && LogCheck(tycheData));
Assert.True(JsonSerializer.Deserialize<int[]>(tycheData.representation)?.Sum() == 20);
var tycheData = JsonSerializer.Deserialize<TycheData>(json);

}
Assert.True(tycheData != null && LogCheck(tycheData));
Assert.Equal(20, JsonSerializer.Deserialize<int[]>(tycheData.representation)?.Sum());

bool LogCheck(TycheData td)
{
Expand All @@ -71,25 +66,22 @@ bool LogCheck(TycheData td)
public void Bool_Distribution_WithTycheLogs_ToFile(int generatedIntUponTrue)
{
var projectRoot = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\.."));
using (StreamWriter writer = new StreamWriter(Path.Combine(projectRoot, "Logging", "Testrun.jsonl")))
{
var loggerFunc = GenLogger.CreateLogger<int[]>(writer, GenLogger.LogProcessor.Tyche,
"Bool_Distribution_WithTycheLogs");
using var writer = new StreamWriter(Path.Combine(projectRoot, "Logging", "Testrun.jsonl"));
var loggerFunc = GenLogger.CreateLogger<int[]>(writer, GenLogger.LogProcessor.Tyche, "Bool_Distribution_WithTycheLogs");

// Random test logic
const int frequency = 10;
var expected = Enumerable.Repeat(frequency, 2).ToArray();
// Random test logic
const int frequency = 10;
var expected = Enumerable.Repeat(frequency, 2).ToArray();

//Try catch to suppress failing original test logic
try
{
Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array[2 * frequency]
.Select(sample => Tally(2, sample))
.Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 100, time: -2, loggerFunc:loggerFunc);
}
catch
{
}
//Try catch to suppress failing original test logic
try
{
Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array[2 * frequency]
.Select(sample => Tally(2, sample))
.Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 100, time: -2, loggerFunc: loggerFunc);
}
catch
{
}
}
}
Loading

0 comments on commit 9a1d990

Please sign in to comment.