') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); GitHub - nessos/PerfUtil: A simple F# utility for testing performance · GitHub
Skip to content

Repository files navigation

PerfUtil No Maintenance Intended

This Library is obsolete and no longer maintained. Please use a modern, properly implemented alternative like BenchmarkDotNet

A collection of tools and abstractions for helping performance tests. Two main operation modes are provided:

  • Comparison of a given implementation against others.
  • Comparison of current implementation against a history of past performance tests.

A NuGet package is available here.

Basic Usage

openPerfUtilletresult= Benchmark.Run (repeat 100(fun()-> Thread.Sleep 10))valresult:PerfResult ={TestId ="";
SessionId ="";
Date = 2/12/2013 7:30:01 pm;
Error = null;
Elapsed = 00:00:00.9998810;
CpuTime = 00:00:00;
GcDelta =[0; 0; 0];}

Comparing implementations

Defining a test context:

typeIOperation=inherit ITestable
abstractRun :unit->unitletdummy name (interval:int)={new IOperation withmember__.Name= name
member__.Run()= System.Threading.Thread.Sleep(interval)}lettested= dummy "foo"10

Testing against other implementations

lettestBed=new OtherImplemantationTester<IOperation>(tested,[dummy "bar"5; dummy "baz"20])
testBed.Test "test 0"(repeat 100(fun o -> o.Run()))// Output// 'test 0': foo was 0.50x faster and 1.00x more memory efficient than bar.// 'test 0': foo was 2.00x faster and 1.00x more memory efficient than baz.

Testing against past test runs

lettest=new PastImplementationTester<IOperation>(tested, Version(0,3), historyFile ="persist.xml")
test.Test "test 0"(repeat 100(fun o -> o.Run()))// Output// 'test 0': 'foo v.0.3' was 1.00x faster and 1.00x more memory efficient than 'foo v.0.1'.// 'test 0': 'foo v.0.3' was 1.00x faster and 1.00x more memory efficient than 'foo v.0.2'.// append current results to history file
test.PersistCurrentResults()

Defining abstract performance tests

In PerfUtil, an abstract performance test can be represented with the record:

typePerfTest<IOperation>={
Id :string
Test :IOPeration -> unit}

Performance tests can be declared in the following manner:

typeTests=[<PerfTest>]static member``Test 1`` (o :IOperation)= o.Run ()[<PerfTest>]static member``Test 2`` (o :IOperation)= o |> repeat 100(fun o -> o.Run ())lettests= PerfTest<IOperation>.OfType<Tests>()// val tests : PerfTest<IOperation> list =// [{Id = "Tests.Test 1";// Test = <fun:Wrap@90>;}; {Id = "Tests.Test 2";// Test = <fun:Wrap@90>;}]

Tests can then be run with a concrete performance tester like so:

tests |> PerfTest.run (fun()->new PastImplementationTester<IOperation>(...))

It is possible to define performance tests in F# modules using the following technique:

moduleTests =typeMarker=classend[<PerfTest>]let``Test 0`` (o :IOperation)= o.Run ()lettest= PerfTest<IOperation>.OfModuleMarker<Tests.Marker>()|> List.head

NUnit Support

A collection of performance tests can be used to define NUnit tests. To do so, simply place a concrete instance of the NUnitPerf abstract class in your assembly.

[<AbstractClass>][<TestFixture>]typeNUnitPerf<'Implwhen'Impl:>ITestable>()=abstractPerfTester :PerformanceTester<'Impl>abstractPerfTests :PerfTest<'Impl>list

Plotting Results

Using FSharp.Charting, the following code provides a way to plot test results:

openFSharp.ChartingopenPerfUtil// simple plot functionletplot yaxis (metric :PerfResult ->float)(results :PerfResult list)=letvalues= results |> List.choose (fun r ->if r.HasFailed then None else Some (r.SessionId, metric r))letname= results |> List.tryPick (fun r -> Some r.TestId)letch= Chart.Bar(values, ?Name = name, ?Title = name, YTitle = yaxis)
ch.ShowChart()// read performance tests from 'Tests' module and run themletresults=
PerfTest<IOperation>.OfModuleMarker<Tests.Marker>()|> PerfTest.run SerializerComparer.Create
// plot everything
TestSession.groupByTest results
|> Map.iter (fun _ r -> plot "milliseconds"(fun r -> r.Elapsed.TotalMilliseconds) r)

Case Study

For more in-depth examples, I have included a simple performance testing implementation for the FsPickler serializer, which can be found in the PerfUtil.CaseStudy project.

About

A simple F# utility for testing performance

Resources

Stars

33 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages