-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from LisiLisenok/develop
Develop
- Loading branch information
Showing
146 changed files
with
5,529 additions
and
1,171 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,3 @@ source=examples | |
[defaults] | ||
encoding=UTF-8 | ||
|
||
[formattool] | ||
profile=my |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,4 @@ source=examples | |
|
||
[defaults] | ||
encoding=UTF-8 | ||
|
||
[formattool] | ||
profile=my | ||
|
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
45 changes: 45 additions & 0 deletions
45
examples/herd/examples/asynctest/benchmark/LockFreeQueue.ceylon
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import java.util.concurrent.atomic { | ||
AtomicReference | ||
} | ||
|
||
|
||
"Lock free queue." | ||
shared class LockFreeQueue<Item>() given Item satisfies Object { | ||
|
||
class Node ( | ||
shared variable Item? item = null, | ||
Node? nextNode = null | ||
) { | ||
shared AtomicReference<Node?> next = AtomicReference<Node?>( nextNode ); | ||
} | ||
|
||
|
||
AtomicReference<Node> head = AtomicReference<Node>( Node() ); | ||
AtomicReference<Node> tail = AtomicReference<Node>( head.get() ); | ||
|
||
shared void enqueue( Item item ) { | ||
Node newTail = Node( item ); | ||
variable Node oldTail = tail.get(); | ||
while ( !oldTail.next.compareAndSet( null, newTail ) ) { | ||
oldTail = tail.get(); | ||
} | ||
// adjust tail | ||
tail.compareAndSet( oldTail, newTail ); | ||
} | ||
|
||
shared Item? dequeue() { | ||
// current head | ||
variable Node oldHead = head.get(); | ||
// store next in order to retriev correct item even if next will be replaced | ||
variable Node? next = oldHead.next.get(); | ||
// shift head to the next | ||
while ( next exists, !head.compareAndSet( oldHead, next ) ) { | ||
oldHead = head.get(); | ||
next = oldHead.next.get(); | ||
} | ||
return next?.item; | ||
} | ||
|
||
shared Boolean empty => !head.get().next.get() exists; | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
examples/herd/examples/asynctest/benchmark/LockFreeQueueBenchmark.ceylon
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import ceylon.test { | ||
|
||
test | ||
} | ||
import herd.asynctest { | ||
|
||
async, | ||
AsyncTestContext | ||
} | ||
import herd.asynctest.benchmark { | ||
|
||
writeRelativeToFastest, | ||
benchmark, | ||
NumberOfLoops, | ||
Options, | ||
MultiBench, | ||
TimeUnit, | ||
CPUClock | ||
} | ||
|
||
|
||
"[[LockFreeQueue]] multithread producer-consumer benchmark." | ||
shared test async void lockFreeQueueProducerConsumer(AsyncTestContext context) { | ||
// queue to test | ||
LockFreeQueue<Integer> queue = LockFreeQueue<Integer>(); | ||
for ( i in 0 : 100 ) { queue.enqueue( 1 ); } | ||
|
||
writeRelativeToFastest ( | ||
context, | ||
benchmark ( | ||
Options(NumberOfLoops(500), NumberOfLoops(100), 100, TimeUnit.milliseconds, CPUClock), | ||
[ | ||
MultiBench( | ||
"producer-consumer", | ||
() => queue.enqueue( 1 ), | ||
queue.dequeue | ||
) | ||
], | ||
[1, 1], [2, 1], [1, 2], [2, 2] | ||
) | ||
); | ||
context.complete(); | ||
} |
50 changes: 50 additions & 0 deletions
50
examples/herd/examples/asynctest/benchmark/RandomTestFunction.ceylon
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import herd.asynctest { | ||
async, | ||
AsyncTestContext | ||
} | ||
import herd.asynctest.benchmark { | ||
NumberOfLoops, | ||
benchmark, | ||
Options, | ||
RandomFlow, | ||
SingleBench | ||
} | ||
import ceylon.test { | ||
test | ||
} | ||
import herd.asynctest.match { | ||
CloseTo | ||
} | ||
|
||
|
||
shared class RandomTestFunction() { | ||
|
||
|
||
variable Integer f1Calls = 0; | ||
variable Integer f2Calls = 0; | ||
variable Integer f3Calls = 0; | ||
|
||
void f1() => f1Calls ++; | ||
void f2() => f2Calls ++; | ||
void f3() => f3Calls ++; | ||
|
||
|
||
shared async test void selectFunctionRandomly(AsyncTestContext context) { | ||
benchmark ( | ||
Options(NumberOfLoops(1000), NumberOfLoops(100)), | ||
[ | ||
SingleBench ( | ||
"random test function", | ||
RandomFlow(1, [f1, 0.3], [f2, 0.2], [f3, 0.5]) | ||
) | ||
] | ||
); | ||
Integer sum = f1Calls + f2Calls + f3Calls; | ||
context.assertThat((f1Calls.float/sum*100+0.5).integer, CloseTo(30, 2), "first function % of calls", true); | ||
context.assertThat((f2Calls.float/sum*100+0.5).integer, CloseTo(20, 2), "second function % of calls", true); | ||
context.assertThat((f3Calls.float/sum*100+0.5).integer, CloseTo(50, 2), "third function % of calls", true); | ||
context.complete(); | ||
} | ||
|
||
} | ||
|
64 changes: 64 additions & 0 deletions
64
examples/herd/examples/asynctest/benchmark/isValueOrFunction.ceylon
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import herd.asynctest { | ||
AsyncTestContext, | ||
async | ||
} | ||
import ceylon.test { | ||
test | ||
} | ||
import herd.asynctest.benchmark { | ||
writeRelativeToFastest, | ||
benchmark, | ||
SingleBench, | ||
Options, | ||
NumberOfLoops | ||
} | ||
|
||
|
||
// benchmark parameters | ||
Integer integerValue => 1; | ||
Integer integerFunction() => 1; | ||
|
||
|
||
"Benchmark function - narrow to `Integer` firstly." | ||
Boolean isValue(Integer|Integer() source) { | ||
if (is Integer source) {return true;} | ||
else {return false;} | ||
} | ||
|
||
"Benchmark function - narrow to `Integer()` firstly." | ||
Boolean isFunction(Integer|Integer() source) { | ||
if (is Integer() source) {return true;} | ||
else {return false;} | ||
} | ||
|
||
|
||
"What is faster narrowing to function of to class?: | ||
Integer|Integer() source => ... | ||
... | ||
if (is Integer source) {return true; } | ||
else {return false;} | ||
or | ||
if (is Integer() source) {return true; } | ||
else {return false;} | ||
" | ||
shared test async void isValueOrFunction(AsyncTestContext context) { | ||
writeRelativeToFastest ( | ||
context, | ||
benchmark ( | ||
Options(NumberOfLoops(1000), NumberOfLoops(100)), | ||
[ | ||
SingleBench("value", isValue), | ||
SingleBench("function", isFunction) | ||
], | ||
[integerValue], [integerFunction] | ||
) | ||
); | ||
context.complete(); | ||
} | ||
|
Oops, something went wrong.