Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A .NET library for carefully refactoring critical paths. It's a port of GitHub's Ruby Scientist library

License

NotificationsYou must be signed in to change notification settings

scientistproject/Scientist.net

Repository files navigation

A .NET Port of theScientist library for carefully refactoring critical paths.

Build statusGitter

To give it a twirl, use NuGet to install:Install-Package Scientist

How do I science?

Let's pretend you're changing the way you handle permissions in a large web app. Tests can help guide your refactoring, but you really want to compare the current and refactored behaviors under load.

usingGitHub;...public boolCanAccess(IUseruser){returnScientist.Science<bool>("widget-permissions", experiment=>{experiment.Use(()=>IsCollaborator(user));// old wayexperiment.Try(()=>HasAccess(user));// new way});// returns the control value}

Wrap aUse block around the code's original behavior, and wrapTry around the new behavior. InvokingScientist.Science<T> will always return whatever theUse block returns, but it does a bunch of stuff behind the scenes:

  • It decides whether or not to run theTry block,
  • Randomizes the order in whichUse andTry blocks are run,
  • Measures the durations of all behaviors,
  • Compares the result ofTry to the result ofUse,
  • Swallows (but records) any exceptions raised in theTry block, and
  • Publishes all this information.

TheUse block is called thecontrol. TheTry block is called thecandidate.

If you don't declare anyTry blocks, none of the Scientist machinery is invoked and the control value is always returned.

Making science useful

Publishing results

What good is science if you can't publish your results?

By default results are published in an in-memory publisher. To override this behavior, create your own implementation ofIResultPublisher:

publicclassMyResultPublisher:IResultPublisher{publicTaskPublish<T,TClean>(Result<T,TClean>result){Logger.Debug($"Publishing results for experiment '{result.ExperimentName}'");Logger.Debug($"Result:{(result.Matched?"MATCH":"MISMATCH")}");Logger.Debug($"Control value:{result.Control.Value}");Logger.Debug($"Control duration:{result.Control.Duration}");foreach(varobservationinresult.Candidates){Logger.Debug($"Candidate name:{observation.Name}");Logger.Debug($"Candidate value:{observation.Value}");Logger.Debug($"Candidate duration:{observation.Duration}");}if(result.Mismatched){// saved mismatched experiments to DBDbHelpers.SaveExperimentResults(result);}returnTask.FromResult(0);}}

Then set Scientist to use it before running the experiments:

Scientist.ResultPublisher=newMyResultPublisher();

As of v1.0.2, AIResultPublisher can also be wrapped inFireAndForgetResultPublisher so that result publishing avoids any delays in running experiments and is delegated to another thread:

Scientist.ResultPublisher=newFireAndForgetResultPublisher(newMyResultPublisher(onPublisherException));

Controlling comparison

Scientist compares control and candidate values using==. To override this behavior, useCompare to define how to compare observed values instead:

publicIUserGetCurrentUser(stringhash){returnScientist.Science<IUser>("get-current-user", experiment=>{experiment.Compare((x,y)=>x.Name==y.Name);experiment.Use(()=>LookupUser(hash));experiment.Try(()=>RetrieveUser(hash));});}

Adding context

Results aren't very useful without some way to identify them. Use theAddContext method to add to the context for an experiment:

publicIUserGetUserByName(stringuserName){returnScientist.Science<IUser>("get-user-by-name", experiment=>{experiment.AddContext("username",userName);experiment.Use(()=>FindUser(userName));experiment.Try(()=>GetUser(userName));});}

AddContext takes a string identifier and an object value, and adds them to an internalDictionary. When you publish the results, you can access the context by using theContexts property:

publicclassMyResultPublisher:IResultPublisher{publicTaskPublish<T,TClean>(Result<T,TClean>result){foreach(varkvpinresult.Contexts){Console.WriteLine($"Key:{kvp.Key}, Value:{kvp.Value}");}returnTask.FromResult(0);}}

Expensive setup

If an experiment requires expensive setup that should only occur when the experiment is going to be run, define it with theBeforeRun method:

publicintDoSomethingExpensive(){returnScientist.Science<int>("expensive-but-worthwile", experiment=>{experiment.BeforeRun(()=>ExpensiveSetup());experiment.Use(()=>TheOldWay());experiment.Try(()=>TheNewWay());});}

Keeping it clean

Sometimes you don't want to store the full value for later analysis. For example, an experiment may returnIUser instances, but when researching a mismatch, all you care about is the logins. You can define how to clean these values in an experiment:

publicIUserGetUserByEmail(stringemailAddress){returnScientist.Science<IUser,string>("get-user-by-email", experiment=>{experiment.Use(()=>OldApi.FindUserByEmail(emailAddress));experiment.Try(()=>NewApi.GetUserByEmail(emailAddress));experiment.Clean(user=>user.Login);});}

And this cleaned value is available in the final published result:

publicclassMyResultPublisher:IResultPublisher{publicTaskPublish<T,TClean>(Result<T,TClean>result){// result.Control.Value = <IUser object>IUseruser=(IUser)result.Control.Value;Console.WriteLine($"Login from raw object:{user.Login}");// result.Control.CleanedValue = "user name"Console.WriteLine($"Login from cleaned object:{result.Control.CleanedValue}");returnTask.FromResult(0);}}

Ignoring mismatches

During the early stages of an experiment, it's possible that some of your code will always generate a mismatch for reasons you know and understand but haven't yet fixed. Instead of these known cases always showing up as mismatches in your metrics or analysis, you can tell an experiment whether or not to ignore a mismatch using theIgnore method. You may include more than one block if needed:

publicboolCanAccess(IUseruser){returnScientist.Science<bool>("widget-permissions", experiment=>{experiment.Use(()=>IsCollaborator(user));experiment.Try(()=>HasAccess(user));// user is staff, always an admin in the new systemexperiment.Ignore((control,candidate)=>user.IsStaff);// new system doesn't handle unconfirmed users yetexperiment.Ignore((control,candidate)=>control&&!candidate&&!user.ConfirmedEmail);});}

The ignore blocks are only called if thevalues don't match. If one observation raises an exception and the other doesn't, it's always considered a mismatch. If both observations raise different exceptions, that is also considered a mismatch.

Enabling/disabling experiments

Sometimes you don't want an experiment to run. Say, disabling a new codepath for anyone who isn't staff. You can disable an experiment by setting aRunIf block. If this returnsfalse, the experiment will merely return the control value. Otherwise, it defers to the globalScientist.Enabled method.

publicdecimalGetUserStatistic(IUseruser){returnScientist.Science<decimal>("new-statistic-calculation", experiment=>{experiment.RunIf(()=>user.IsTestSubject);experiment.Use(()=>CalculateStatistic(user));experiment.Try(()=>NewCalculateStatistic(user));});}

Ramping up experiments

As a scientist, you know it's always important to be able to turn your experiment off, lest it run amok and result in villagers with pitchforks on your doorstep. You can set a global switch to control whether or not experiments is enabled by using theScientist.Enabled method.

intpercentEnabled=10;Randomrand=newRandom();Scientist.Enabled(()=>{returnrand.Next(100)<percentEnabled;});

This code will be invoked for every method with an experiment every time, so be sensitive about its performance. For example, you can store an experiment in the database but wrap it in various levels of caching.

Running candidates in parallel (asynchronous)

Scientist runs tasks synchronously by default. This can end up doubling (more or less) the time it takes the original method call to complete, depending on how many candidates are added and how long they take to run.

In cases where Scientist is used for production refactoring, for example, this ends up causing the calling method to return slower than before which may affect the performance of your original code. However, if the candidates can be run at the same time as the control method without affecting each other, then they can be run in parallel so the Scientist call will only take as long as the slowest task (plus a tiny bit of overhead):

awaitScientist.ScienceAsync<int>("ExperimentName",3,// number of tasks to run concurrentlyexperiment=>{experiment.Use(async()=>awaitStartRunningSomething(myData));experiment.Try(async()=>awaitRunAtTheSameTimeAsTheControlMethod(myData));experiment.Try(async()=>awaitAlsoRunThisConcurrently(myData));});

As always when using async/await, don't forget to call.ConfigureAwait(false) where appropriate.

Testing

When running your test suite, it's helpful to know that the experimental results always match. To help with testing, Scientist has aThrowOnMismatches property that can be set totrue. Only do this in your test suite!

To throw on mismatches:

Scientist.Science<int>("ExperimentN", experiment=>{experiment.ThrowOnMismatches=true;// ...});

Scientist will throw aMismatchException<T, TClean> exception if any observations don't match.

Handling errors

If an exception is thrown in any of Scientist's internal helpers likeCompare,Enabled, orIgnore, the default behavior of Scientist is to re-throw that exception. Since this halts the experiment entirely, it's often a better idea to handle this error and continue so the experiment as a whole isn't canceled entirely:

Scientist.Science<int>("ExperimentCatch", experiment=>{experiment.Thrown((operation,exception)=>InternalTracker.Track($"Science failure in ExperimentCatch:{operation}.",exception))// ...});

The operations that may be handled here are:

  • Operation.Compare - an exception is raised in aCompare block
  • Operation.Enabled - an exception is raised in theEnabled block
  • Operation.Ignore - an exception is raised in anIgnore block
  • Operation.Publish - an exception is raised while publishing results
  • Operation.RunIf - an exception is raised in aRunIf block

Designing an experiment

BecauseEnabled andRunIf determine when a candidate runs, it's impossible to guarantee that it will run every time. For this reason, Scientist is only safe for wrapping methods that aren't changing data.

When using Scientist, we've found it most useful to modify both the existing and new systems simultaneously anywhere writes happen, and verify the results at read time withScience.ThrowOnMismatches has also been useful to ensure that the correct data was written during tests, and reviewing published mismatches has helped us find any situations we overlooked with our production data at runtime. When writing to and reading from two systems, it's also useful to write some data reconciliation scripts to verify and clean up production data alongside any running experiments.

Finishing an experiment

As your candidate behavior converges on the controls, you'll start thinking about removing an experiment and using the new behavior.

  • If there are any ignore blocks, the candidate behavior isguaranteed to be different. If this is unacceptable, you'll need to remove the ignore blocks and resolve any ongoing mismatches in behavior until the observations match perfectly every time.
  • When removing a read-behavior experiment, it's a good idea to keep any write-side duplication between an old and new system in place until well after the new behavior has been in production, in case you need to roll back.

Breaking the rules

Sometimes scientists just gotta do weird stuff. We understand.

Ignoring results entirely

Science is useful even when all you care about is the timing data or even whether or not a new code path blew up. If you have the ability to incrementally control how often an experiment runs via yourEnabled method, you can use it to silently and carefully test new code paths and ignore the results altogether. You can do this by settingIgnore((x, y) => true), or for greater efficiency,Compare((x, y) => true).

This will still log mismatches if any exceptions are raised, but will disregard the values entirely.

Trying more than one thing

It's not usually a good idea to try more than one alternative simultaneously. Behavior isn't guaranteed to be isolated and reporting + visualization get quite a bit harder. Still, it's sometimes useful.

To try more than one alternative at once, add names to someTry blocks:

publicboolCanAccess(IUseruser){returnScientist.Science<bool>("widget-permissions", experiment=>{experiment.Use(()=>IsCollaborator(user));experiment.Try("api",()=>HasAccess(user));experiment.Try("raw-sql",()=>HasAccessSql(user));});}

Alternatives

Here are other implementations of Scientist available in different languages.

About

A .NET library for carefully refactoring critical paths. It's a port of GitHub's Ruby Scientist library

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp