Last active
July 31, 2024 08:14
-
-
Save xoofx/a9310a18153c9f0110fb077fb3f0f57a to your computer and use it in GitHub Desktop.
Bench Windows Terminal with a wall of 24-bits RGB ANSI scrolling chars
This file contains 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
// Bench Windows Terminal with a wall of 24-bits RGB ANSI scrolling chars | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
var width = Console.WindowWidth; | |
var height = Console.WindowHeight; | |
var total = width * height; | |
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | |
{ | |
WindowsHelper.EnableAnsiEscapeOnWindows(); | |
} | |
var builder = new StringBuilder(); | |
var totalMilli = 0.0; | |
// Enforce utf8 | |
Console.OutputEncoding = Encoding.UTF8; | |
// Use the stream for rendering utf8 directly | |
var stream = Console.OpenStandardOutput(); | |
Console.CursorVisible = false; | |
// Switch to alternate screen for the benchmark | |
Console.Out.Write("\x1b[?1049h"); | |
Console.Out.Flush(); | |
var clock = Stopwatch.StartNew(); | |
const int count = 1000; | |
int i = 0; | |
for (i = 0; i < count; i++) | |
{ | |
builder.Length = 0; | |
// Set cursor position 0,0 | |
builder.Append($"\x1b[0;0H"); | |
{ | |
for (int c = 0; c < total; c++) | |
{ | |
var r = (byte)(c + i); | |
var g = (byte)(r * 2); | |
var b = (byte)(r * 4); | |
var color = $"\x1b[38;2;{r};{g};{b}m"; | |
var o = (char)(' ' + 1 + (r % ('z' - ' '))); | |
builder.Append(color); | |
builder.Append(o); | |
} | |
var reset = "\x1b[0m"; | |
builder.Append(reset); | |
} | |
var buffer = builder.ToString().ToCharArray(); | |
var utf8Buffer = Encoding.UTF8.GetBytes(buffer); | |
clock.Restart(); | |
stream.Write(utf8Buffer); | |
stream.Flush(); | |
clock.Stop(); | |
totalMilli += clock.Elapsed.TotalMilliseconds; | |
Console.Title = $"[{i + 1}/{count}] Width x Height = {width}x{height} = {total}, Elapsed: {totalMilli / (i + 1)}ms"; | |
if (Console.KeyAvailable) | |
{ | |
break; | |
} | |
} | |
// Switch back to main screen | |
Console.Out.Write("\x1b[?1049l"); | |
Console.Out.Flush(); | |
Console.ResetColor(); | |
Console.CursorVisible = true; | |
Console.WriteLine($"{width}x{height} = {total} Elapsed: {totalMilli / (i + 1)}ms"); | |
internal static class WindowsHelper | |
{ | |
private const int STD_OUTPUT_HANDLE = -11; | |
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; | |
public static void EnableAnsiEscapeOnWindows() | |
{ | |
var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
if (iStdOut != IntPtr.Zero && GetConsoleMode(iStdOut, out uint outConsoleMode)) | |
{ | |
SetConsoleMode(iStdOut, outConsoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); | |
} | |
} | |
[DllImport("kernel32")] | |
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); | |
[DllImport("kernel32")] | |
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); | |
[DllImport("kernel32")] | |
private static extern IntPtr GetStdHandle(int nStdHandle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment