#!/usr/bin/env perl

my @animals = ("Canidae","Felidae","Cat","Cattle","Dog","Donkey","Goat","Guinea pig","Horse","Pig","Rabbit","Fancy rat varieties","laboratory rat strains","Sheep breeds","Water buffalo breeds","Chicken breeds","Duck breeds","Goose breeds","Pigeon breeds","Turkey breeds","Aardvark","Aardwolf","African buffalo","African elephant","African leopard","Albatross","Alligator","Alpaca","American buffalo (bison)","American robin","Amphibian","list","Anaconda","Angelfish","Anglerfish","Ant","Anteater","Antelope","Antlion","Ape","Aphid","Arabian leopard","Arctic Fox","Arctic Wolf","Armadillo","Arrow crab","Asp","Ass (donkey)","Baboon","Badger","Bald eagle","Bandicoot","Barnacle","Barracuda","Basilisk","Bass","Bat","Beaked whale","Bear","list","Beaver","Bedbug","Bee","Beetle","Bird","list","Bison","Blackbird","Black panther","Black widow spider","Blue bird","Blue jay","Blue whale","Boa","Boar","Bobcat","Bobolink","Bonobo","Booby","Box jellyfish","Bovid","Buffalo, African","Buffalo, American (bison)","Bug","Butterfly","Buzzard","Camel","Canid","Cape buffalo","Capybara","Cardinal","Caribou","Carp","Cat","list","Catshark","Caterpillar","Catfish","Cattle","list","Centipede","Cephalopod","Chameleon","Cheetah","Chickadee","Chicken","list","Chimpanzee","Chinchilla","Chipmunk","Clam","Clownfish","Cobra","Cockroach","Cod","Condor","Constrictor","Coral","Cougar","Cow","Coyote","Crab","Crane","Crane fly","Crawdad","Crayfish","Cricket","Crocodile","Crow","Cuckoo","Cicada","Damselfly","Deer","Dingo","Dinosaur","list","Dog","list","Dolphin","Donkey","list","Dormouse","Dove","Dragonfly","Dragon","Duck","list","Dung beetle","Eagle","Earthworm","Earwig","Echidna","Eel","Egret","Elephant","Elephant seal","Elk","Emu","English pointer","Ermine","Falcon","Ferret","Finch","Firefly","Fish","Flamingo","Flea","Fly","Flyingfish","Fowl","Fox","Frog","Fruit bat","Gamefowl","list","Galliform","list","Gazelle","Gecko","Gerbil","Giant panda","Giant squid","Gibbon","Gila monster","Giraffe","Goat","list","Goldfish","Goose","list","Gopher","Gorilla","Grasshopper","Great blue heron","Great white shark","Grizzly bear","Ground shark","Ground sloth","Grouse","Guan","list","Guanaco","Guineafowl","list","Guinea pig","list","Gull","Guppy","Haddock","Halibut","Hammerhead shark","Hamster","Hare","Harrier","Hawk","Hedgehog","Hermit crab","Heron","Herring","Hippopotamus","Hookworm","Hornet","Horse","list","Hoverfly","Hummingbird","Humpback whale","Hyena","Iguana","Impala","Irukandji jellyfish","Jackal","Jaguar","Jay","Jellyfish","Junglefowl","Kangaroo","Kangaroo mouse","Kangaroo rat","Kingfisher","Kite","Kiwi","Koala","Koi","Komodo dragon","Krill","Ladybug","Lamprey","Landfowl","Land snail","Lark","Leech","Lemming","Lemur","Leopard","Leopon","Limpet","Lion","Lizard","Llama","Lobster","Locust","Loon","Louse","Lungfish","Lynx","Macaw","Mackerel","Magpie","Mammal","Manatee","Mandrill","Manta ray","Marlin","Marmoset","Marmot","Marsupial","Marten","Mastodon","Meadowlark","Meerkat","Mink","Minnow","Mite","Mockingbird","Mole","Mollusk","Mongoose","Monitor lizard","Monkey","Moose","Mosquito","Moth","Mountain goat","Mouse","Mule","Muskox","Narwhal","Newt","New World quail","Nightingale","Ocelot","Octopus","Old World quail","Opossum","Orangutan","Orca","Ostrich","Otter","Owl","Ox","Panda","Panther","Panthera hybrid","Parakeet","Parrot","Parrotfish","Partridge","Peacock","Peafowl","Pelican","Penguin","Perch","Peregrine falcon","Pheasant","Pig","Pigeon","list","Pike","Pilot whale","Pinniped","Piranha","Planarian","Platypus","Polar bear","Pony","Porcupine","Porpoise","Portuguese man o' war","Possum","Prairie dog","Prawn","Praying mantis","Primate","Ptarmigan","Puffin","Puma","Python","Quail","Quelea","Quokka","Rabbit","list","Raccoon","Rainbow trout","Rat","Rattlesnake","Raven","Ray (Batoidea)","Ray (Rajiformes)","Red panda","Reindeer","Reptile","Rhinoceros","Right whale","Roadrunner","Rodent","Rook","Rooster","Roundworm","Saber-toothed cat","Sailfish","Salamander","Salmon","Sawfish","Scale insect","Scallop","Scorpion","Seahorse","Sea lion","Sea slug","Sea snail","Shark","list","Sheep","list","Shrew","Shrimp","Silkworm","Silverfish","Skink","Skunk","Sloth","Slug","Smelt","Snail","Snake","list","Snipe","Snow leopard","Sockeye salmon","Sole","Sparrow","Sperm whale","Spider","Spider monkey","Spoonbill","Squid","Squirrel","Starfish","Star-nosed mole","Steelhead trout","Stingray","Stoat","Stork","Sturgeon","Sugar glider","Swallow","Swan","Swift","Swordfish","Swordtail","Tahr","Takin","Tapir","Tarantula","Tarsier","Tasmanian devil","Termite","Tern","Thrush","Tick","Tiger","Tiger shark","Tiglon","Toad","Tortoise","Toucan","Trapdoor spider","Tree frog","Trout","Tuna","Turkey","list","Turtle","Tyrannosaurus","Urial","Vampire bat","Vampire squid","Vicuna","Viper","Vole","Vulture","Wallaby","Walrus","Wasp","Warbler","Water Boa","Water buffalo","Weasel","Whale","Whippet","Whitefish","Whooping crane","Wildcat","Wildebeest","Wildfowl","Wolf","Wolverine","Wombat","Woodpecker","Worm","Wren","Xerinae","X-ray fish","Yak","Yellow perch","Zebra");


use strict;
use warnings;

use Benchmark qw/cmpthese timethese/;
use Data::Dumper;

use Koha::SearchEngine::Elasticsearch::Search;

my @animals_10 = ();
my @animals_50 = ();
my @animals_100 = ();

for( 1..10){
    push @animals_10, { facet_label_value => $animals[rand @animals] };
}
for( 1..50){
    push @animals_50, { facet_label_value => $animals[rand @animals] };
}
for( 1..100){
    push @animals_100, { facet_label_value => $animals[rand @animals] };
}


cmpthese(
    -10,
    {
        sort_10_standard => sub {
            my @sorted = sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @animals_10;
        },
        sort_10_locale => sub {
            my @sorted = Koha::SearchEngine::Elasticsearch::Search::_sort_facets(undef,{ facets=>\@animals_10})
        },
        sort_50_standard => sub {
            my @sorted = sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @animals_50;
        },
        sort_50_locale => sub {
            my @sorted = Koha::SearchEngine::Elasticsearch::Search::_sort_facets(undef,{ facets=>\@animals_50})
        },
        sort_100_standard => sub {
            my @sorted = sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @animals_100;
        },
        sort_100_locale => sub {
            my @sorted = Koha::SearchEngine::Elasticsearch::Search::_sort_facets(undef,{ facets=>\@animals_100})
        },
    }
);

