optimizations, tried to fix queue of longs songs.

This commit is contained in:
Master Kwoth 2016-02-28 18:42:34 +01:00
parent 09adb9854c
commit 6e23aa2d67
8 changed files with 90 additions and 46 deletions

View File

@ -124,7 +124,7 @@ namespace NadekoBot.Classes.Music {
$"**【 {SongInfo.Title.TrimTo(55)} 】**`{(SongInfo.Provider ?? "-")}`";
public SongInfo SongInfo { get; }
private PoopyBuffer songBuffer { get; } = new PoopyBuffer(2.MB());
private PoopyBuffer songBuffer { get; } = new PoopyBuffer(10.MB());
private bool prebufferingComplete { get; set; } = false;
public MusicPlayer MusicPlayer { get; set; }
@ -148,24 +148,24 @@ namespace NadekoBot.Classes.Music {
while (!cancelToken.IsCancellationRequested) {
int read = await p.StandardOutput.BaseStream.ReadAsync(buffer, 0, blockSize);
if (read == 0)
if (attempt++ == 10)
if (attempt++ == 20)
break;
else
await Task.Delay(50);
else
attempt = 0;
await songBuffer.WriteAsync(buffer, read, cancelToken);
if (songBuffer.ContentLength > 1.MB())
if (songBuffer.ContentLength > 2.MB())
prebufferingComplete = true;
}
Console.WriteLine("Buffering done.");
Console.WriteLine($"Buffering done. [{songBuffer.ContentLength}]");
});
internal async Task Play(IAudioClient voiceClient, CancellationToken cancelToken) {
var t = BufferSong(cancelToken).ConfigureAwait(false);
int bufferAttempts = 0;
int waitPerAttempt = 500;
int toAttemptTimes = SongInfo.ProviderType != MusicType.Normal ? 4 : 8;
int toAttemptTimes = SongInfo.ProviderType != MusicType.Normal ? 5 : 9;
while (!prebufferingComplete && bufferAttempts++ < toAttemptTimes) {
await Task.Delay(waitPerAttempt);
}

View File

@ -7,10 +7,8 @@ using System.Linq;
using NadekoBot.Extensions;
using System.Threading.Tasks;
namespace NadekoBot
{
public class NadekoStats
{
namespace NadekoBot {
public class NadekoStats {
public string BotVersion = "NadekoBot 0.8-beta14";
private static readonly NadekoStats _instance = new NadekoStats();
@ -23,6 +21,10 @@ namespace NadekoBot
private string _statsCache = "";
private Stopwatch _statsSW = new Stopwatch();
public int ServerCount { get; private set; } = 0;
public int TextChannelsCount { get; private set; } = 0;
public int VoiceChannelsCount { get; private set; } = 0;
List<string> messages = new List<string>();
static NadekoStats() { }
@ -37,6 +39,50 @@ namespace NadekoBot
Task.Run(() => StartCollecting());
Console.WriteLine("Logging enabled.");
ServerCount = _client.Servers.Count();
var channels = _client.Servers.SelectMany(s => s.AllChannels);
TextChannelsCount = channels.Where(c => c.Type == ChannelType.Text).Count();
VoiceChannelsCount = channels.Count() - TextChannelsCount;
_client.JoinedServer += (s, e) => {
try {
ServerCount++;
TextChannelsCount += e.Server.TextChannels.Count();
VoiceChannelsCount += e.Server.VoiceChannels.Count();
}
catch { }
};
_client.LeftServer += (s, e) => {
try {
ServerCount--;
TextChannelsCount -= e.Server.TextChannels.Count();
VoiceChannelsCount -= e.Server.VoiceChannels.Count();
}
catch { }
};
_client.ChannelCreated += (s, e) => {
try {
if (e.Channel.IsPrivate)
return;
if (e.Channel.Type == ChannelType.Text)
TextChannelsCount++;
else if (e.Channel.Type == ChannelType.Voice)
VoiceChannelsCount++;
}
catch { }
};
_client.ChannelDestroyed += (s, e) => {
try {
if (e.Channel.IsPrivate)
return;
if (e.Channel.Type == ChannelType.Text)
VoiceChannelsCount++;
else if (e.Channel.Type == ChannelType.Voice)
VoiceChannelsCount--;
}
catch { }
};
}
public TimeSpan GetUptime() =>
@ -47,7 +93,8 @@ namespace NadekoBot
return time.Days + " days, " + time.Hours + " hours, and " + time.Minutes + " minutes.";
}
public void LoadStats() {
public Task LoadStats() =>
Task.Run(() => {
var sb = new System.Text.StringBuilder();
sb.AppendLine("`Author: Kwoth` `Library: Discord.Net`");
//$"\nDiscord.Net version: {DiscordConfig.LibVersion}" +
@ -57,19 +104,20 @@ namespace NadekoBot
sb.AppendLine($"`Bot id: {_client.CurrentUser.Id}`");
sb.AppendLine($"`Owner id: {NadekoBot.OwnerID}`");
sb.AppendLine($"`Uptime: {GetUptimeString()}`");
sb.Append($"`Servers: {_client.Servers.Count()}");
sb.AppendLine($" | Channels: {_client.Servers.Sum(s => s.AllChannels.Count())}`");
sb.Append($"`Servers: {ServerCount}");
sb.Append($" | TextChannels: {TextChannelsCount}");
sb.AppendLine($" | VoiceChannels: {VoiceChannelsCount}`");
//$"\nUsers: {_client.Servers.SelectMany(x => x.Users.Select(y => y.Id)).Count()} (non-unique)" +
sb.AppendLine($"`Heap: {Math.Round((double)GC.GetTotalMemory(true) / 1.MiB(), 2).ToString()} MB`");
sb.AppendLine($"`Heap: {Math.Round((double)GC.GetTotalMemory(false) / 1.MiB(), 2).ToString()} MB`");
sb.AppendLine($"`Commands Ran this session: {_commandsRan}`");
sb.AppendLine($"`Message queue size:{_client.MessageQueue.Count}`");
sb.AppendLine($"`Greeted {Commands.ServerGreetCommand.Greeted} times.`");
_statsCache = sb.ToString();
}
});
public string GetStats() {
if (_statsSW.ElapsedTicks > 5) {
LoadStats();
public async Task<string> GetStats() {
if (_statsSW.Elapsed.Seconds > 5) {
await LoadStats();
_statsSW.Restart();
}
return _statsCache;
@ -91,15 +139,15 @@ namespace NadekoBot
ConnectedServers = connectedServers,
DateAdded = DateTime.Now
});
} catch {
}
catch {
Console.WriteLine("DB Exception in stats collecting.");
break;
}
}
}
//todo - batch save this
private void StatsCollector_RanCommand(object sender, CommandEventArgs e)
{
private void StatsCollector_RanCommand(object sender, CommandEventArgs e) {
try {
_commandsRan++;
Classes.DBHandler.Instance.InsertData(new Classes._DataModels.Command {
@ -112,7 +160,8 @@ namespace NadekoBot
CommandName = e.Command.Text,
DateAdded = DateTime.Now
});
} catch {
}
catch {
Console.WriteLine("Parse error in ran command.");
}
}

View File

@ -368,21 +368,16 @@ namespace NadekoBot.Modules {
cgb.CreateCommand(".stats")
.Description("Shows some basic stats for Nadeko.")
.Do(async e => {
var t = Task.Run(() => {
return NadekoStats.Instance.GetStats(); //+ "`" + Music.GetMusicStats() + "`";
await e.Channel.SendMessage(await NadekoStats.Instance.GetStats());
});
await e.Channel.SendMessage(await t);
});
/*
cgb.CreateCommand(".leaveall")
.Description("Nadeko leaves all servers **OWNER ONLY**")
.Do(e => {
if (e.User.Id == NadekoBot.OwnerID)
NadekoBot.client.Servers.ForEach(async s => { if (s.Name == e.Server.Name) return; await s.Leave(); });
});
*/
cgb.CreateCommand(".prune")
.Parameter("num", ParameterType.Required)
.Description("Prunes a number of messages from the current channel.\n**Usage**: .prune 5")

View File

@ -69,7 +69,7 @@ namespace NadekoBot {
LogLevel = LogSeverity.Warning,
LogHandler = (s, e) => {
try {
Console.WriteLine($"Severity: {e.Severity}\nMessage: {e.Message}\nExceptionMessage: {e.Exception?.Message ?? "-"}\nException: {(e.Exception?.ToString() ?? "-")}");
Console.WriteLine($"Severity: {e.Severity}\nMessage: {e.Message}\nExceptionMessage: {e.Exception?.Message ?? "-"}");//\nException: {(e.Exception?.ToString() ?? "-")}");
}
catch { }
}
@ -135,7 +135,7 @@ namespace NadekoBot {
return;
}
Console.WriteLine("-----------------");
Console.WriteLine(NadekoStats.Instance.GetStats());
Console.WriteLine(await NadekoStats.Instance.GetStats());
Console.WriteLine("-----------------");
try {

Binary file not shown.