Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from appmetr/netstandard
Browse files Browse the repository at this point in the history
Added netstandard support; Changed default json serializer to latest Json.NET; Moved from log4net to Common.Logging
  • Loading branch information
mastergenius authored Sep 9, 2021
2 parents 0f2c454 + c4347c7 commit 73ab090
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 810 deletions.
57 changes: 6 additions & 51 deletions AppmetrS2S.Tests/AppMetrJsonSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using AppmetrS2S.Actions;
using AppmetrS2S.Persister;
using AppmetrS2S.Serializations;
Expand All @@ -17,25 +16,11 @@ public AppMetrJsonSerializationTests(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void SerializersShouldReturnsEqualsValues()
{
var batch = CreateBatch(50000);

var defaultSerializer = new JavaScriptJsonSerializer();
var cacheSerializer = new JavaScriptJsonSerializerWithCache();

var defaultJson = defaultSerializer.Serialize(batch);
var newtonsoftJson = cacheSerializer.Serialize(batch);

Assert.Equal(defaultJson, newtonsoftJson);
}


[Fact]
public void SerializeServerInstall()
{
var defaultSerializer = new JavaScriptJsonSerializer();
var defaultSerializer = new BasicJsonSerializer();
var install = Events.ServerInstall("test").SetProperties(new Dictionary<string, object>
{
{"key", "value"}
Expand All @@ -52,36 +37,6 @@ public void SerializeServerInstall()
Assert.Equal("value", receivedAction.GetProperties()["key"]);
}

[Fact]
public void SerializersBench()
{
var batch = CreateBatch(10000);

var defaultSerializer = new JavaScriptJsonSerializer();
var cacheSerializer = new JavaScriptJsonSerializerWithCache();

defaultSerializer.Serialize(batch);
cacheSerializer.Serialize(batch);

const int iterationsCount = 100;

var cacheTime = Stopwatch.StartNew();
for (var i = 0; i < iterationsCount; i++)
{
cacheSerializer.Serialize(batch);
}
cacheTime.Stop();

var defaultTime = Stopwatch.StartNew();
for (var i = 0; i < iterationsCount; i++)
{
defaultSerializer.Serialize(batch);
}
defaultTime.Stop();

_output.WriteLine("Default: " + defaultTime.Elapsed);
_output.WriteLine("Newtonsoft: " + cacheTime.Elapsed);
}

[Fact]
public void SerializePayment()
Expand All @@ -92,7 +47,7 @@ public void SerializePayment()
};
var batch = new Batch(Guid.NewGuid().ToString(), 1, events);

var defaultSerializer = new JavaScriptJsonSerializer();
var defaultSerializer = new BasicJsonSerializer();

var json = defaultSerializer.Serialize(batch);

Expand All @@ -109,7 +64,7 @@ public void SerializeUserTime()
var events = new List<AppMetrAction> { e };
var batch = new Batch(Guid.NewGuid().ToString(), 1, events);

var defaultSerializer = new JavaScriptJsonSerializer();
var defaultSerializer = new BasicJsonSerializer();

var json = defaultSerializer.Serialize(batch);

Expand All @@ -122,7 +77,7 @@ public void SerializeUserTime_not_specified()
var events = new List<AppMetrAction> { new Event("test") };
var batch = new Batch(Guid.NewGuid().ToString(), 1, events);

var defaultSerializer = new JavaScriptJsonSerializer();
var defaultSerializer = new BasicJsonSerializer();

var json = defaultSerializer.Serialize(batch);

Expand All @@ -132,7 +87,7 @@ public void SerializeUserTime_not_specified()
[Fact]
public void SerializeAttachEntityAttributes()
{
var defaultSerializer = new JavaScriptJsonSerializer();
var defaultSerializer = new BasicJsonSerializer();
var attach = new AttachEntityAttributes("$serverUserId", "testId");
var events = new List<AppMetrAction> { attach };
var batch = new Batch(Guid.NewGuid().ToString(), 1, events);
Expand Down
8 changes: 4 additions & 4 deletions AppmetrS2S.Tests/AppmetrS2S.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AppmetrS2S.Tests</RootNamespace>
<AssemblyName>AppmetrS2S.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -36,9 +37,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion AppmetrS2S.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net45" />
<package id="xunit" version="2.1.0-beta4-build3109" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0-beta4-build3109" targetFramework="net45" />
Expand Down
10 changes: 1 addition & 9 deletions AppmetrS2S/Actions/AddToPropertiesValue.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
namespace AppmetrS2S.Actions
{
#region using directives

using System;
using System.Runtime.Serialization;

#endregion

[DataContract]
public class AddToPropertiesValue : AppMetrAction
{
public const String ACTION = "addToPropertiesValue";
public const string ACTION = "addToPropertiesValue";

public AddToPropertiesValue() : base(ACTION)
{
Expand Down
38 changes: 19 additions & 19 deletions AppmetrS2S/Actions/AppMetrAction.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
namespace AppmetrS2S.Actions
using Newtonsoft.Json;

namespace AppmetrS2S.Actions
{
#region using directives

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

#endregion

[DataContract]
public abstract class AppMetrAction
{
[DataMember(Name = "action")]
private String _action;
[JsonProperty("action")]
private string _action;

[DataMember(Name = "timestamp")]
[JsonProperty("timestamp")]
private long _timestamp = Utils.GetNowUnixTimestamp();

[DataMember(Name = "userTime")]
[JsonProperty("userTime")]
private long? _userTime;

[DataMember(Name = "properties")]
private IDictionary<String, Object> _properties = new Dictionary<string, object>();
[JsonProperty("properties")]
private IDictionary<string, object> _properties = new Dictionary<string, object>();

[DataMember(Name = "userId")]
private String _userId;
[JsonProperty("userId")]
private string _userId;

protected AppMetrAction()
{
Expand All @@ -46,23 +46,23 @@ public AppMetrAction SetTimestamp(long timestamp)
return this;
}

public IDictionary<String, Object> GetProperties()
public IDictionary<string, object> GetProperties()
{
return _properties;
}

public AppMetrAction SetProperties(IDictionary<String, Object> properties)
public AppMetrAction SetProperties(IDictionary<string, object> properties)
{
_properties = properties;
return this;
}

public String GetUserId()
public string GetUserId()
{
return _userId;
}

public AppMetrAction SetUserId(String userId)
public AppMetrAction SetUserId(string userId)
{
_userId = userId;
return this;
Expand All @@ -71,23 +71,23 @@ public AppMetrAction SetUserId(String userId)
//http://codeblog.jonskeet.uk/2011/04/05/of-memory-and-strings/
public virtual int CalcApproximateSize()
{
int size = 40 + (40 * _properties.Count); //40 - Map size and 40 - each entry overhead
var size = 40 + (40 * _properties.Count); //40 - Map size and 40 - each entry overhead

size += GetStringLength(_action);
size += GetStringLength(Convert.ToString(_timestamp));
size += GetStringLength(_userId);

foreach (KeyValuePair<String, Object> pair in _properties) {
foreach (var pair in _properties) {
size += GetStringLength(pair.Key);
size += GetStringLength(pair.Value != null ? Convert.ToString(pair.Value) : null); //toString because sending this object via json
}

return 8 + size + 8; //8 - object header
}

protected int GetStringLength(String str)
protected int GetStringLength(string str)
{
return str == null ? 0 : str.Length * 2 + 26; //24 - String object size, 16 - char[]
return str?.Length * 2 + 26 ?? 0; //24 - String object size, 16 - char[]
}
}
}
21 changes: 11 additions & 10 deletions AppmetrS2S/Actions/AttachEntityAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace AppmetrS2S.Actions
using Newtonsoft.Json;

namespace AppmetrS2S.Actions
{
#region using directives

Expand All @@ -7,33 +9,32 @@

#endregion

[DataContract]
public class AttachEntityAttributes : AppMetrAction
{
public const String ACTION = "attachEntityAttributes";
public const string ACTION = "attachEntityAttributes";

[DataMember(Name = "entityName")]
private String _entityName;
[JsonProperty("entityName")]
private string _entityName;

[DataMember(Name = "entityValue")]
private String _entityValue;
[JsonProperty("entityValue")]
private string _entityValue;

protected AttachEntityAttributes()
{
}

public AttachEntityAttributes(String entityName, String entityValue) : base(ACTION)
public AttachEntityAttributes(string entityName, string entityValue) : base(ACTION)
{
_entityName = entityName;
_entityValue = entityValue;
}

public String GetEntityName()
public string GetEntityName()
{
return _entityName;
}

public String GetEntityValue()
public string GetEntityValue()
{
return _entityValue;
}
Expand Down
10 changes: 1 addition & 9 deletions AppmetrS2S/Actions/AttachProperties.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
namespace AppmetrS2S.Actions
{
#region using directives

using System;
using System.Runtime.Serialization;

#endregion

[DataContract]
public class AttachProperties : AppMetrAction
{
public const String ACTION = "attachProperties";
public const string ACTION = "attachProperties";

public AttachProperties() : base(ACTION)
{
Expand Down
21 changes: 8 additions & 13 deletions AppmetrS2S/Actions/Event.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
namespace AppmetrS2S.Actions
{
#region using directives

using System;
using System.Runtime.Serialization;
using Newtonsoft.Json;

#endregion
[DataContract]
namespace AppmetrS2S.Actions
{

public class Event : AppMetrAction
{
public const String ACTION = "trackEvent";
public const string ACTION = "trackEvent";

[DataMember(Name = "event")]
private String _event;
[JsonProperty("event")]
private string _event;

protected Event()
{
Expand All @@ -24,7 +19,7 @@ public Event(string eventName) : base(ACTION)
_event = eventName;
}

public String GetEvent()
public string GetEvent()
{
return _event;
}
Expand Down
11 changes: 3 additions & 8 deletions AppmetrS2S/Actions/Events.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
namespace AppmetrS2S.Actions
{
#region using directives

using System;

#endregion
public static class Events
public static class Events
{
public static Event ServerInstall(String userId)
public static Event ServerInstall(string userId)
{
Event _event = new Event("server/server_install");
var _event = new Event("server/server_install");
_event.SetUserId(userId);
return _event;
}
Expand Down
Loading

0 comments on commit 73ab090

Please sign in to comment.